The different aspects of Semaphores


Semaphore’ is a term used for a variable in the Operating System, which acts as a counter. When multiple processes try to access the same resource or file continually, then access to the particular file should be controlled by assigning a value to the semaphore.

A Semaphore is like an integer, with three differences. The first one is, when you create a semaphore, you can initialize its value to any integer, but after that the only operations that are allowed to perform are increment and decrement and it is not possible to read the current values of the semaphore. The second difference is when the thread decrements the semaphore and if the result gets negative, the thread blocks itself and will not continue until other thread increments the semaphore. Third one is when a thread increments the semaphore, and if there are other threads waiting, one among the waiting threads will get unblocked.

The value of the semaphore is initialized by the first process, when the file is in access. When the second process tries to access the file, it first checks the value of the semaphore and if it finds the value as initialized it will not then access the file. After the first process is completed, it resets the semaphore value and now the second process uses it. The above example is only for two processes, but a semaphore can be used for the same file as well. Thus semaphores are used to co-ordinate access to a resource by different processes.

Aspects of Semaphore: The semaphores are identified by the semaphore ID, which is unique for each semaphore and it can be deleted by using the function ‘semdelete’. The semaphore value can be incremented or decremented by using the functions ‘wait’ and ‘signal’ respectively. The value of semaphore represents the number of threads which are nothing but processes. If the value is positive, then the thread will decrement and proceed for execution without suspending. If the value of the semaphore is negative then the number of threads or processes it represents will get blocked and kept in suspended states. If the value of semaphore is zero then it means there are no threads and processes in the waiting state. So the important fact in semaphore is they are assigned with values.

Kernel Support for Semaphores: They are mainly used for synchronizing the access to shared memory segments. Let us consider, semaphore as an integer named variable that is a resource counter. The value of the variable at any point in time is the number of resource units available. If we have one resource, i.e., one file is shared. Then the valid semaphore values are zero and one. As the semaphore is to provide synchronization between processes, the actual semaphore value must be stored in the kernel. Semaphore is a single binary, with a single value that can be either zero or one. In addition to that, kernel also maintains three other pieces of information for each value in the set.

1. The process ID of the process that does the last operation on the value.

2. A count on the number of processes waiting for the value to increase.

3. A count on the number of processes waiting for the value to become zero.

Written by admin

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>