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.
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.
read/write: 142746/100000
Want to see more full solutions like this?
Chapter 12 Solutions
Computer Systems: A Programmer's Perspective (3rd Edition)
- Modular Program Structure. Analysis of Structured Programming Examples. Ways to Reduce Coupling. Based on the given problem, create an algorithm and a block diagram, and write the program code: Function: y=xsinx Interval: [0,π] Requirements: Create a graph of the function. Show the coordinates (x and y). Choose your own scale and show it in the block diagram. Create a block diagram based on the algorithm. Write the program code in Python. Requirements: Each step in the block diagram must be clearly shown. The graph of the function must be drawn and saved (in PNG format). Write the code in a modular way (functions and the main part should be separate). Please explain and describe the results in detail.arrow_forwardBased on the given problem, create an algorithm and a block diagram, and write the program code: Function: y=xsinx Interval: [0,π] Requirements: Create a graph of the function. Show the coordinates (x and y). Choose your own scale and show it in the block diagram. Create a block diagram based on the algorithm. Write the program code in Python. Requirements: Each step in the block diagram must be clearly shown. The graph of the function must be drawn and saved (in PNG format). Write the code in a modular way (functions and the main part should be separate). Please explain and describe the results in detail.arrow_forwardBased on the given problem, create an algorithm and a block diagram, and write the program code: Function: y=xsinx Interval: [0,π] Requirements: Create a graph of the function. Show the coordinates (x and y). Choose your own scale and show it in the block diagram. Create a block diagram based on the algorithm. Write the program code in Python. Requirements: Each step in the block diagram must be clearly shown. The graph of the function must be drawn and saved (in PNG format). Write the code in a modular way (functions and the main part should be separate). Please explain and describe the results in detail.arrow_forward
- Question: Based on the given problem, create an algorithm and a block diagram, and write the program code: Function: y=xsinx Interval: [0,π] Requirements: Create a graph of the function. Show the coordinates (x and y). Choose your own scale and show it in the block diagram. Create a block diagram based on the algorithm. Write the program code in Python. Requirements: Each step in the block diagram must be clearly shown. The graph of the function must be drawn and saved (in PNG format). Write the code in a modular way (functions and the main part should be separate). Please explain and describe the results in detail.arrow_forward23:12 Chegg content://org.teleg + 5G 5G 80% New question A feed of 60 mol% methanol in water at 1 atm is to be separated by dislation into a liquid distilate containing 98 mol% methanol and a bottom containing 96 mol% water. Enthalpy and equilibrium data for the mixture at 1 atm are given in Table Q2 below. Ask an expert (a) Devise a procedure, using the enthalpy-concentration diagram, to determine the minimum number of equilibrium trays for the condition of total reflux and the required separation. Show individual equilibrium trays using the the lines. Comment on why the value is Independent of the food condition. Recent My stuff Mol% MeOH, Saturated vapour Table Q2 Methanol-water vapour liquid equilibrium and enthalpy data for 1 atm Enthalpy above C˚C Equilibrium dala Mol% MeOH in Saturated liquid TC kJ mol T. "Chk kot) Liquid T, "C 0.0 100.0 48.195 100.0 7.536 0.0 0.0 100.0 5.0 90.9 47,730 928 7,141 2.0 13.4 96.4 Perks 10.0 97.7 47,311 87.7 8,862 4.0 23.0 93.5 16.0 96.2 46,892 84.4…arrow_forwardYou are working with a database table that contains customer data. The table includes columns about customer location such as city, state, and country. You want to retrieve the first 3 letters of each country name. You decide to use the SUBSTR function to retrieve the first 3 letters of each country name, and use the AS command to store the result in a new column called new_country. You write the SQL query below. Add a statement to your SQL query that will retrieve the first 3 letters of each country name and store the result in a new column as new_country.arrow_forward
- We are considering the RSA encryption scheme. The involved numbers are small, so the communication is insecure. Alice's public key (n,public_key) is (247,7). A code breaker manages to factories 247 = 13 x 19 Determine Alice's secret key. To solve the problem, you need not use the extended Euclid algorithm, but you may assume that her private key is one of the following numbers 31,35,55,59,77,89.arrow_forwardConsider the following Turing Machine (TM). Does the TM halt if it begins on the empty tape? If it halts, after how many steps? Does the TM halt if it begins on a tape that contains a single letter A followed by blanks? Justify your answer.arrow_forwardPllleasassseee ssiiirrrr soolveee thissssss questionnnnnnnarrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr
- Operations Research : Applications and AlgorithmsComputer ScienceISBN:9780534380588Author:Wayne L. WinstonPublisher:Brooks ColeSystems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage LearningEBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT