All Topics  
Semaphore (programming)

 

   Email Print
   Bookmark   Link






 

Semaphore (programming)



 
 
For other uses, see Semaphore
Semaphore

A semaphore telegraph, optical telegraph, shutter telegraph chain, Chappe telegraph, or Napoleon I of France semaphore is a system of conveying information by means of visual signals, using towers with pivoting shutters, also known as blades or paddles....
.


In computer science, a semaphore is a protected variable
Variable

A variable is a symbol that stands for a value that may vary; the term usually occurs in opposition to constant, which is a symbol for a non-varying value, i.e....
 or abstract data type
Abstract data type

In computing, an abstract data type is a specification of a set of data and the set of operations that can be performed on the data. Such a data type is abstract in the sense that it is independent of various concrete implementations....
 which constitutes the classic method for restricting access to shared resources such as shared memory
Shared memory

In computing, shared memory is a memory that may be simultaneously accessed by multiple programs with an intent to provide communication among them or avoid redundant copies....
 in a multiprogramming environment. A counting semaphore is a counter for a set of available resources, rather than a locked/unlocked flag of a single resource. It was invented by Edsger Dijkstra
Edsger Dijkstra

Edsger Wybe Dijkstra was a Netherlands computer science. He received the 1972 Turing Award for fundamental contributions in the area of programming languages, and was the Schlumberger Centennial Chair of Computer Sciences at University of Texas at Austin from 1984 until 2000....
. Semaphores are the classic solution to preventing race conditions in the dining philosophers problem
Dining philosophers problem

In computer science, the dining philosophers problem is an illustrative example of a common computing problem in Concurrency . It is a classic Parallel programming synchronization problem....
, although they do not prevent resource deadlock
Deadlock

A deadlock is a situation wherein two or more competing actions are waiting for the other to finish, and thus neither ever does. It is often seen in a paradox like 'the chicken or the egg'....
s.

phores can only be accessed using the following operations.






Discussion
Ask a question about 'Semaphore (programming)'
Start a new discussion about 'Semaphore (programming)'
Answer questions from other users
Full Discussion Forum



Encyclopedia


For other uses, see Semaphore
Semaphore

A semaphore telegraph, optical telegraph, shutter telegraph chain, Chappe telegraph, or Napoleon I of France semaphore is a system of conveying information by means of visual signals, using towers with pivoting shutters, also known as blades or paddles....
.


In computer science, a semaphore is a protected variable
Variable

A variable is a symbol that stands for a value that may vary; the term usually occurs in opposition to constant, which is a symbol for a non-varying value, i.e....
 or abstract data type
Abstract data type

In computing, an abstract data type is a specification of a set of data and the set of operations that can be performed on the data. Such a data type is abstract in the sense that it is independent of various concrete implementations....
 which constitutes the classic method for restricting access to shared resources such as shared memory
Shared memory

In computing, shared memory is a memory that may be simultaneously accessed by multiple programs with an intent to provide communication among them or avoid redundant copies....
 in a multiprogramming environment. A counting semaphore is a counter for a set of available resources, rather than a locked/unlocked flag of a single resource. It was invented by Edsger Dijkstra
Edsger Dijkstra

Edsger Wybe Dijkstra was a Netherlands computer science. He received the 1972 Turing Award for fundamental contributions in the area of programming languages, and was the Schlumberger Centennial Chair of Computer Sciences at University of Texas at Austin from 1984 until 2000....
. Semaphores are the classic solution to preventing race conditions in the dining philosophers problem
Dining philosophers problem

In computer science, the dining philosophers problem is an illustrative example of a common computing problem in Concurrency . It is a classic Parallel programming synchronization problem....
, although they do not prevent resource deadlock
Deadlock

A deadlock is a situation wherein two or more competing actions are waiting for the other to finish, and thus neither ever does. It is often seen in a paradox like 'the chicken or the egg'....
s.

Introduction

Semaphores can only be accessed using the following operations. Those marked atomic should not be interrupted (that is, if the system decides that the "turn is up" for the program doing this, it shouldn't stop it in the middle of those instructions) for the reasons explained below.

P(Semaphore s) // Acquire Resource

V(Semaphore s) // Release Resource

Init(Semaphore s, Integer v)

Notice that incrementing the variable s must not be interrupted, and the P operation must not be interrupted after s is found to be greater than 0. This can be done using a special instruction such as test-and-set
Test-and-set

In computer science, the test-and-set instruction is an instruction used to both test and write to a memory location as part of a single atomic operation....
 (if the architecture's instruction set
Instruction set

An instruction set is a list of all the instruction , and all their variations, that a processor can execute.Instructions include:* Arithmetic such as add and subtract...
 supports it), or (on uniprocessor systems) ignoring interrupt
Interrupt

In computing, an interrupt is an asynchronous communication signal from hardware indicating the need for attention or a synchronous event in software indicating the need for a change in execution....
s in order to prevent other processes from becoming active.

The value of a semaphore is the number of units of the resource which are free. (If there is only one resource, a "binary
Binary numeral system

The binary numeral system, or notation with a radix of 2. Owing to its straightforward implementation in digital electronic circuitry using logic gates, the binary system is used internally by all modern computers....
 semaphore" with values 0 or 1 is used.) The P operation busy-waits
Busy waiting

In software engineering, busy waiting or spinning is a technique in which a process repeatedly checks to see if a condition is true, such as waiting for Computer keyboard input or waiting for a lock to become available....
 (uses its turn to do nothing) or maybe sleeps (tells the system not to give it a turn) until a resource is available, whereupon it immediately claims one. V is the inverse; it simply makes a resource available again after the process has finished using it. Init is only used to initialize the semaphore before any requests are made. The P and V operations must be atomic, which means that no process may ever be preempted in the middle of one of those operations to run another operation on the same semaphore.

The canonical names P and V come from the initials of Dutch
Dutch language

Dutch is a West Germanic languages spoken by over 22 million people as a first language, and about 5 million people as a second language."1% of the EU population claims to speak Dutch well enough in order to have a conversation." Outside the European Union the number of second language speakers of Dutch is very small. Most native...
 words. V stands for verhogen, or "increase". Several explanations have been given for P (including proberen for "to test", passeer for "pass", probeer "try", and pakken "grab"), but in fact Dijkstra wrote that he intended P to stand for the made-up word prolaag, short for probeer te verlagen, or "try-and-decrease" (A less ambiguous, and more accurate, English translation would be "try-to-decrease".) This confusion stems from the unfortunate characteristic of the Dutch language that the words for increase and decrease both begin with the letter V, and the words spelled out in full would be impossibly confusing for non–Dutch-speakers.

In the programming language
Programming language

A programming language is a machine-readable artificial language designed to express computations that can be performed by a machine, particularly a computer....
 ALGOL 68
ALGOL 68

ALGOL 68 is an imperative programming computer programming programming language that was conceived as a successor to the ALGOL 60 programming language, designed with the goal of a much wider scope of application and more rigorously defined syntax and semantics....
, in the Linux kernel
Linux kernel

The Linux kernel is an operating system kernel used by a family of Unix-like operating systems. The term Linux distribution is used to refer to the various operating systems that run on top of the Linux Kernel....
, and in some English textbooks, the P and V operations are called, respectively, down and up. In software engineering practice, they are often called wait and signal, or acquire and release (which the standard Java
Java (programming language)

Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java ....
 library uses ), or pend and post. Some texts call them procure and vacate to match the original Dutch initials.

To avoid busy-waiting, a semaphore may have an associated queue of processes (usually a first-in, first out
FIFO

FIFO is an acronym for First In, First Out, an abstraction in ways of organizing and manipulation of data relative to time and prioritization....
). If a process performs a P operation on a semaphore which has the value zero, the process is added to the semaphore's queue. When another process increments the semaphore by performing a V operation, and there are processes on the queue, one of them is removed from the queue and resumes execution. When processes have different priorities, the queue may be ordered by priority, so that the highest priority process is taken from the queue first.

The counting semaphore concept can be extended with the ability of claiming or returning more than one 'unit' from the semaphore. This is indeed the way the classical UNIX semaphore works. The modified P and V operations work like this:

P(Semaphore s, integer howmany)

V(Semaphore s, integer howmany)

To understand why it is better than just calling the simple version of P 'howmany' times consider the following problem. Let's say you have a pool of N resources, say fixed size buffers. You may want to use a counting semaphore initialised to N to keep track of the number of the buffers available. When a process wants to allocate a buffer, it calls P on the semaphore and gets a buffer. If there are no buffers available, a process waits until some other process releases a buffer and invokes V on the semaphore.

Consider that there are two processes that respectively want to acquire K < N and L < N buffers, such that K + L > N. The naive implementation would have the first process call the simple decrementing variant P on the semaphore K times, and it would have the second process call the simple decrementing variant P on the semaphore L times. However, this approach can lead to a deadlock: Imagine that the operating system allows the first process to run. Then, when the first process has only acquired control of Z buffers (such that Z < K and Z + L > N), the operating system preempts
Preemption (computing)

Preemption in computing is the act of temporarily interrupting a task being carried out by a computer, without requiring its cooperation, and with the intention of resuming the task at a later time....
 the first process to allow the second process time to run. The second process begins acquiring buffers. However, when the second process acquires (N - Z) buffers, the semaphore becomes 0 and the second process gets suspended in order to wait for some other process to free up more buffers (because L > N - Z). The operating system eventually allows the first process to resume, continuing its quest to acquire the remaining (K - Z) buffers that it needs. Unfortunately, since the semaphore is 0, the first process cannot complete this task, so it too becomes suspended in order to wait for some other process to free up more buffers. Neither the first nor the second process can acquire enough buffers to continue, and therefore neither returns any buffers to the pool. Thus, they are stuck in a deadlock situation.

With the modified semaphore version, the first process will ask for K buffers (or more precisely, semaphore units), which it will get in an atomic operation, leaving N-K units on the semaphore. Then the second process arrives, decrements the semaphore down to N-K-L and since that is a negative number, will wait. As the first process releases buffers and increments the semaphore, as soon as the semaphore reaches 0 it means that there are L elements available in the pool, the second process can be woken up and can receive all of its buffers.

It should be noted that the semaphore count is not necessarily equal to the buffers available in the pool. The checking and waiting twice for the s >= 0 condition in P is needed to guarantee that as multiple processes are added to the semaphore's waiting list they do not disturb each other's request: a process does not change the semaphore's count until it is next in the queue. In real implementations it is done without actually activating up the waiting process for the intermediate step.

Semaphores today as used by programmers

Semaphores remain in common use in programming language
Programming language

A programming language is a machine-readable artificial language designed to express computations that can be performed by a machine, particularly a computer....
s that do not intrinsically support other forms of synchronization. They are the primitive synchronization mechanism in many operating system
Operating system

An operating system is an interface between hardware and applications; it is responsible for the management and coordination of activities and the sharing of the limited resources of the computer....
s. The trend in programming language development, though, is towards more structured forms of synchronization, such as monitor
Monitor (synchronization)

In concurrent programming, a monitor is an Object intended to be used safely by more than one Thread .The defining characteristic of a monitor is that its methods are executed with mutual exclusion....
s (though these advanced structures typically employ semaphores behind the scenes). In addition to their inadequacies in dealing with (multi-resource) deadlocks, semaphores do not protect the programmer from the easy mistakes of taking a semaphore that is already held by the same process, and forgetting to release a semaphore that has been taken.

Example usage

Since semaphores have a count associated with them, they may be employed when multiple threads need to achieve an objective cooperatively. Consider this example:
A thread named A needs information from two databases before it can proceed. Access to these databases is controlled by two separate threads B, C. These two threads have a message-processing loop; anybody needing to use one of the databases posts a message into the corresponding thread's message queue. Thread A initializes a semaphore S with init(S,-1). A then posts a data request, including a pointer to the semaphore S, to both B and C. Then A calls P(S), which blocks. The other two threads meanwhile take their time obtaining the information; when each thread finishes obtaining the information, it calls V(S) on the passed semaphore. Only after both threads have completed will the semaphore's value be positive and A be able to continue. A semaphore used in this way is called a "counting semaphore."


Apart from a counting semaphore, there is a "blocking semaphore". A blocking semaphore is a semaphore that is initialized to zero. This has the effect that any thread that does a P(S) will block until another thread does a V(S). This kind of construct is very useful when the order of execution among threads needs to be controlled.

Hardware support


The use of semaphores normally requires hardware support to guarantee the atomicity of operations that require it. Computer machine languages typically include instructions that are designed specifically with semaphores in mind. These special instructions carry out a read-modify-write
Read-modify-write

In computer science, read-modify-write is a class of Linearizability such as test-and-set, fetch-and-add, and compare-and-swap which both read a memory location and write a new value into it simultaneously, either with a completely new value or some function of the previous value....
 cycle to memory that is not only uninterruptible but excludes all other operations to the same location in memory by any other processors or input/output devices. The special instructions guarantee that a semaphore procedure using them can test and alter a semaphore in a single, atomic operation.

Binary semaphore vs. Mutex


A mutex is a binary semaphore, usually including extra features like ownership or priority inversion protection. The differences between mutexes and semaphores are operating system dependent. Mutexes are meant to be used for mutual exclusion only and binary semaphores are meant to be used for event notification and mutual exclusion.

See also

  • Cigarette smokers problem
    Cigarette smokers problem

    The cigarette smokers problem is a concurrency problem, originally described in 1971 by Suhas Patil....
  • Dining philosophers problem
    Dining philosophers problem

    In computer science, the dining philosophers problem is an illustrative example of a common computing problem in Concurrency . It is a classic Parallel programming synchronization problem....
  • Readers-writers problem
    Readers-writers problem

    In computer science, the first and second readers-writers problems are examples of a common computing problem in concurrency . The two problems deal with situations in which many thread s must access the same shared memory at one time, some reading and some writing, with the natural constraint that no process may access the share for reading...
  • Sleeping barber problem
    Sleeping barber problem

    In computer science, the sleeping barber problem is a classic inter-process communication and synchronization problem between multiple operating system Process ....
  • Reentrant mutex
    Reentrant mutex

    A reentrant mutex is a mutual exclusion mechanism. In a reentrant mutex, the same thread can acquire the lock multiple times. However, the lock must be released the same number of times or else other threads will be unable to acquire the lock....
    , which can "count" like a counting semaphore.


External links

  • , in which Dijkstra introduces the concept (in Dutch
    Dutch language

    Dutch is a West Germanic languages spoken by over 22 million people as a first language, and about 5 million people as a second language."1% of the EU population claims to speak Dutch well enough in order to have a conversation." Outside the European Union the number of second language speakers of Dutch is very small. Most native...
    )
  • programming interface - The Open Group Base Specifications Issue 6, IEEE Std 1003.1
  • , by Allen B. Downey