Semaphore in Operating System (OS) (What is)
Semaphore in Operating System
Semaphore is also an entity devised by Edsger W. Dijkstra, to solve Process Synchronization problem in OS. Its most popular use is it solve Critical Section algorithm.
It uses signalling mechanism to allow access to shared resource namely by two –
What is Semaphore in Operating System
There are two types of Semaphores –
- Binary Semaphore – Only True/False or 0/1 values
- Counting Semaphore – Non-negative value
Semaphore can have two different operations which are wait and signal. In some books wait signals are also denoted by P(s) and signal by V(s). Where s is a common semaphore.
Wait p(s) or wait(s)
- Wait decrements the value of semaphore by 1
Signal v(s) or signal(s)
- Signal increments the value of semaphore by 1
- Semaphore can only have non negative values
- Before start of program it is always initialised to 1
Code Logic for Incrementing – Decrementing value of Semaphore
The code that needs to be protected can be surrounded by wait and signal operation as follows –
For Binary Semaphore
Let us try to understand the above code with an example –
- Imagine that there are two processes A and B.
- At the beginning the value of semaphore is initialised as 1.
- Imagine process A wants to enter the critical section
- Before it can do that it checks the value of semaphore which is 1 thus, it can enter the CS and semaphore value is turned to 0
- Now imagine that process B wants to enter too
- It checks the semaphore value which is 0 thus it can’t enter and waits until the value is non zero – non negative value
- Now, Process A finishes and signals semaphore which in turns changes semaphore value to 1
- Thus, now process B can enter Critical section
In this way mutual exclusion was achieved.
For Counting Semaphore
For Counting Semaphore we initialise the value of semaphore as the number of concurrent access of critical sections we want to allow.
For example Let us assume that the value is 3.
- Process 1 enters Critical section and semaphore value is changed to 2
- Process 2 also enters critical section and semaphore value is changed to 1
- Process 2 signals semaphore and comes out of critical section and Semaphore value is 2
- Note at this moment only 1 process that is process 1 is in critical section
- Process 3 and 4 also enter critical section simultaneously and semaphore value is 0
- At this moment there are three processes in Critical section which are process Process 1, 3, 4
- Now imagine that process 5 wants to enter the CS. It would not be able to enter as semaphore value is 0
- It can only enter once any of the process 1, 3, 4 signals out of the critical section.
Thus value of semaphore in counting semaphore is equal to number of simultaneous processes allowed to access the critical section.
Monitors are a synchronisation construct that were created to overcome the problems caused by semaphores such as timing errors.
Semaphores can also be used but may cause timing errors. Mutex can never be used for process synchronisation
Mutex can not be used for the purpose of Process Synchronization
- Process Synchronization
- Critical Section
- Inter-Process Communication
- UEFI(Unified Extensible Firmware Interface) and how is it different from BIOS
- Mutex vs. Semaphore
- Atomic Operations in OS
- Peterson’s Algorithm for Mutual Exclusion (Only important for Cisco and Arista Networs)
- Peterson’s Algorithm for Critical Section Problem (Only important for Cisco and Arista Networs)
- Readers-Writers Problem