Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In order to solve the Readers-Writers problem and avoid writers starvation, you can design a program with an additional condition: readers must wait if there

In order to solve the Readers-Writers problem and avoid writers starvation, you can design a program with an additional condition:readers must wait if there is at least one writer waiting: Modify the reader_writers.c program as follows:

#include

#include

#include //compile and link with -pthread

const int NUM_READERS_WRITERS = 10;

pthread_cond_t cond; //condition

pthread_mutex_t mutex; //lock, a.k.a. mutex

int activeWritersNumber;

int activeReadersNumber;

int waitingWritersNumber = 0;

void *read( void *ptr ) {

int tid = (int) pthread_self();

int value;

while(1){

printf("\tReader %d wants to read! ", tid);

pthread_mutex_lock(&mutex);

while(activeWritersNumber > 0 || waitingWritersNumber > 0 ){

printf("\tReader %d is waiting... ", tid);

pthread_cond_wait(&cond, &mutex);

}

++activeReadersNumber;

pthread_mutex_unlock(&mutex);

printf("\tReader %d is reading! ", tid);

printf("Number of active readers: %d, number of active writers: %d, number of waiting writers: %d ", activeReadersNumber, activeWritersNumber, waitingWritersNumber );

usleep(rand() % 1000);

pthread_mutex_lock(&mutex);

--activeReadersNumber;

printf("\tReader %d has finished reading. ", tid);

printf("Number of active readers: %d, number of active writers: %d, number of waiting writers: %d ", activeReadersNumber, activeWritersNumber, waitingWritersNumber );

if(activeReadersNumber == 0){

pthread_cond_signal(&cond);

printf("\tReader %d was the last reader. Notifying one waiting writer.", tid);

}

pthread_mutex_unlock(&mutex);

usleep(rand() % 1000);

}

}

void *write( void *ptr ) {

int tid = (int) pthread_self();

int value;

while(1){

printf("\t\t\tWriter %d wants to write! ", tid);

pthread_mutex_lock(&mutex);

while(activeReadersNumber > 0 || activeWritersNumber > 0 ){

++waitingWritersNumber;

printf("\t\t\tWriter %d is waiting... ", tid);

pthread_cond_wait(&cond, &mutex);

--waitingWritersNumber;

}

++activeWritersNumber;

pthread_mutex_unlock(&mutex);

printf("\t\t\tWriter %d is writing! ", tid);

printf("Number of active readers: %d, number of active writers: %d, number of waiting writers: %d ", activeReadersNumber, activeWritersNumber, waitingWritersNumber );

usleep(rand() % 1000);

pthread_mutex_lock(&mutex);

--activeWritersNumber;

printf("\t\t\tWriter %d has finished writing. Notifying waiting readers and writers. ",tid);

printf("Number of active readers: %d, number of active writers: %d, number of waiting writers: %d ", activeReadersNumber, activeWritersNumber, waitingWritersNumber );

pthread_cond_signal(&cond);

pthread_mutex_unlock(&mutex);

usleep(rand() % 1000); }

}

int main() {

pthread_t readers[NUM_READERS_WRITERS];

pthread_t writers[NUM_READERS_WRITERS];

activeWritersNumber = 0;

activeReadersNumber = 0;

pthread_mutex_init(&mutex, NULL);

pthread_cond_init(&cond, NULL);

srand(time(NULL));

int i;

for(i = 0; i < NUM_READERS_WRITERS; i++){

pthread_create( &readers[i], NULL, read, NULL);

pthread_create( &writers[i], NULL, write, NULL);

}

for(i = 0; i < NUM_READERS_WRITERS; i++){

pthread_join( readers[i], NULL);

pthread_join( writers[i], NULL);

}

exit(0);

}

Compile reader_writers.c again:

student@Ubuntu:~/labs/lab3$ gcc -pthread reader_writers.c -o rw

Run the reader_writers program for a few seconds. Then press <Ctrl+ C> to stop the program:

student@Ubuntu:~ /labs/lab3 $ ./rw

Copy and paste the output below. Check the output. Any new problem noticeable? (Hint: starvation is a problem encountered where a process is perpetually denied necessary resources.)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Accounting And Auditing Research And Databases Practitioner's Desk Reference

Authors: Thomas R. Weirich, Natalie Tatiana Churyk, Thomas C. Pearson

1st Edition

1118334426, 978-1118334423

More Books

Students also viewed these Databases questions

Question

Define the term Working Capital Gap.

Answered: 1 week ago

Question

What are the Five Phases of SDLC? Explain each briefly.

Answered: 1 week ago

Question

How can Change Control Procedures manage Project Creep?

Answered: 1 week ago