562 views
1 votes
1 votes
In the standard code for UP operation,

UP (S)

{

       if(Waiting queue L is empty)   

              S = 1;

      else

               Select a process from waiting queue and wake up();

}

( S is the mutex)

Here, why S=1 is not set in the else part as well?

For eg: if S=0 and many processes are already waiting in the queue, then one other process can still do UP(), make S=1 and enter the critical section right?

ie, shouldn't the "else' part be:

else

{

                S = 1;

               Select a process from waiting queue and wake up();

}

 

Or, is it mandatory that a process can enter the critical section only after letting in, all the processes the got blocked before it?

1 Answer

1 votes
1 votes

"Here, why S=1 is not set in the else part as well?" 
You have taken example of Binary Semaphore, S=1 is not set in else part to maintain Mutual Exclusion. Two process can go in CS together because one process is woke up and another process can go because it finds S=1. 

Or, is it mandatory that a process can enter the critical section only after letting in, all the processes the got blocked before it?
Yes!
until block queue of a particular binary semaphore becomes empty, S can't be set to 1.
When there are many processes blocked in the queue of a semaphore (each semaphore variable has its own block queue), process wake up means its PCB has been placed in ready queue, whether it will be executed immediately or later it depends on the CPU schedulig.

edited

Related questions

547
views
1 answers
2 votes
subhashchaganti asked Sep 21, 2022
547 views
Say there is a Binary semaphore R, initialized to 0. Say process X and Y are working on R.At T1 :- X has performed P(R); X gets blocked and gets added ... queue?Which process will run after Y has performed V(R)? Process Y or Process X?
561
views
1 answers
1 votes
eyeamgj asked Sep 19, 2018
561 views
https://gateoverflow.in/37083/can-anyone-explain-the-below-question-in-a-detailed-wayanswer 0or 1??
1.3k
views
1 answers
2 votes
Warlock lord asked Jan 7, 2018
1,305 views
Say I have a Binary Semaphore S initialized to 1. Consider V() as Signal and P() as Wait. If I now invoke V(S), will it wait for S to become 0? Or will it go on with further execution without making any changes to S?
1.0k
views
2 answers
1 votes
sumit chakraborty asked Nov 26, 2017
1,044 views
Given Processes P1: Wait(Sx); Wait(Sy); Do something;Signal(Sx); Signal(Sy); and Process P2: Wait(Sx); Wait(Sy); Do something;Signal(Sy); Signal(Sx); . Will it ever lead to starvation ?