struct Semaphore
{
enum value(0,1);
Queue type L;
}
Down (Semaphore S)
{
if(S.value==1)
{
S.value=0;
}
else
{
put process(PCB) in S.L;
sleep();
}
}
Up(Semaphore S)
{
if(S.L is empty)
{
S.value=1;
}
else
{
select a process from S.L;
wakeup();
}
}
I saw this implementation of Binary Semaphore. Is this right?
Consider two processes P0, P1.
P0 performs down first and enters CS. S.value = 0 now.
P1 tries to perform down now and goes to sleep.
P0 comes out and exeutes the else part of Up and wakes P1 up. Now how will P1 enter the CS, since S.value is still 0?