Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"S->value <= 0" signal() implementation in semaphore with no busy waiting

following is the code of signal() operation in semaphore with no busy waiting (without busy waiting)

Implementation of signal():

signal (semaphore *S) {
    S->value++; 
    if (S->value <= 0) { 
        remove a process P from S->list; 
        wakeup(P);  
    } 
}

and i have a question with "if-condition".

i think, the fact that S->value is zero or negative means that there is no available resource, so wakeup() should not be permitted. but as you can see, whenever signal() operation is called, a process (which is in waiting list) is being woken regardless of the state of S->value.

so in my opinion, a sign of inequality S->value >= 0 is natural and makes sense, because S->value > 0 means there are available resoures.

is there anybody who can explain to me in easy english?

like image 703
3 revs Avatar asked Sep 28 '22 15:09

3 revs


1 Answers

You are confusing between Semaphore's queue and the ready queue here. This solution is to meet the bounded waiting condition.

When a process must wait for a semaphore S, it is blocked and put on the semaphore’s queue. signal() removes one process from the queue and moves it to Ready queue. Because signal() is always called from a process which has just finished its critical section. So there will be a new process added to the ready queue.

If you add a process to the ready queue when semaphore's value is positive it doesn't make sense because you can use the semaphore directly then and there, without joining the ready queue.

    signal (semaphore *S) {
        S->value++; 
        if (S->value <= 0) { 
        remove a process P from S->list; 
        wakeup(P);  //move one process P from S->list to ready list
        } 
    }

The negative value indicates a number of processes waiting. Hope this helps.

like image 91
Sandeep Ipk Avatar answered Oct 05 '22 23:10

Sandeep Ipk