Computer Systems: A Programmer's Perspective (3rd Edition)
Computer Systems: A Programmer's Perspective (3rd Edition)
3rd Edition
ISBN: 9780134092669
Author: Bryant, Randal E. Bryant, David R. O'Hallaron, David R., Randal E.; O'Hallaron, Bryant/O'hallaron
Publisher: PEARSON
Question
Book Icon
Chapter 12, Problem 12.20HW
Program Plan Intro

Readers-Writers problem:

  • The readers-writers interactions would happen frequently in real systems.
  • It has some variations; each is centered on priority of writers and readers.
  • The details for “first readers-writers problem” is displayed below:
    • This problem favors readers.
    • It needs that no reader must be kept waiting lest a writer has already been granted approval to use object.
    • There should be no reader waiting due to waiting of writer.
  • The  details for  “second readers-writers problem” is displayed below:
    • This problem favors writers.
    • It requires that after a writer is set to write, it performs write as fast as possible.
    • A reader arriving after writer should wait, even if writer is also waiting.
  • The “w” semaphore controls access to critical sections that access shared object.
  • The “mutex” semaphore would protect admittance to shared variable “readcnt”.
  • It counts number of readers currently in critical section.
  • A writer locks “w” mutex each time it would enter critical section and unlocks it each time it leaves.
  • This guarantees that there exists at most one writer in critical section at any time point.
  • The first reader who enter critical section locks “w” and last reader to leave critical section unlocks it.
  • The “w” mutex is ignored by readers who enter and leave while other readers are present.
  • A correct solution to either of readers-writers problem could result in starvation.
  • A thread is been blocked indefinitely and is failed from making progress.

Expert Solution & Answer
Check Mark

Explanation of Solution

C code for readers-writers problem:

//Include libraries

#include <stdio.h>

#include "csapp.h"

//Define constants

#define WrteLmt 100000

#define Pple 20

#define N 5

//Declare variable

static int readtms;

//Declare variable

static int writetms;

//Declare semaphore variable

sem_t mtx;

//Declare semaphore variable

sem_t rdrcnt;

//Declare reader method

void *reader(void *vargp)

{

//Loop

while (1)

{

//P operation

P(&rdrcnt);

//P operation

P(&mtx);

//Increment variable

readtms++;

//V operation

V(&mtx);

//V operation

V(&rdrcnt);

}

}

//Declare writer method

void *writer(void *vargp)

{

//Loop

while (1)

{

//P operation

P(&mtx);

//Increment value

writetms++;

//If condition satisfies

if (writetms == WrteLmt)

{

//Display

printf("read/write: %d/%d\n", readtms, writetms);

//Exit

exit(0);

}

//V operation

V(&mtx);

}

}

//Declare init method

void init(void)

{

//Declare variables

readtms = 0;

//Declare variables

writetms = 0;

//Call method

Sem_init(&mtx, 0, 1);

//Call method

Sem_init(&rdrcnt, 0, N);

}

//Define main

int main(int argc, char* argv[])

{

//Declare variable

int li;

//Declare thread variable

pthread_t lTd;

//Call method

init();

//Loop

for (li = 0; li < Pple; li++)

{

//If condition satisfies

if (li%2 == 0)

//Call method

Pthread_create(&lTd, NULL, reader, NULL);

//If condition does not satisfies

else

//Call method

Pthread_create(&lTd, NULL, writer, NULL);

}

//Call method

Pthread_exit(NULL);

//Exit

exit(0);

}

Explanation:

  • The reader method decrements the reader count and semaphore initially.
  • The reading operation is performed after that.
  • The reader count and semaphore values are incremented after the operation.
  • The writer method decrements the semaphore variable initially.
  • The write operation is then performed.
  • If count reaches limit of writers, then display count.
  • The semaphore values are incremented after the operation.
Sample Output

read/write: 142746/100000

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
A manipulator is identified by the following table of parameters and variables:a. Obtain the transformation matrices between adjacent coordinate frames and calculate the global transformation matrix.
Which tool takes the 2 provided input datasets and produces the following output dataset? Input 1: Record First Last Output: 1 Enzo Cordova Record 2 Maggie Freelund Input 2: Record Frist Last MI ? First 1 Enzo Last MI Cordova [Null] 2 Maggie Freelund [Null] 3 Jason Wayans T. 4 Ruby Landry [Null] 1 Jason Wayans T. 5 Devonn Unger [Null] 2 Ruby Landry [Null] 6 Bradley Freelund [Null] 3 Devonn Unger [Null] 4 Bradley Freelund [Null] OA. Append Fields O B. Union OC. Join OD. Find Replace Clear selection
What are the similarities and differences between massively parallel processing systems and grid computing. with references
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
Operations Research : Applications and Algorithms
Computer Science
ISBN:9780534380588
Author:Wayne L. Winston
Publisher:Brooks Cole
Text book image
Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT