Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Semaphore wait() and signal()

I am going through process synchronization, and facing difficulty in understanding semaphore. So here is my doubt:

the source says that

" Semaphore S is an integer variable that is accessed through standard atomic operations i.e. wait() and signal().

It also provided basic definition of wait()

wait(Semaphore S)
{
   while S<=0
     ; //no operation
   S--;
}

Definition of signal()

signal(S)
{
   S++;
}

Let the initial value of a semaphore be 1, and say there are two concurrent processes P0 and P1 which are not supposed to perform operations of their critical section simultaneously.

Now say P0 is in its critical section, so the Semaphore S must have value 0, now say P1 wants to enter its critical section so it executes wait(), and in wait() it continuously loops, now to exit from the loop the semaphore value must be incremented, but it may not be possible because according the source, wait() is an atomic operation and can't be interrupted and thus the process P0 can't call signal() in a single processor system.

I want to know, is the understanding i have so far is correct or not. and if correct then how come process P0 call signal() when process P1 is strucked in while loop?

like image 894
Akashdeep Saluja Avatar asked Nov 23 '12 12:11

Akashdeep Saluja


People also ask

Why must the semaphore methods wait () and signal () be atomic operations?

Operation wait() and signal() must be completely atomic; no two processes can execute wait() or signal() operation simultaneously because they are implemented in kernel and processes in kernel mode can not be preempted.

What is the function of semaphore signal?

Railway semaphore signal is one of the earliest forms of fixed railway signals. This semaphore system involves signals that display their different indications to train drivers by changing the angle of inclination of a pivoted 'arm'.

Is semaphore wait Atomic?

Semaphore is simply a variable that is non-negative and shared between threads. A semaphore is a signaling mechanism, and a thread that is waiting on a semaphore can be signaled by another thread. It uses two atomic operations, 1) Wait, and 2) Signal for the process synchronization.


2 Answers

I think it's an inaccuracy in your source. Atomic for the wait() operation means each iteration of it is atomic, meaning S-- is performed without interruption, but the whole operation is interruptible after each completion of S-- inside the while loop.

like image 185
SomeWittyUsername Avatar answered Oct 08 '22 07:10

SomeWittyUsername


I think the top-voted answer is inaccurate!

Operation wait() and signal() must be completely atomic; no two processes can execute wait() or signal() operation simultaneously because they are implemented in kernel and processes in kernel mode can not be preempted.

If several processes attempt a P(S) simultaneously, only one process will be allowed to proceed(non-preemptive kernel that is free of race condition).

for the above implementation to work preemption is necessary (preemptive kernel)

read about the atomicity of semaphore operations http://personal.kent.edu/~rmuhamma/OpSystems/Myos/semaphore.htm https://en.wikibooks.org/wiki/Operating_System_Design/Processes/Semaphores

like image 31
G_kuldeep Avatar answered Oct 08 '22 06:10

G_kuldeep