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

(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.

L1:
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);
}

 

L2: 
Writer () {
    wait (mutex);
    if (▭) { _________ (3)
        signal (mutex);
        goto L2;
    }
    W=1;
    signal (mutex);
    ...../*do the write*/
    wait( mutex);
    W=0;
    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
+1
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

(2)............signal(mutex)

and it goes into loop

but I am not getting writer part. plz help

2 Answers

+15 votes
Best answer

My attempt :

(a)

 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
0

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

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

@Mithilesh

Please explain this part in words

 

0
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
0

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
118,876 comments
41,381 users