2,580 views
5 votes
5 votes

these are the codes for down and up operations in a binary semaphore. The down operation's code seems to be correct, but I am having some doubt in the UP's code.
Suppose a process p1 arrives and executes down. the initial value of "value" is 1. now p1 will decrement it to 0 ,  and then proceed to execute CS. Suppose, during this time another process p2 arrives and executes down. now it will be forced to sleep as value==0. Lets assume the same for another arriving process p3.
 Now if P1 finishes its CS, it will execute UP, and find that L is not empty, so it will select a process from it and wake it up. BUT, p1 has not changed the value of "value". So even if p2 wakes up and executes down, it will be forced to sleep again.. Am I missing something, or is the above implementation incorrect ?

3 Answers

6 votes
6 votes

Implementation is correct

when  process p2 goes to sleep() then it is placed in the suspended List   - At this point it is executing the last statement of the down operation.

so, when it will wake up , it will resume from this point and go to critical section directly without re-executing down operation.

edited by
2 votes
2 votes

Whenever Binary Semaphores are used to synchronize the Critical Section by a Binary Semaphore Variable lets take it "S".Generally the code structure will be like below

P(s) // Entry Section

Critical Section

V(s) // Exit Section

we do down operation to achieve mutual exclusion i.e once a process its entered into the critical section and executing it, meanwhile no other process is allowed to execute the critical section and more on it will be suspended and it's PCB will be kept into the suspended list of semaphore variable i.e. S.

After execution of critical section the process will perform up operation on S inside exit section and binary semaphore does not always make S value to 1. It makes the S.value = 1 once the S.list is empty and it will simply wake up a process from the S.list and allocate the critical section to it when S.list is not empty.

Now your confusion is, "Why UP() operation is not performing S.value = 1 when S.list is not empty ?"

Lets do it first, once we will make S.value  =1 when S.list is not empty. There will be chance of process P2 may enter into starvation.Because once the S.value is 1 any process can come and perform down operation and execute critical section. This the process P2 may not get chance to execute the critical section.

edited by
0 votes
0 votes
Semaphore implementation contains Queue. While performing the DOWN() operation if it is unsuccessful then process goes and sleep into this queue. DOWN() fail because of the some other process is in CS. While performing the UP() operation it will check the list is empty or not. If list is empty then it simply assign the vale 1 to semaphore. If list is not empty then it will wake the process from the list and allow that process to enter into CS. So UP() just wake the process from the list and allowed to enter that process into CS. Hope this is clear.

Related questions

0 votes
0 votes
4 answers
2
piyushkr asked Jan 21, 2016
1,585 views
Up(Semaphore S) { if (Suspended list() is empty) S.value=1; else { Select a process from the suspended list and WakeUp() } }
3 votes
3 votes
3 answers
3
GateAspirant999 asked Feb 14, 2017
2,535 views
Below are four concurrent processes and three counting semaphores.What must be the initial values of the three semaphores, so that output “TGE” is obtained?
1 votes
1 votes
1 answer
4
Rahul_Rathod_ asked Dec 12, 2018
1,569 views
a) s1-wait(p) , s2-wait(q) , s3-wait(q) , s4-wait(p)b) s1-wait(p) , s2-wait(q) , s3-wait(p) , s4-wait(q)c) s1-wait(q) , s2-wait(p) , s3-wait(p) , s4-wait(q)d) none of...