Why semaphore is needed




















Mutex uses the lock-based technique to handle the critical section problem. Whenever a process requests for a resource from the system, then the system will create a mutex object with a unique name or ID. So, whenever the process wants to use that resource, then the process occupies a lock on the object. After locking, the process uses the resource and finally releases the mutex object. After that, other processes can create the mutex object in the same manner and use it.

By locking the object, that particular resource is allocated to that particular process and no other process can take that resource. So, in the critical section, no other processes are allowed to use the shared resource. In this way, the process synchronization can be achieved with the help of a mutex object. Semaphore is an integer variable S , that is initialized with the number of resources present in the system and is used for process synchronization.

It uses two functions to change the value of S i. Both these functions are used to modify the value of semaphore but the functions allow only one process to change the value at a particular time i. There are two categories of semaphores i. Counting semaphores and Binary semaphores. In Counting semaphores , firstly, the semaphore variable is initialized with the number of resources available.

After that, whenever a process needs some resource, then the wait function is called and the value of the semaphore variable is decreased by one. The process then uses the resource and after using the resource, the signal function is called and the value of the semaphore variable is increased by one. So, when the value of the semaphore variable goes to 0 i. In this way, we achieve the process synchronization. In Binary semaphores , the value of the semaphore variable will be 0 or 1.

Initially, the value of semaphore variable is set to 1 and if some process wants to use some resource then the wait function is called and the value of the semaphore is changed to 0 from 1. The process then uses the resource and when it releases the resource then the signal function is called and the value of the semaphore variable is increased to 1. If at a particular instant of time, the value of the semaphore variable is 0 and some other process wants to use the same resource then it has to wait for the release of the resource by the previous process.

Now if suppose P1 enters in its critical section then the value of semaphore s becomes 0. This way mutual exclusion is achieved. Look at the below image for details which is Binary semaphore. 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. 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. 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. 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.

To avoid this another implementation is provided below. 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. Skip to content. Change Language. After the semaphore value is decreased, which becomes negative, the command is held up until the required conditions are satisfied.

This type of Semaphore operation is used to control the exit of a task from a critical section. It helps to increase the value of the argument by 1, which is denoted as V S. Skip to content. What is Semaphore? Report a Bug. Previous Prev. Next Continue. Home Testing Expand child menu Expand. SAP Expand child menu Expand. Web Expand child menu Expand. Must Learn Expand child menu Expand.



0コメント

  • 1000 / 1000