Share
Explore BrainMass

Semaphores

Semaphores were conceived of by Edsger Dijkstra who is also well-known for his self-titled shortest-path algorithm and early stance against goto statements.

In a multitasking system (such as any OS), resource allocation is very important. Semaphores are flags that keep track of whether a resource is available for use by a process at a certain time. As many resources allow multiple uses simultaneously, a semaphore can either keep track of the number of users, or simple be binary, indicating whether a resource is available for use or not with a 1 or 0. They can be used both as locks and conditional variables. Their usage this way makes them important in parallel programming and multi-user systems as they help, but do not completely, prevent race conditions from occurring.

There are two basic operations for semaphores:

  1. turn on/set the semaphore (if non binary, then increment and only deny access when a limit value has been reached), showing that the resource is being utilised to its maximum capacity
  2. turn off/clear the semaphore (if non-binary, decrement but remain non-negative), showing that the resource can allow at least one more process to use it without going over maximum capacity

Often the second operation will be 'waiting' and activate automatically when the semaphore is set. It is wise to allow a superuser of some kind the ability to 'open' semaphores to allow resource use, should a case of deadlock occur.

 

Dijkstra photo credit: Hamilton Richards

Further Reading: Technopedia - Sempahores