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
Correct answer will be upvoted else downvoted. Computer science.    You are given a grid a comprising of positive integers. It has n lines and m segments.    Develop a framework b comprising of positive integers. It ought to have a similar size as a, and the accompanying conditions ought to be met:    1≤bi,j≤106;    bi,j is a various of ai,j;    the outright worth of the contrast between numbers in any nearby pair of cells (two cells that share a similar side) in b is equivalent to k4 for some integer k≥1 (k isn't really something similar for all sets, it is own for each pair).    We can show that the appropriate response consistently exists.    Input    The primary line contains two integers n and m (2≤n,m≤500).    Every one of the accompanying n lines contains m integers. The j-th integer in the I-th line is ai,j (1≤ai,j≤16).    Output    The output ought to contain n lines each containing m integers. The j-th integer in the I-th line ought to be bi,j.
The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the resulting value contains two or more digits, those digits are summed and the process is repeated. This is continued as long as necessary to obtain a single digit. For example, consider the positive integer 24. Adding the 2 and the 4 yields a value of 6. Since 6 is a single digit, 6 is the digital root of 24. Now consider the positive integer 39. Adding the 3 and the 9 yields 12. Since 12 is not a single digit, the process must be repeated. Adding the 1 and the 2 yields 3, a single digit and also the digital root of 39. Input File: The input file will contain a list of positive integers, one per line. The end of the input will be indicated by an integer value of zero. Output File: For each integer in the input, output its digital root on a separate line of the output. Example Input: 24 39…
vvvHarry has a big wall clock, that got hit while he was playing. Now, the minute hand doesn't rotate by the angle 2π/3600 each second, but now it moves according to different angle x. You can assume that coordinates of the centre of the clock are (0, 0) and the length of the minute hand is l. One endpoint of the minute hand is always located at the clock centre; the other endpoint is initially located at the point (0, l). One second later, Harry observes that this endpoint is at distance d above the x-axis, i.e., the y-coordinate of this endpoint is equal to d. Harry is curious about where the minute hand will be (specifically, its y-coordinate) after t seconds. Because t can be very large, Harry can't wait for that moment. Please help him to write a python code that prints a single line containing the output.Input: 4 2 2Output4Harry has a big wall clock, that got hit while he was playing. Now, the minute hand doesn't rotate by the angle 2π/3600 each second, but now it moves according…
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education