Menu
support@authoritypapers.com
+1(805) 568 7317

write a multi threaded c program for readers writersproblem using pthreads this prog 4951735

Write a multi-threaded C program for readers/writersproblem using Pthreads. This program should adhere to the followingconstraints:
1. Multiple readers/writers must be supported (at least4 of each type);
2. Readers must read the shared variable multiple times(at least 4 times);
3. Writers must write the shared variable multiple times(at least 4 times);
4. Readers must print: a. The reader thread ID, b. The value of the shared variable, c. The number of readers present when value is read;
5. Writers must print: a. The writer thread ID, b. The written value to the shared variable, c. The number of readers present were when value is written. The print out can be implemented using fprintf(). A sample code canbe found in readerswriters.c. Before a reader/writer attempts toaccess the shared variable, it should wait some random amount oftime. This will help ensure that reads and writes do not occur allat once. For this purpose, you can use srandom() and usleep().
A sample_code() can be found below: #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.nAnd therandom number is %d.n”, 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.n”); } 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; } Attached

"Order a similar paper and get 15% discount on your first order with us
Use the following coupon
"GET15"

Order Now