Semaphore
A semaphore is a synchronization mechanism that controls access by multiple processes to a common resource in a parallel programming environment. It is widely used to control access to file and shared memory. In the computer system, it is a variable whose value indicates the status of the common resource.
Its purpose is to lock resource being used. They are counter used to provide control access to the shared data structure for multiple processes. Operation allocated with semaphore is tested, set and waited. Test and set process is concerned, uninterruptable and can’t be stopped once started. The result of test and wait process is the current value of the semaphore.
Two types of semaphore are:
1) Binary semaphore
2) Counting semaphore
Binary Semaphore:
Suppose there is only one dress changing room in the mall with identical keys of changing room. So, the counter value is either zero or one. Only single customer can occupy the key of changing room at a time. So, no customer can occupy a changing room at the same time. After one customer leaves the dressing room, the key is assigned to another customer who is waiting in the queue. When the customer accesses the dressing room, the counter value is incremented by 1 and when releases the dressing room, the counter value is decremented by 1.
Here counter acts as a binary semaphore. It is like signaling mechanism between processes.
It uses only up-down or lock-unlock status of semaphore. You can increment or decrement its value. If its value is zero, a new thread will try to increment until anyone increments it. Binary semaphore has only 0 and 1 value. If the Semaphore value is zero means it is in lock state but if some another thread is incrementing it then it is in unlock state. Suppose thread A and thread B want to use semaphore. When thread A accesses the semaphore, thread B is in waiting state and is incremented by 1. After execution of thread A, thread B accesses the semaphore by decrementing its value.
Thread B waits for operation on semaphores S, written as B(S), operated as follows:
B(S): IF S > 0
THEN S := S – 1
ELSE (wait on S)
Thread B wakeup operation on semaphore S, written as A(S), operated as follows:
A(S): IF (one or more process or thread are waiting on S)
THEN (one of these processes or thread proceed)
ELSE S := S +1
In binary semaphore, any thread can increment or decrement the value of semaphore. Binary semaphore provides the guarantee of not accessing another process or thread until not finishing the execution of the current process. You need to carefully write the code of semaphore. That’s why binary semaphore doesn’t protect the shared data or resource. Now you notice the difference between semaphore and mutex. Mutex protects the shared data or resource. It is also a synchronization mechanism and similar to binary semaphore. It assigns the priority to thread for access to use it but semaphore doesn’t assign any priority to access it.
Counting semaphore:
We have five dress changing rooms in mall with identical keys of changing room. So, the counter value is five. Currently changing room is empty means no one is accessing the changing room. When someone is accessing the changing room, counter value is decremented by one. After access of all five changing rooms’ counter value is zero. So, customer cannot access more changing rooms. If someone leaves the changing room, counter value is incremented by one. Key is free to be assigned to another customer who is waiting to access the changing room.
Here counter is nothing but counting semaphore. It is like signaling mechanism between processes.
Counting semaphore’s value is not a negative number. Value of counting semaphore is decremented when resource (i.e. thread or process) is accessed and incremented after release of the resource. When the value becomes zero, it cannot access more resources. Counting semaphore is used in multithreading application or when more resources are required. It counts as well as manages the resource (i.e. thread or process).
Filed Under: Tutorials
Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.