<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://debianws.lexgopc.com/wiki1463/index.php?action=history&amp;feed=atom&amp;title=Semaphore_%28programming%29</id>
	<title>Semaphore (programming) - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://debianws.lexgopc.com/wiki1463/index.php?action=history&amp;feed=atom&amp;title=Semaphore_%28programming%29"/>
	<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki1463/index.php?title=Semaphore_(programming)&amp;action=history"/>
	<updated>2026-12-04T02:33:20Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.46.3</generator>
	<entry>
		<id>http://debianws.lexgopc.com/wiki1463/index.php?title=Semaphore_(programming)&amp;diff=118812&amp;oldid=prev</id>
		<title>imported&gt;Xathious: Interceptor: Reverting editing test by ~2026-51078-34</title>
		<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki1463/index.php?title=Semaphore_(programming)&amp;diff=118812&amp;oldid=prev"/>
		<updated>2026-09-22T08:28:18Z</updated>

		<summary type="html">&lt;p&gt;&lt;a href=&quot;/wiki1463/index.php?title=WP:CVPI&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new&quot; title=&quot;WP:CVPI (page does not exist)&quot;&gt;Interceptor&lt;/a&gt;: Reverting editing test by &lt;a href=&quot;/wiki1463/index.php?title=Special:Contributions/~2026-51078-34&quot; title=&quot;Special:Contributions/~2026-51078-34&quot;&gt;~2026-51078-34&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{short description|Variable used in a concurrent system}}&lt;br /&gt;
{{about|the computer programming term|other uses|Semaphore (disambiguation)}}&lt;br /&gt;
{{More citations needed|date=September 2025}}&lt;br /&gt;
&lt;br /&gt;
In [[computer science]], a &amp;#039;&amp;#039;&amp;#039;semaphore&amp;#039;&amp;#039;&amp;#039; is a [[Synchronization (computer science)|synchronization device]] used to control access to a common resource by multiple [[Thread (computing)|threads]] or [[process (computing)|processes]] in a [[concurrent system]], such as a [[Computer multitasking|multitasking]] operating system. It allows simultaneous access to a [[critical section]] by some predetermined number of processes.&lt;br /&gt;
&lt;br /&gt;
A useful way to think of a semaphore is as a record of how many units of a particular resource are available, coupled with operations to adjust that record &amp;#039;&amp;#039;safely&amp;#039;&amp;#039; (i.e., to avoid [[race condition]]s) as units are acquired or become free, and, if necessary, wait until a unit of the resource becomes available. Though semaphores are useful for preventing race conditions, they do not guarantee their absence. &lt;br /&gt;
&lt;br /&gt;
Semaphores that allow an arbitrary resource count are called &amp;#039;&amp;#039;&amp;#039;counting semaphores&amp;#039;&amp;#039;&amp;#039;, while semaphores that are restricted to the values 0 and 1 (or locked/unlocked, unavailable/available) are called &amp;#039;&amp;#039;&amp;#039;binary semaphores&amp;#039;&amp;#039;&amp;#039; and are used to implement [[Lock (computer science)|locks]].&lt;br /&gt;
&lt;br /&gt;
The semaphore concept was invented by [[Dutch people|Dutch]] [[computer scientist]] [[Edsger Dijkstra]] in 1962 or 1963,&amp;lt;ref name=&amp;quot;ReferenceA&amp;quot;&amp;gt;{{Cite EWD|35|Over de sequentialiteit van procesbeschrijvingen}} (undated, 1962 or 1963)&amp;lt;/ref&amp;gt; when Dijkstra and his team were developing an [[operating system]] for the [[Electrologica X8]]. That system eventually became known as the [[THE multiprogramming system]].&lt;br /&gt;
&lt;br /&gt;
==Library analogy==&lt;br /&gt;
Suppose a physical [[library]] has ten identical study rooms, to be used by one student at a time. Students must request a room from the front desk. If no rooms are free, students wait at the desk until someone relinquishes a room. When a student has finished using a room, the student must return to the desk and indicate that the room is free.&lt;br /&gt;
&lt;br /&gt;
In the simplest implementation, the [[clerk]] at the [[front desk]] knows only the number of free rooms available. This requires that all of the students use their room while they have signed up for it and return it when they are done. When a student requests a room, the clerk decreases this number. When a student releases a room, the clerk increases this number. The room can be used as long as desired and rooms cannot be booked in advance.&lt;br /&gt;
&lt;br /&gt;
In this scenario, the front desk count-holder represents a counting semaphore, the rooms are the resource, and the students represent [[Process (computing)|processes]]/[[Thread (computing)|threads]]. The value of the semaphore in this scenario is initially 10, with all rooms empty. When a student requests a room, they are granted access, and the value of the semaphore is changed to 9. After the next student comes, it drops to 8, then 7, and so on. If someone requests a room and the current value of the semaphore is 0,&amp;lt;ref name=&amp;quot;LittleBookOfSemaphores&amp;quot;&amp;gt;[http://greenteapress.com/semaphores/ &amp;#039;&amp;#039;The Little Book of Semaphores&amp;#039;&amp;#039;] Allen B. Downey&amp;lt;/ref&amp;gt; they are forced to wait until a room is freed (when the count is increased from 0). If one of the rooms was released, but there are several students waiting, any method can be used to select the one who will occupy the room (like [[FIFO (computing and electronics)|FIFO]] or random selection). And of course, a student must inform the clerk about releasing their room only after leaving it.&lt;br /&gt;
&lt;br /&gt;
===Important observations===&lt;br /&gt;
When used to control access to a [[pool (computer science)|pool]] of resources, a semaphore tracks only &amp;#039;&amp;#039;how many&amp;#039;&amp;#039; resources are free. It does not keep track of &amp;#039;&amp;#039;which&amp;#039;&amp;#039; of the resources are free. Some other mechanism (possibly involving more semaphores) may be required to select a particular free resource.&lt;br /&gt;
&lt;br /&gt;
The paradigm is especially powerful because the semaphore count may serve as a useful trigger for a number of different actions. The librarian above may turn the lights off in the study hall when there are no students remaining, or may place a sign that says the rooms are very busy when most of the rooms are occupied.&lt;br /&gt;
&lt;br /&gt;
The success of the protocol requires applications to follow it correctly. Fairness and safety are likely to be compromised (which practically means a program may behave slowly, act erratically, [[hang (computing)|hang]], or [[crash (computing)|crash]]) if even a single process acts incorrectly. This includes:&lt;br /&gt;
* requesting a resource and forgetting to release it;&lt;br /&gt;
* releasing a resource that was never requested;&lt;br /&gt;
* holding a resource for a long time without needing it;&lt;br /&gt;
* using a resource without requesting it first (or after releasing it).&lt;br /&gt;
&lt;br /&gt;
Even if all processes follow these rules, &amp;#039;&amp;#039;multi-resource [[deadlock (computer science)|deadlock]]&amp;#039;&amp;#039; may still occur when there are different resources managed by different semaphores and when processes need to use more than one resource at a time, as illustrated by the [[dining philosophers problem]].&lt;br /&gt;
&lt;br /&gt;
==Semantics and implementation==&lt;br /&gt;
Counting semaphores are equipped with two operations, historically denoted as P and V (see {{section link||Operation names}} for alternative names). Operation V increments the semaphore &amp;#039;&amp;#039;S&amp;#039;&amp;#039;, and operation P decrements it.&lt;br /&gt;
&lt;br /&gt;
The value of the semaphore &amp;#039;&amp;#039;S&amp;#039;&amp;#039; represents the number of units of available resource units when non-negative. In some implementations, negative values indicate the number of processes waiting for the resource. The P operation [[busy waiting|wastes time]] or [[Sleep (system call)|sleeps]] until a resource protected by the semaphore becomes available, at which time the resource is immediately claimed. The V operation is the inverse: it makes a resource available again after the process has finished using it.&lt;br /&gt;
One important property of semaphore &amp;#039;&amp;#039;S&amp;#039;&amp;#039; is that its value cannot be changed except by using the V and P operations.&lt;br /&gt;
&lt;br /&gt;
A simple way to understand {{mono|wait}} (P) and {{mono|signal}} (V) operations is:&lt;br /&gt;
* {{mono|wait}}: Decrements the value of the semaphore variable by 1. If the new value of the semaphore variable is negative, the process executing {{mono|wait}} is blocked (i.e., added to the semaphore&amp;#039;s queue). Otherwise, the process continues execution, having used a unit of the resource.&lt;br /&gt;
* {{mono|signal}}: Increments the value of the semaphore variable by 1. After the increment, if the pre-increment value was negative (meaning there are processes waiting for a resource), it transfers a blocked process from the semaphore&amp;#039;s waiting queue to the ready queue.&lt;br /&gt;
&lt;br /&gt;
Many operating systems provide efficient semaphore primitives that unblock a waiting process when the semaphore is incremented. This means that processes do not waste time checking the semaphore value unnecessarily.&lt;br /&gt;
&lt;br /&gt;
The counting semaphore concept can be extended with the ability to claim or return more than one &amp;quot;unit&amp;quot; from the semaphore.&amp;lt;ref&amp;gt;{{Cite web |title=Semaphore in tokio::sync - Rust |url=https://docs.rs/tokio/latest/tokio/sync/struct.Semaphore.html#method.acquire_many |access-date=2026-08-20 |website=docs.rs}}&amp;lt;/ref&amp;gt; This is sometimes called a weighted semaphore.&amp;lt;ref&amp;gt;{{Cite web |title=semaphore package - golang.org/x/sync/semaphore - Go Packages |url=https://pkg.go.dev/golang.org/x/sync/semaphore#Weighted |access-date=2026-08-19 |website=pkg.go.dev}}&amp;lt;/ref&amp;gt; The modified V and P operations are as follows, using square brackets to indicate [[atomic operation]]s, i.e., operations that appear indivisible to other processes:&lt;br /&gt;
&lt;br /&gt;
 &amp;#039;&amp;#039;&amp;#039;function&amp;#039;&amp;#039;&amp;#039; V(semaphore S, integer I):&lt;br /&gt;
     [S ← S + I]&lt;br /&gt;
 &lt;br /&gt;
 &amp;#039;&amp;#039;&amp;#039;function&amp;#039;&amp;#039;&amp;#039; P(semaphore S, integer I):&lt;br /&gt;
     &amp;#039;&amp;#039;&amp;#039;repeat:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
         [&amp;#039;&amp;#039;&amp;#039;if&amp;#039;&amp;#039;&amp;#039; S ≥ I:&lt;br /&gt;
         S ← S − I&lt;br /&gt;
         &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039;]&lt;br /&gt;
&lt;br /&gt;
Weighted semaphores are useful for limiting access to, e.g., memory or disk space: a process that needs &amp;#039;&amp;#039;N&amp;#039;&amp;#039; megabytes of space to run needs to decrease the semaphore by &amp;#039;&amp;#039;N&amp;#039;&amp;#039; units. However, doing this in &amp;#039;&amp;#039;N&amp;#039;&amp;#039; separate calls to &amp;#039;&amp;#039;P&amp;#039;&amp;#039; can cause deadlocks.&lt;br /&gt;
&lt;br /&gt;
However, the rest of this section refers to semaphores with unary V and P operations, unless otherwise specified.&lt;br /&gt;
&lt;br /&gt;
To avoid [[Resource starvation|starvation]], a semaphore has an associated [[queue (data structure)|queue]] of processes (usually with [[FIFO (computing and electronics)|FIFO]] semantics). If a process performs a P operation on a semaphore that has the value zero, the process is added to the semaphore&amp;#039;s queue and its execution is suspended. 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 thereby, such that the highest priority process is taken from the queue first.&lt;br /&gt;
&lt;br /&gt;
If the implementation does not ensure atomicity of the increment, decrement, and comparison operations, there is a risk of increments or decrements being forgotten, or of the semaphore value becoming negative. Atomicity may be achieved by using a machine instruction that can [[read–modify–write|read, modify, and then write]] the semaphore in a single operation. Without such a hardware instruction, an atomic operation may be synthesized by using a [[Mutual exclusion#Software solutions|software mutual exclusion algorithm]]. On [[uniprocessor]] systems, atomic operations can be ensured by temporarily suspending [[preemption (computing)|preemption]] or disabling hardware [[interrupt]]s. This approach does not work on multiprocessor systems where it is possible for two programs sharing a semaphore to run on different processors at the same time. To solve this problem in a multiprocessor system, a locking variable can be used to control access to the semaphore. The locking variable is manipulated using a [[Test-and-set|test-and-set-lock]] command.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
&lt;br /&gt;
===Trivial example===&lt;br /&gt;
Consider a variable &amp;#039;&amp;#039;A&amp;#039;&amp;#039; and a Boolean variable &amp;#039;&amp;#039;S&amp;#039;&amp;#039;. &amp;#039;&amp;#039;A&amp;#039;&amp;#039; is only accessed when &amp;#039;&amp;#039;S&amp;#039;&amp;#039; is marked true. Thus, &amp;#039;&amp;#039;S&amp;#039;&amp;#039; is a semaphore for &amp;#039;&amp;#039;A&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
One can imagine a stoplight signal (&amp;#039;&amp;#039;S&amp;#039;&amp;#039;) just before a train station (&amp;#039;&amp;#039;A&amp;#039;&amp;#039;). In this case, if the signal is green, then one can enter the train station. If it is yellow or red (or any other color), the train station cannot be accessed.&lt;br /&gt;
&lt;br /&gt;
===Login queue===&lt;br /&gt;
Consider a system that can only support ten users (S=10). Whenever a user logs in, P is called, decrementing the semaphore &amp;#039;&amp;#039;S&amp;#039;&amp;#039; by 1. Whenever a user logs out, V is called, incrementing &amp;#039;&amp;#039;S&amp;#039;&amp;#039; by 1 representing a login slot that has become available. When &amp;#039;&amp;#039;S&amp;#039;&amp;#039; is 0, any users wishing to log in must wait until &amp;#039;&amp;#039;S&amp;#039;&amp;#039; increases. The login request is enqueued onto a FIFO queue until a slot is freed. [[Mutual exclusion]] is used to ensure that requests are enqueued in order. Whenever &amp;#039;&amp;#039;S&amp;#039;&amp;#039; increases (login slots available), a login request is dequeued, and the user owning the request is allowed to log in. If &amp;#039;&amp;#039;S&amp;#039;&amp;#039; is already greater than 0, then login requests are immediately dequeued.&lt;br /&gt;
&lt;br /&gt;
===Producer–consumer problem===&lt;br /&gt;
In the [[producer–consumer problem]], one process (the producer) generates data items and another process (the consumer) receives and uses them. They communicate using a queue of maximum size &amp;#039;&amp;#039;N&amp;#039;&amp;#039; and are subject to the following conditions:&lt;br /&gt;
* the consumer must wait for the producer to produce something if the queue is empty;&lt;br /&gt;
* the producer must wait for the consumer to consume something if the queue is full.&lt;br /&gt;
&lt;br /&gt;
The semaphore solution to the producer–consumer problem tracks the state of the queue with two semaphores: &amp;lt;code&amp;gt;emptyCount&amp;lt;/code&amp;gt;, the number of empty places in the queue, and &amp;lt;code&amp;gt;fullCount&amp;lt;/code&amp;gt;, the number of elements in the queue. To maintain integrity, &amp;lt;code&amp;gt;emptyCount&amp;lt;/code&amp;gt; may be lower (but never higher) than the actual number of empty places in the queue, and &amp;lt;code&amp;gt;fullCount&amp;lt;/code&amp;gt; may be lower (but never higher) than the actual number of items in the queue. Empty places and items represent two kinds of resources, empty boxes and full boxes, and the semaphores &amp;lt;code&amp;gt;emptyCount&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;fullCount&amp;lt;/code&amp;gt; maintain control over these resources.&lt;br /&gt;
&lt;br /&gt;
The binary semaphore &amp;lt;code&amp;gt;useQueue&amp;lt;/code&amp;gt; ensures that the integrity of the state of the queue itself is not compromised, for example, by two producers attempting to add items to an empty queue simultaneously, thereby corrupting its internal state. Alternatively a [[mutex]] could be used in place of the binary semaphore.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;emptyCount&amp;lt;/code&amp;gt; is initially &amp;#039;&amp;#039;N&amp;#039;&amp;#039;, &amp;lt;code&amp;gt;fullCount&amp;lt;/code&amp;gt; is initially 0, and &amp;lt;code&amp;gt;useQueue&amp;lt;/code&amp;gt; is initially 1.&lt;br /&gt;
&lt;br /&gt;
The producer does the following repeatedly:&lt;br /&gt;
&lt;br /&gt;
 &amp;#039;&amp;#039;&amp;#039;produce:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
     P(emptyCount)&lt;br /&gt;
     P(useQueue)&lt;br /&gt;
     putItemIntoQueue(item)&lt;br /&gt;
     V(useQueue)&lt;br /&gt;
     V(fullCount)&lt;br /&gt;
&lt;br /&gt;
The consumer does the following repeatedly&lt;br /&gt;
&lt;br /&gt;
 &amp;#039;&amp;#039;&amp;#039;consume:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
     P(fullCount)&lt;br /&gt;
     P(useQueue)&lt;br /&gt;
     item ← getItemFromQueue()&lt;br /&gt;
     V(useQueue)&lt;br /&gt;
     V(emptyCount)&lt;br /&gt;
&lt;br /&gt;
Below is a substantive example:&lt;br /&gt;
# A single consumer enters its critical section. Since &amp;lt;code&amp;gt;fullCount&amp;lt;/code&amp;gt; is 0, the consumer blocks.&lt;br /&gt;
# Several producers enter the producer critical section. No more than &amp;#039;&amp;#039;N&amp;#039;&amp;#039; producers may enter their critical section due to &amp;lt;code&amp;gt;emptyCount&amp;lt;/code&amp;gt; constraining their entry.&lt;br /&gt;
# The producers, one at a time, gain access to the queue through &amp;lt;code&amp;gt;useQueue&amp;lt;/code&amp;gt; and deposit items in the queue.&lt;br /&gt;
# Once the first producer exits its critical section, &amp;lt;code&amp;gt;fullCount&amp;lt;/code&amp;gt; is incremented, allowing one consumer to enter its critical section.&lt;br /&gt;
&lt;br /&gt;
Note that &amp;lt;code&amp;gt;emptyCount&amp;lt;/code&amp;gt; may be much lower than the actual number of empty places in the queue, for example, where many producers have decremented it but are waiting their turn on &amp;lt;code&amp;gt;useQueue&amp;lt;/code&amp;gt; before filling empty places. Note that &amp;lt;code&amp;gt;emptyCount + fullCount ≤ &amp;#039;&amp;#039;N&amp;#039;&amp;#039;&amp;lt;/code&amp;gt; always holds, with equality if and only if no producers or consumers are executing their critical sections.&lt;br /&gt;
&lt;br /&gt;
===Passing the baton pattern===&lt;br /&gt;
The &amp;quot;Passing the baton&amp;quot; pattern&amp;lt;ref&amp;gt;{{cite book |last1=Andrews |first1=Gregory R. | date=1999|publisher=	&lt;br /&gt;
Addison-Wesley|title=Foundations of Multithreaded, Parallel, and Distributed Programming}}&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;{{cite book |last1=Carver |first1=Richard H. |last2=Thai |first2=Kuo-Chung |date=2005|publisher=Wiley|title=Modern Multithreading: Implementing, Testing, and Debugging Multithreaded Java and C++/Pthreads/Win32 Programs}}&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;{{cite book |last1=Maurer |first1=Christian | date=2021|publisher=	&lt;br /&gt;
Springer|title=Nonsequential and Distributed Programming with Go}}&amp;lt;/ref&amp;gt; proposed by Gregory R. Andrews is a generic scheme to solve many complex concurrent programming problems in which multiple processes compete for the same resource with complex access conditions (such as satisfying specific priority criteria or avoiding starvation). Given a shared resource, the pattern requires a private &amp;quot;priv&amp;quot; semaphore (initialized to zero) for each process (or class of processes) involved and a single mutual exclusion &amp;quot;mutex&amp;quot; semaphore (initialized to one). &lt;br /&gt;
The pseudo-code for each process is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
void process(int proc_id, int res_id) {&lt;br /&gt;
	resource_acquire(proc_id, res_id);&lt;br /&gt;
	&lt;br /&gt;
	&amp;lt;use the resource res_id&amp;gt;;&lt;br /&gt;
	&lt;br /&gt;
	resource_release(proc_id, res_id);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The pseudo-code of the resource acquisition and release primitives are:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
void resource_acquire(int proc_id, int res_id) {&lt;br /&gt;
	P(mutex);&lt;br /&gt;
	&lt;br /&gt;
	if (&amp;lt;the condition to access res_id is not verified for proc_id&amp;gt;) {&lt;br /&gt;
		&amp;lt;indicate that proc_id is suspended for res_id&amp;gt;;&lt;br /&gt;
		V(mutex);&lt;br /&gt;
		P(priv[proc_id]);&lt;br /&gt;
		&amp;lt;indicate that proc_id is not suspended for res_id anymore&amp;gt;;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	&amp;lt;indicate that proc_id is accessing the resource&amp;gt;;&lt;br /&gt;
	&lt;br /&gt;
	pass_the_baton(); // See below&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
void resource_release(int proc_id, int res_id) {&lt;br /&gt;
	P(mutex);&lt;br /&gt;
	&lt;br /&gt;
	&amp;lt;indicate that proc_id is not accessing the resource res_id anymore&amp;gt;;&lt;br /&gt;
	&lt;br /&gt;
	pass_the_baton(); // See below&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Both primitives in turn use the &amp;quot;pass_the_baton&amp;quot; method, whose pseudo-code is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
void pass_the_baton(int res_id) {&lt;br /&gt;
	if /* &amp;lt;the condition to access res_id is true for at least one suspended process&amp;gt; */ {&lt;br /&gt;
		int p = &amp;lt;choose the process to wake&amp;gt;;&lt;br /&gt;
		V(priv[p]);&lt;br /&gt;
	} else {&lt;br /&gt;
		V(mutex);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Remarks&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
The pattern is called &amp;quot;passing the baton&amp;quot; because a process that releases the resource as well as a freshly reactivated process will activate at most one suspended process, that is, shall &amp;quot;pass the baton to it&amp;quot;. The mutex is released only when a process is going to suspend itself (resource_acquire), or when pass_the_baton is unable to reactivate another suspended process.&lt;br /&gt;
&lt;br /&gt;
==Operation names==&lt;br /&gt;
The canonical names V and P come from the initials of [[Dutch language|Dutch]] words. V is generally explained as &amp;#039;&amp;#039;verhogen&amp;#039;&amp;#039; (&amp;quot;increase&amp;quot;). Several explanations have been offered for P, including &amp;#039;&amp;#039;proberen&amp;#039;&amp;#039; (&amp;quot;to test&amp;quot; or &amp;quot;to try&amp;quot;),&amp;lt;ref&amp;gt;{{citation |last1 = Silberschatz |first1 = Abraham | last2 = Galvin | first2 = Peter Baer | last3 = Gagne | first3 = Greg | edition = 8th | isbn = 978-0-470-12872-5 | publisher = John Wiley &amp;amp; Sons. Inc | title = Operating System Concepts | year = 2008 |page=234}}&amp;lt;/ref&amp;gt; &amp;#039;&amp;#039;passeren&amp;#039;&amp;#039; (&amp;quot;pass&amp;quot;), and &amp;#039;&amp;#039;pakken&amp;#039;&amp;#039; (&amp;quot;grab&amp;quot;). Dijkstra&amp;#039;s earliest paper on the subject&amp;lt;ref name=&amp;quot;ReferenceA&amp;quot;/&amp;gt; gives &amp;#039;&amp;#039;passering&amp;#039;&amp;#039; (&amp;quot;passing&amp;quot;) as the meaning for &amp;#039;&amp;#039;P&amp;#039;&amp;#039;, and &amp;#039;&amp;#039;vrijgave&amp;#039;&amp;#039; (&amp;quot;release&amp;quot;) as the meaning for V. It also mentions that the terminology is taken from that used in railroad signals. Dijkstra subsequently wrote that he intended &amp;#039;&amp;#039;P&amp;#039;&amp;#039; to stand for &amp;#039;&amp;#039;prolaag&amp;#039;&amp;#039;,&amp;lt;ref&amp;gt;{{Cite EWD|74}}&amp;lt;/ref&amp;gt; short for &amp;#039;&amp;#039;probeer te verlagen&amp;#039;&amp;#039;, literally &amp;quot;try to reduce&amp;quot;, or to parallel the terms used in the other case, &amp;quot;try to decrease&amp;quot;.&amp;lt;ref&amp;gt;{{Cite EWD|51| MULTIPROGAMMERING EN DE X8}} (in [[Dutch language|Dutch]])&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;try-and&amp;quot;&amp;gt;Dijkstra&amp;#039;s own translation reads &amp;quot;try-&amp;#039;&amp;#039;and&amp;#039;&amp;#039;-decrease&amp;quot;, although that phrase might be confusing for those unaware of the [http://www.wsu.edu/~brians/errors/try.html colloquial &amp;quot;try-and...&amp;quot;]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[https://lkml.org/lkml/2005/12/19/34 (PATCH 1/19) MUTEX: Introduce simple mutex implementation] Linux Kernel Mailing List, 19 December 2005&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In [[ALGOL 68]], the [[Linux kernel]],&amp;lt;ref&amp;gt;[http://www.linuxgrill.com/anonymous/fire/netfilter/kernel-hacking-HOWTO-5.html#ss5.3 Linux Kernel hacking HOWTO] {{Webarchive|url=https://web.archive.org/web/20100528154351/http://www.linuxgrill.com/anonymous/fire/netfilter/kernel-hacking-HOWTO-5.html#ss5.3 |date=2010-05-28 }} LinuxGrill.com&amp;lt;/ref&amp;gt; and in some English textbooks, the &amp;#039;&amp;#039;V&amp;#039;&amp;#039; and &amp;#039;&amp;#039;P&amp;#039;&amp;#039; operations are called, respectively, &amp;#039;&amp;#039;up&amp;#039;&amp;#039; and &amp;#039;&amp;#039;down&amp;#039;&amp;#039;. In software engineering practice, they are often called &amp;#039;&amp;#039;signal&amp;#039;&amp;#039; and &amp;#039;&amp;#039;wait&amp;#039;&amp;#039;,&amp;lt;ref name=&amp;quot;plan9&amp;quot;&amp;gt;{{cite conference |last1=Mullender |first1=Sape |first2=Russ |last2=Cox |title=Semaphores in Plan 9 |conference=3rd International Workshop on [[Plan 9 from Bell Labs|Plan 9]] |year=2008 |url=http://doc.cat-v.org/plan_9/IWP9/2008/iwp9_proceedings08.pdf#page=62}}&amp;lt;/ref&amp;gt; &amp;#039;&amp;#039;release&amp;#039;&amp;#039; and &amp;#039;&amp;#039;acquire&amp;#039;&amp;#039;{{r|plan9}} (standard [[Java (programming language)|Java]] library),&amp;lt;ref&amp;gt;{{Javadoc:SE|package=java.util.concurrent|java/util/concurrent|Semaphore}}&amp;lt;/ref&amp;gt; or &amp;#039;&amp;#039;post&amp;#039;&amp;#039; and &amp;#039;&amp;#039;pend&amp;#039;&amp;#039;. Some texts call them &amp;#039;&amp;#039;vacate&amp;#039;&amp;#039; and &amp;#039;&amp;#039;procure&amp;#039;&amp;#039; to match the original Dutch initials.&amp;lt;ref&amp;gt;{{Cite web|url=http://amigadev.elowar.com/read/ADCD_2.1/Includes_and_Autodocs_2._guide/node036A.html|title=exec.library/Procure|website=amigadev.elowar.com|access-date=2016-09-19}}&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;{{Cite web|url=http://amigadev.elowar.com/read/ADCD_2.1/Includes_and_Autodocs_2._guide/node0389.html|title=exec.library/Vacate|website=amigadev.elowar.com|access-date=2016-09-19}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Semaphores vs. mutexes==&lt;br /&gt;
A [[mutex]] is a [[Mutual exclusion#Types of mutual exclusion devices|locking mechanism]] that sometimes uses the same basic implementation as the binary semaphore. However, they differ in how they are used. While a binary semaphore may be colloquially referred to as a mutex, a true mutex has a more specific use-case and definition, in that only the [[Task (computing)|task]] that locked the mutex is supposed to unlock it.{{Citation needed|date=August 2026}} This constraint aims to handle some potential problems of using semaphores:&lt;br /&gt;
# [[Priority inversion]]: If the mutex knows who locked it and is supposed to unlock it, it is possible to promote the priority of that task whenever a higher-priority task starts waiting on the mutex.&lt;br /&gt;
# Premature task termination: Mutexes may also provide deletion safety, where the task holding the mutex cannot be accidentally deleted. {{citation needed|date=May 2019}}  (This is also a cost; if the mutex can prevent a task from being reclaimed, then a garbage collector has to monitor the mutex.)&lt;br /&gt;
# Termination deadlock: If a mutex-holding task terminates for any reason, the [[Real-time operating system|OS]] can release the mutex and signal waiting tasks of this condition.&lt;br /&gt;
# Recursion deadlock: a task is allowed to lock a [[reentrant mutex]] multiple times as it unlocks it an equal number of times.&lt;br /&gt;
# Accidental release: An error is raised on the release of the mutex if the releasing task is not its owner.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
* [[Monitor (synchronization)]]&lt;br /&gt;
* [[Lock (computer science)]]&lt;br /&gt;
* [[Async/await]]&lt;br /&gt;
* [[Synchronization (computer science)]]&lt;br /&gt;
* [[Flag (programming)]]&lt;br /&gt;
* [[Spinlock]]&lt;br /&gt;
* [[Readers–writers problem]]&lt;br /&gt;
* [[Dining philosophers problem]]&lt;br /&gt;
* [[Sleeping barber problem]]&lt;br /&gt;
* [[Cigarette smokers problem]]&lt;br /&gt;
* [[Spurious wakeup]]&lt;br /&gt;
* [[Critical section]]&lt;br /&gt;
* [[Race condition]]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
{{Reflist}}&lt;br /&gt;
&lt;br /&gt;
==External links==&lt;br /&gt;
&lt;br /&gt;
=== Introductions ===&lt;br /&gt;
&lt;br /&gt;
* Hilsheimer, Volker (2004). &amp;quot;[https://doc.qt.io/archives/qq/qq11-mutex.html Implementing a Read/Write Mutex]&amp;quot; (Web page). &amp;#039;&amp;#039;Qt Quarterly&amp;#039;&amp;#039;, Issue 11 - Q3 2004&lt;br /&gt;
* {{cite journal |url= https://see.stanford.edu/materials/icsppcs107/23-Concurrency-Examples.pdf |first1= Julie |last1= Zelenski |first2= Nick |last2= Parlante |series= CS107 Programming Paradigms |volume= Spring 2008 |issue= 23 |journal= Handout |title= Thread and Semaphore Examples |publisher= Stanford Engineering Everywhere (SEE) }}&lt;br /&gt;
&lt;br /&gt;
=== References ===&lt;br /&gt;
*{{Cite EWD|123|Cooperating sequential processes}} (September 1965)&lt;br /&gt;
*{{citation |mode=cs1 |url=http://www.opengroup.org/onlinepubs/009695399/basedefs/semaphore.h.html |section=semaphore.h - semaphores (REALTIME) |title=The Open Group Base Specifications Issue 6 IEEE Std 1003.1, 2004 Edition |publisher=Open Group |date=2004}}&lt;br /&gt;
*{{cite web |url= http://greenteapress.com/semaphores/ |title= The Little Book of Semaphores |first= Allen B. |last= Downey |publisher= Green Tea Press |issue= 2.2.1 |edition= 2nd |date= 2016 |orig-year= 2005 }}&lt;br /&gt;
*{{cite web |url= https://www.mv.helsinki.fi/home/joleppaj/jleppaja_gradu_080511.pdf |title= A pragmatic, historically oriented survey on the universality of synchronization primitives |first= Jouni |last= Leppäjärvi |date= May 11, 2008 |location= University of Oulu, Finland }}&lt;br /&gt;
&lt;br /&gt;
{{Data types}}&lt;br /&gt;
{{Design patterns}}&lt;br /&gt;
{{Inter-process communication}}&lt;br /&gt;
{{Concurrent computing}}&lt;br /&gt;
{{Parallel computing}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{DEFAULTSORT:Semaphore (Programming)}}&lt;br /&gt;
[[Category:Synchronization|Computer science]]&lt;br /&gt;
[[Category:Concurrency control]]&lt;br /&gt;
[[Category:Concurrency (computer science)]]&lt;br /&gt;
[[Category:Parallel computing]]&lt;br /&gt;
[[Category:Computer-mediated communication]]&lt;br /&gt;
[[Category:Edsger W. Dijkstra]]&lt;br /&gt;
[[Category:Dutch inventions]]&lt;/div&gt;</summary>
		<author><name>imported&gt;Xathious</name></author>
	</entry>
</feed>