Question

In: Computer Science

The typical solution of the readers-writers problem works for the following assumptions: (a) Only one writer...

The typical solution of the readers-writers problem works for the following assumptions: (a) Only one writer at a time can access the buffer. Using semaphores, write psuedo code for the following two variations of the reader-writer problem. In each case, provide a short explanation of why there is no deadlock and no starvation in your new code.

1) exactly one writer and one reader exist

Writer:

Reader:

2) exactly two writers and two readers exist, where writers have priority

Writer:

Reader:

Solutions

Expert Solution

In the readers-writers problem, we use three semaphore variables:

  • Semaphore S: This semaphore variable is used to achieve mutual exclusion between processes. By using this variable, either readers or writers will be allowed to use or access the shared buffer at a particular time. This variable is set to 1 initially.
  • Semaphore E: This semaphore variable is used to define the empty space in the buffer. Initially, it is set to the whole space of the buffer i.e. "n" because the buffer is initially empty.
  • Semaphore F: This semaphore variable is used to define the space that is filled by the readers. Initially, it is set to "0" because there is no space filled by the readers initially.


By using the above three semaphore variables and by using the wait() and signal() function, we can solve our problem(the wait() function decreases the semaphore variable by 1 and the signal() function increases the semaphore variable by 1). So. let's see how.

The following is the pseudo-code for the readers:

void readers() {
while(T) {
produce()
wait(E)
wait(S)
append()
signal(S)
signal(F)
}
}
The above code can be summarized as:

  • while() is used to produce data, again and again, if it wishes to produce, again and again.
  • produce() function is called to produce data by the readers.
  • wait(E) will reduce the value of the semaphore variable "E" by one i.e. when the readers produces something then there is a decrease in the value of the empty space in the buffer. If the buffer is full i.e. the vale of the semaphore variable "E" is "0", then the program will stop its execution and no production will be done.
  • wait(S) is used to set the semaphore variable "S" to "0" so that no other process can enter into the critical section.
  • append() function is used to append the newly produced data in the buffer.
  • signal(s) is used to set the semaphore variable "S" to "1" so that other processes can come into the critical section now because the production is done and the append operation is also done.
  • signal(F) is used to increase the semaphore variable "F" by one because after adding the data into the buffer, one space is filled in the buffer and the variable "F" must be updated.

This is how we solve the produce part of the readers-writers problem. Now, let's see the writers solution. The following is the code for the writers:

void writers() {
while(T) {
wait(F)
wait(S)
take()
signal(S)
signal(E)
use()
}
}
The above code can be summarized as:

  • while() is used to consume data, again and again, if it wishes to consume, again and again.
  • wait(F) is used to decrease the semaphore variable "F" by one because if some data is consumed by the writers then the variable "F" must be decreased by one.
  • wait(S) is used to set the semaphore variable "S" to "0" so that no other process can enter into the critical section.
  • take() function is used to take the data from the buffer by the writers.
  • signal(S) is used to set the semaphore variable "S" to "1" so that other processes can come into the critical section now because the consumption is done and the take operation is also done.
  • signal(E) is used to increase the semaphore variable "E" by one because after taking the data from the buffer, one space is freed from the buffer and the variable "E" must be increased.
  • use() is a function that is used to use the data taken from the buffer by the process to do some operation.

Related Solutions

The typical solution of the readers-writers problem works for the following assumptions: Only one writer at...
The typical solution of the readers-writers problem works for the following assumptions: Only one writer at a time can access the buffer. Write two separate solutions (pseudo code) for the reader and the writer processes in the cases in which 1) exactly one writer and one reader exist 2) exactly two writers and two readers exist, where writers have priority In each case, provide a short explanation of why there is no deadlock and no starvation in your new code.
The typical solution of the readers-writers problem works for the following assumptions: (a) Only one writer...
The typical solution of the readers-writers problem works for the following assumptions: (a) Only one writer at a time can access the buffer. Write two separate solutions (pseudo code) for the reader and the writer processes in the cases in which 1) exactly one writer and one reader exist 2) exactly two writers and two readers exist Note: writers have priority In each case. Also, provide a short explanation (about 2 lines) of why there is no deadlock and no...
one-writer many-readers problem
Explain what is the problem with this implementation of the one-writer many-readers problem?
Adopt one (and only one) of the following assumptions and write a 150 word (or less)...
Adopt one (and only one) of the following assumptions and write a 150 word (or less) letter to the editor of a newspaper explaining why you either: 1. Oppose globalization 2. Are in favour of globalization (Choose only one position)
Suppose the price of one typical stock could only increase by 2 or decrease by 1...
Suppose the price of one typical stock could only increase by 2 or decrease by 1 in one day. From the historical data, we somehow know that this stock goes up with probability 0.7, goes down with probability 0.3. Suppose the initial price is 100. Suppose we want to study the price behavior for that stock for one week(5-weekdays). (Round your answer in 3 decimal Places) This question is just for setting up the model. what is the probability of...
For this project, following assumptions have been made:   The booking is only open for the next...
For this project, following assumptions have been made:   The booking is only open for the next seven days from the current date. Only two categories of the plane can be booked i.e. Economy and Business. The total number of tickets that can be booked in each category is 10 only. By ‘user’, we mean the person who is booking the ticket for the passenger(s). For example, an employee of Saudi Airline. Your project should include information about the following entities...
The basic assumptions is that the company has only one product. How can we use CVP...
The basic assumptions is that the company has only one product. How can we use CVP analysis for a multi-product company? Another basic assumption is that all costs can be classified as either fixed or variable. How can we adapt CVP analysis for situations that have step fixed or curve-linear variable costs?
Instructions: Solve the following problem. To receive credit, readers must be able to follow your logic...
Instructions: Solve the following problem. To receive credit, readers must be able to follow your logic and you must explain each step in detail. Please label each step, identify each equation and variable, and explain your answer. The management of a mutual fund plans to sell a basket of stocks in three months. The stocks are similar to the S&P 500. It seeks protection against a decrease in the price of the stocks. The current price of the S&P 500...
Which one of the following business strategies, used by Direct Writer insurers, involves an organization selling...
Which one of the following business strategies, used by Direct Writer insurers, involves an organization selling its product directly to the customer rather than through a wholesaler or other intermediary? Select one: a. Multi-divisional structure b. Differentiation c. Cost leadership d. Vertical integration
Describe in few sentences how a blood glucometer or a ELISA assay works (describe only one...
Describe in few sentences how a blood glucometer or a ELISA assay works (describe only one test) In your answer please include the analytical technique used, the analytical signal used for the detection and a simple description of the analytical method.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT