The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com




Thread: semaphore
View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #5 (permalink)  
Old 06-09-2009
linuxwebmaster linuxwebmaster is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 2
Post Semaphore

Semaphore: Semaphore is nothing but a term used in UNIX for a variable which acts as a counter. For instance there may be times when two processes try to access the same file simultaneously. In this event we must control the access of the file when the other process is accessing. This is done by assigning value to semaphore.

The value of the semaphore is initialized by the first process when the file is in access by it. When the second process try to access the file it checks the value of the semaphore and if it finds the value as initialized it does not access the file. After the first process is completed it reinitializes the semaphore value and now the second process uses it. The above example is for two processes but a semaphore can be used even when numbers of processes try to access the same file. Thus semaphores are used to coordinate access to a resource by different processes.

We have seen that semaphore can be used when numbers of processes try to access the same file. In this case we must make the semaphore available accessible to all processes so that they can read and check the value and also initialize and reinitialize the value of semaphore appropriately. For this reason only the semaphore is stored in kernel so that it can be accessed by all processes.

The value of semaphore represents thus the number of threads which are nothing but processes. In other words we found that if the value is positive then we have threads to decrement and proceed for execution without suspending. If the value of semaphore is negative then it represents that the number of threads or process is blocked and kept in suspended state. If the value of semaphore is zero then it means that there are no threads or processes in waiting state.