Semaphores in Process Synchronization

17 Jun, 2022
Semaphore was proposed by Dijkstra in 1965 which is a very significant technique to manage concurrent processes by using a simple integer value, which is known as a semaphore. Semaphore is simply an integer variable that is shared between threads. This variable is used to solve the critical section problem and to achieve process synchronization in the multiprocessing environment. Semaphores are of two types:
Binary Semaphore – This is also known as mutex lock. It can have only two values – 0 and 1. Its value is initialized to 1. It is used to implement the solution of critical section problems with multiple processes.Counting Semaphore – Its value can range over an unrestricted domain. It is used to control access to a resource that has multiple instances.
Binary Semaphore – This is also known as mutex lock. It can have only two values – 0 and 1. Its value is initialized to 1. It is used to implement the solution of critical section problems with multiple processes.
Counting Semaphore – Its value can range over an unrestricted domain. It is used to control access to a resource that has multiple instances.
Now let us see how it does so.
First, look at two operations that can be used to access and change the value of the semaphore variable. 
Some point regarding P and V operation :
P operation is also called wait, sleep, or down operation, and V operation is also called signal, wake-up, or up operation.Both operations are atomic and semaphore(s) is always initialized to one. Here atomic means that variable on which read, modify and update happens at the same time/moment with no pre-emption i.e. in-between read, modify and update no other operation is performed that may change the variable.A critical section is surrounded by both operations to implement process synchronization. See the below image. The critical section of Process P is in between P and V operation. 
P operation is also called wait, sleep, or down operation, and V operation is also called signal, wake-up, or up operation.
Both operations are atomic and semaphore(s) is always initialized to one. Here atomic means that variable on which read, modify and update happens at the same time/moment with no pre-emption i.e. in-between read, modify and update no other operation is performed that may change the variable.
A critical section is surrounded by both operations to implement process synchronization. See the below image. The critical section of Process P is in between P and V operation. 
Now, let us see how it implements mutual exclusion. Let there be two processes P1 and P2 and a semaphore s is initialized as 1. Now if suppose P1 enters in its critical section then the value of semaphore s becomes 0. Now if P2 wants to enter its critical section then it will wait until s > 0, this can only happen when P1 finishes its critical section and calls V operation on semaphore s. 
This way mutual exclusion is achieved. Look at the below image for details which is Binary semaphore. 
Implementation of binary semaphores :
C++
struct semaphore {    enum value(0, 1);     // q contains all Process Control Blocks (PCBs)    // corresponding to processes got blocked    // while performing down operation.    Queue<process> q; } P(semaphore s){    if (s.value == 1) {        s.value = 0;    }    else {        // add the process to the waiting queue        q.push(P)        sleep();    }}V(Semaphore s){    if (s.q is empty) {        s.value = 1;    }    else {         // select a process from waiting queue        Process p=q.front();          // remove the process from wating as it has been sent for CS          q.pop();        wakeup(p);                      }}
The description above is for binary semaphore which can take only two values 0 and 1 and ensure mutual exclusion. There is one other type of semaphore called counting semaphore which can take values greater than one.
Now suppose there is a resource whose number of instances is 4. Now we initialize S = 4 and the rest is the same as for binary semaphore. Whenever the process wants that resource it calls P or waits for function and when it is done it calls V or signal function. If the value of S becomes zero then a process has to wait until S becomes positive. For example, Suppose there are 4 processes P1, P2, P3, P4, and they all call wait operation on S(initialized with 4). If another process P5 wants the resource then it should wait until one of the four processes calls the signal function and the value of semaphore becomes positive.
Limitations :
One of the biggest limitations of semaphore is priority inversion.Deadlock, suppose a process is trying to wake up another process which is not in a sleep state. Therefore, a deadlock may block indefinitely.The operating system has to keep track of all calls to wait and to signal the semaphore.
One of the biggest limitations of semaphore is priority inversion.
Deadlock, suppose a process is trying to wake up another process which is not in a sleep state. Therefore, a deadlock may block indefinitely.
The operating system has to keep track of all calls to wait and to signal the semaphore.
Problem in this implementation of semaphore :The main problem with semaphores is that they require busy waiting, If a process is in the critical section, then other processes trying to enter critical section will be waiting until the critical section is not occupied by any process.Whenever any process waits then it continuously checks for semaphore value (look at this line while (s==0); in P operation) and waste CPU cycle.
There is also a chance of “spinlock” as the processes keep on spins while waiting for the lock.
 To avoid this another implementation is provided below.
Implementation of counting semaphore : 
CPP
struct Semaphore {    int value;     // q contains all Process Control Blocks(PCBs)    // corresponding to processes got blocked    // while performing down operation.    Queue<process> q; } P(Semaphore s){    s.value = s.value - 1;    if (s.value < 0) {         // add process to queue        // here p is a process which is currently executing        q.push(p);        block();    }    else        return;} V(Semaphore s){    s.value = s.value + 1;    if (s.value >= 0) {         // remove process p from queue        Process p=q.pop();        wakeup(p);    }    else        return;}
In this implementation whenever the process waits it is added to a waiting queue of processes associated with that semaphore. This is done through system call block() on that process. When a process is completed it calls the signal function and one process in the queue is resumed. It uses wakeup() system call.
Sam_Sparrow
HarshalChavan
Hasanul Islam
soumya7
tushargupta09
ap173777
secret_giggle
abhishekpal97854368
ankithreddy2001
bengerisanjeev
phoniex
ganeshdhumal2020
geeky01adarsh
GATE CS
Operating Systems
Operating Systems
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.