0

The answer my book says is x.

But how is that possible? I just learnt from Differnce between Counting and Binary Semaphores that counting semaphores have positive value so that multiple process can access the critical section. So, in that case - how can one say that x processes are waiting, because on reaching 0, the next wait signal will busy-wait a process and the semaphore value can never be less than 0.

Now, there I think can be a second scenario. Like the counting semaphore is initialized to 1. Now, when a process access it, it becomes 0. Next in the wait if we write,

while(s <= 0);

then the next process will make it -1. So, a single process waiting makes the semaphore value -1.

Therefore I can conclude, that for -x, x processes are busy-waiting!

Can someone clarify if I'm right or wrong? Any help is appreciated. Thanks in advance.

Community
  • 1
  • 1
lu5er
  • 3,229
  • 2
  • 29
  • 50

1 Answers1

5

The counting semaphore is implemented as below:

    struct semaphore{
           int value;
           Queue L;
           }


Here the variable "value" can take positive , negative or '0' as value depending on its initial value and the number of processes tried accessing it. The initial value of variable "value" tells the number of processes that can access it simultaneously.

The wait() method is implemented as :

    wait(semaphore s){
           s.value--;
           if(s.value < 0){
               put the process in the queue s.L;
               sleep();
               }

So when a process tries to access the semaphore and if the value becomes less than '0', it will go to sleep as no more permission is there to access the resource.
Hence, the number of times processes try to access the semaphore, those many times its value will be decremented, once value goes to negative, the processes will wait in the waiting queue until woken up by a signal() method, making the absolute value equal to the number of processes tried accessing it unsuccessfully.

Akash Mahapatra
  • 2,988
  • 1
  • 14
  • 28