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&MOD MSGT/ET SA AC PKG
- Write a FancyCar class to support basic operations such as drive, add gas, honk horn, and start engine. FancyCar.java is provided with method stubs. Follow each step to gradually complete all methods. Note: This program is designed for incremental development. Complete each step and submit for grading before starting the next step. Only a portion of tests pass after each step but confirm progress. The main() method includes basic method calls. Add statements in main() as methods are completed to support development mode testing. Step 0. Declare private fields for miles driven as shown on the odometer (int), gallons of gas in tank (double), miles per gallon or MPG (double), driving capacity (double), and car model (String). Note the provided final variable indicates the gas tank capacity of 14.0 gallons. Step 1 (2 pts). 1) Complete the default constructor by initializing the odometer to five miles, tank is full of gas, miles per gallon is 24.0, and the model is "Old Clunker". 2)…arrow_forwardFind the error: daily_sales = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] days_of_week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] for i in range(7): daily_sales[i] = float(input('Enter the sales for ' \ + day_of_week[i] + ': ')arrow_forwardFind the error: daily_sales = [0.0, 0,0, 0.0, 0.0, 0.0, 0.0, 0.0] days_of_week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] for i in range(7): daily_sales[i] = float(input('Enter the sales for ' \ + days_of_week[i] + ': ')arrow_forward
- Find the error: daily_sales = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] days_of_week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] for i in range(6): daily_sales[i] = float(input('Enter the sales for ' \ + days_of_week[i] + ': '))arrow_forwardWhat are the steps you will follow in order to check the database and fix any problems with it and normalize it? Give two references with your answer.arrow_forwardWhat are the steps you will follow in order to check the database and fix any problems with it? Have in mind that you SHOULD normalize it as well. Consider that the database offline is not allowed since people are connected to it and personal data might be bridged and not secured. Provide three refernces with you answer.arrow_forward
- Should software manufacturers should be tolerant of the practice of software piracy in third-world countries to allow these countries an opportunity to move more quickly into the information age? Why or why not?arrow_forwardI would like to know about the features of Advanced Threat Protection (ATP), AMD-V, and domain name space (DNS).arrow_forwardPlease show the code for the Tikz figurearrow_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