The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
+15 votes

(a) Fill in the boxes below to get a solution for the reader-writer problem, using a single binary semaphore, mutex (initialized to 1) and busy waiting. Write the box numbers (1, 2 and 3), and their contents in your answer book.

int R = 0, W = 0;

Reader () {
    wait (mutex); 
    if (W == 0) {
        R = R + 1;
        ▭ ______________(1)
    else {
      ▭ ______________(2)
        goto L1;
    ..../* do the read*/
    wait (mutex);
    R = R - 1;
    signal (mutex);


Writer () {
    wait (mutex);
    if (▭) { _________ (3)
        signal (mutex);
        goto L2;
    signal (mutex);
    ...../*do the write*/
    wait( mutex);
    signal (mutex);


(b) Can the above solution lead to starvation of writers?

asked in Operating System by Veteran (59.4k points)
edited by | 1.2k views
for L1 if writer is 0, then more than one reader can enter into critical section

so (1) ........signal(mutex);

for unsuccessful operation it directly gives signals in else part


and it goes into loop

but I am not getting writer part. plz help

2 Answers

+15 votes
Best answer

My attempt :


 1. Give the opportunity for other Readers , so that they can read the C.S. ; Release the mutex ,i.e., Signal(mutex).
 2. Similarly to case (1),i.e., Signal(mutex).
 3. Check if any Reader(s) or Writer in C.S.( Critical Section) , if there any ; i.e. , R=>1 or W==1.

(b) If atleast one Reader is always present in critical section , then Writer can not be enter in critical section . Hence  readers-writers problem may starve writers in the queue.

answered by Loyal (5.9k points)
reshown by

@mithlesh , pls explain more in detail specially line 3 ...

goto label are starting of box naa??
ohk . thanks :)
 if (▭) { _____R>=1 or W==1____ (3)
        signal (mutex);
        goto L2;


Please explain this part in words


I can understand this.but plz xplain in terms of the code snippet,wats happening?


when multiple reader or one writer is present,then why goto L2 and and up on mutex which was previously made 0 by wait

For (3). If Many reader or Single writer is already doing some work with the file. Then no other  Writer is allowed to do write operation.

(3) is ( R || W). 

+9 votes
1. signal mutex
2. signal mutex
3. R ¦¦ W (binary OR operator)
answered by Veteran (54.7k points)

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

34,770 questions
41,730 answers
41,381 users