Semaphore in OS is an integer value that indicates whether the resource required by the process is available or not. The value of a semaphore is modified by wait() or signal() operation where the wait() operation decrements the value of semaphore and the signal() operation increments the value of the semaphore.
The wait() operation is performed when the process wants to access the resources and the signal() operation is performed when the process want to release the resources. The semaphore can be binary semaphore or the counting semaphore. In this section, we will discuss the concept of semaphore in brief along with the types, advantages and disadvantages.
Content: Semaphore in OS
- What is Semaphore?
- Implementation of Semaphores
- Types of Semaphores
- Advantages and Disadvantages
- Key Takeaways
What is Semaphore in OS?
Semaphore is a process synchronization tool that prevents race condition that may occur when multiple cooperative processes try to access the same resources. Two or more process can synchronise by means of the signal. This means the process, trying to access the same shared resource can be forced to stop at a specific place until it is signalled to access the resource.
Now you will be thinking how the semaphore signals the process in order to achieve synchronization?
Actually, the semaphore is an integer variable whose value signals that if the process must access the resource or wait or wake up to access the shared resources. Apart from the initialization, the semaphore value can be modified using two operations wait() and signal().
The wait() operation decrements the value of semaphore and if the semaphore value becomes negative then the process executing wait() would be blocked. The signal() operation increments the value of semaphore and if the semaphore value is less than or equal to 0 then a process blocked by wait() is unblocked.
The wait() and signal() operation must be performed indivisibly i.e. if a process is modifying the semaphore value no other process must simultaneously modify the semaphore value. Along with that the execution of wait() and signal() must not be interrupted in between.
You can also observe the P(), down() as a synonym to wait() operation. Now, let us see the working of wait() operation.
Wait(Semaphore S) { S.vlaue= S.value -1; If (S.value≤0) { block the process Sleep(); } else return; }
As a synonym to signal(), v(), up(), post() and release(). Now let us see the working of signal() operation.
Signal(Semaphore S) { S.value= S.value + 1; If(S.value≤0) { Unblock the process wake up(); } }
Implementation of Semaphore
Any processes that want to access the shared resources have to first execute the entry section where the wait() operation decrements the value of the semaphore. If the value of the semaphore is 0 that means all the resources are in use and now onwards the processes trying to execute the entry section would be blocked.
Now when a process wants to release the resource then it has to execute the exit code before releasing the resource. Executing the exit code the process increments the value of semaphore with the signal() operation, unblocks a blocked process and thereby release the resource.
The unblocked process can now try to execute the entry section code to access the resource. There is always a waiting queue or blocked queue linked with each semaphore. The processes are unblocked or removed from the waiting queue in the First-in-First-out manner. That means the processes that have been waiting for a long is released first.
Types of Semaphores
The operating system classifies semaphores into two types as discussed below:
1. Counting Semaphore
The counting semaphores controls access to resources that have finite instances. Therefore the value of counting semaphore is not restricted to a certain domain.
The counting semaphore is initialized with a value equivalent to the number of resources available. When a process wants to access the resource it executes the entry section code where the wait() operation decrements the value of semaphore and allot the resource to the process.
When the counting semaphore value decreases to 0 it means no resources are available now and the process that executes the entry section code further would be blocked. Now when a process releases a resource it executes its exit section code where the signal() operation would increment the value of counting semaphore and release a blocked process which can further try to execute the entry section code.
2. Binary Semaphores
Binary semaphores are generally used to access the critical section. As we know that only one process can enter a critical section at a time therefore the value of binary semaphores ranges over 0 to 1.
Consider that the binary semaphore value is initialized to 1. Now if a process P1 want to enter the critical section then it executes the entry section code where the wait() operation decrements the semaphore value to 0 and enters the critical section. Now when second process P2 tries to enter the critical section then it executes the entry section code where the wait() operation verifies the binary semaphore value is already 0 which means no process further can enter the critical section and blocks the second process P2.
When process P1 leaves the critical section it executes the signal() operation of exit section which increments the semaphore value to 1 and releases a blocked process P2 which can further try to execute entry section code to enter the critical section.
Advantages and Disadvantages
Advantages of Semaphore
- Semaphore resolves the process synchronization issues.
- Waiting list associated with each semaphore avoids busy waiting and lets CPU perform other productively.
Disadvantages of Semaphore
- Implementing semaphore can lead to priority inversion where the two processes get into spinlock condition.
- If not implemented properly then semaphore can cause deadlock condition.
- Semaphore requires busy waiting and it wastes CPU cycles.
Key Takeaways
- Semaphore is an integer variable whose value indicates whether the resources required by the processes is available or not.
- The semaphore value can be modified by the wait() and signal() operation, where the wait() operation occurs in the entry section of resources and signal() operation occurs in the exit section of the resources.
- If the resource required by the process is available then the wait() operation decrements the semaphore value and let the process access the resource.
- If the resource required by the process is not available then the wait() operation decrements the value of semaphore and block the process.
- If a process releases the resource then the signal() operation would increment the value of semaphore and unblocks a process in the waiting list.
- The semaphore is of two types binary semaphore and counting semaphore.
- Binary semaphore ranges over 0 to 1 and counting semaphore ranges over -∞ to +∞.
So this is all about the semaphore in the operating system. We have seen how it manages to retain process synchronization. We have also covered the types of semaphore. So, the semaphore is quite complicated and should be implemented carefully.
Leave a Reply