Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

readers-writers.c sample program: #include #include #include #include /* GLOBAL SHARED DATA ====================================================== */ unsigned int gSharedValue = 0; pthread_mutex_t gSharedMemoryLock = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t gReadPhase =

image text in transcribed

readers-writers.c sample program:

#include

#include

#include

#include

/* GLOBAL SHARED DATA ====================================================== */

unsigned int gSharedValue = 0;

pthread_mutex_t gSharedMemoryLock = PTHREAD_MUTEX_INITIALIZER;

pthread_cond_t gReadPhase = PTHREAD_COND_INITIALIZER;

pthread_cond_t gWritePhase = PTHREAD_COND_INITIALIZER;

int gReaders = 0;

void *readerMain(void *threadArgument) {

int id = *((int*)threadArgument);

// reader code

pthread_exit(0);

}

void *writerMain(void *threadArgument) {

int id = *((int*)threadArgument);

// writer code

pthread_exit(0);

}

// usage of usleep and fprintf

// more options can be found using man

void sample_code(){

int random_number = random();

int wait_time = random_number % 10;

fprintf(stdout, "We are going to wait %d seconds. And the random number is %d. ", wait_time, random_number);

// use usleep to make thread sleep

// so that reads and writes do not all happen at once

usleep(1000000 * wait_time);

fprintf(stdout, "Now we are free again. ");

}

int main(int argc, char **argv) {

// Seed the random number generator, requied by using random() function

srandom((unsigned int)time(NULL));

// comment out the call to sample_code()

sample_code();

return 0;

}

5. Write a multi-threaded C program for readers/writers problem using Pthreads. This program should adhere to the following constraints: 1. 2. 3. 4. Multiple readers/writers must be supported (at least 4 of each type); Readers must read the shared variable multiple times (at least 4 times); Writers must write the shared variable multiple times (at least 4 times); Readers must print: a. b. C. The reader thread ID, The value of the shared variable, The number of readers present when value is read; 5. Writers must print: a. b. c. The writer thread ID, The written value to the shared variable, The number of readers present were when value is written. The print out can be implemented using fprintf (). A sample code can be found in readers- random amount of time. This will help ensure that reads and writes do not occur all at once. For this purpose, you can use srandom) and usleep ). A sample_code() can be found in readers- writers.c

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions