Semaphores: Difference between revisions

From COMP15212 Wiki
gravatar Yuron [userbureaucratinterface-adminsysopPHRhYmxlIGNsYXNzPSJ0d3BvcHVwIj48dHI+PHRkIGNsYXNzPSJ0d3BvcHVwLWVudHJ5dGl0bGUiPkdyb3Vwczo8L3RkPjx0ZD51c2VyPGJyIC8+YnVyZWF1Y3JhdDxiciAvPmludGVyZmFjZS1hZG1pbjxiciAvPnN5c29wPGJyIC8+PC90ZD48L3RyPjwvdGFibGU+] (talk | contribs)
m (1 revision imported)
pc>Yuron
No edit summary
Line 1: Line 1:
{{#set: Priority=3 | Summary=A signalling mechanism for access control in processes.}}<!--
{{#set: Priority=3 | Summary=A signalling mechanism for access control in processes.}}<!--
-->{{#invoke:Dependencies|add|Mutual exclusion,5|Synchronisation,2|Locks,1}}
-->{{#invoke:Dependencies|add|Mutual exclusion,5|Synchronisation,2|Locks,1}}
Semaphores are a means of implementing access control.  They are a
Semaphores are a means of implementing access control.  They are a ‘classic’ mechanism and are employed in operating systems
‘classic’ mechanism and are employed in operating systems
for various purposes.  They can be though of as a count of the number of a particular resource which is currently available.
for various purposes.  They can be though of as a count of the number
of a particular resource which is currently available.


Here is a description of a ‘counting’ semaphore: this can
Here is a description of a ‘counting’ semaphore: this can easily be reduced to a binary semaphore ([[Mutual exclusion|mutex]]) if desired.
easily be reduced to a binary semaphore ([[Mutual exclusion|mutex]]) if desired.


The semaphore is initialised to a particular value.  Service calls
The semaphore is initialised to a particular value.  Service calls allow the semaphore to be decremented or incremented.
allow the semaphore to be decremented or incremented.


The original semaphores were [https://en.wikipedia.org/wiki/Semaphore_%28programming%29#Operation_names named in Dutch]
The original semaphores were [https://en.wikipedia.org/wiki/Semaphore_%28programming%29#Operation_names named in Dutch]
and the etymology has become confused.  For non-Dutch speakers here we
and the etymology has become confused.  For non-Dutch speakers here we will use English <em>names</em> with the Dutch abbreviations as the latter are widely used.
will use English <em>names</em> with the Dutch abbreviations as the latter
are widely used.


A process wanting a resource calls
A process wanting a resource calls ‘[[Signal and Wait|<strong>wait</strong>]]’ (P).  This uses an [[Atomicity|<strong>atomic</strong>]] operation to decrement the semaphore <strong>if</strong> it is non-zero.  If the semaphore was already zero then the process is (somehow) suspended: let’s assume it is then [[Process_States|<strong>blocked</strong>]] on that semaphore and some other process can be run.
‘[[Signal and Wait|<strong>wait</strong>]]’ (P).  This uses an
[[Atomicity|<strong>atomic</strong>]] operation to decrement the semaphore <strong>if</strong> it
is non-zero.  If the semaphore was already zero then the process is
(somehow) suspended: let’s assume it is then
[[Process_States|<strong>blocked</strong>]] on that semaphore and some other process
can be run.
<blockquote>
<blockquote>
An English mnemonic could be <strong>P</strong>rocure and <strong>V</strong>acate.
An English mnemonic could be <strong>P</strong>rocure and <strong>V</strong>acate.
</blockquote>
</blockquote>
This mechanism allows a maximum number of processes into the critical
This mechanism allows a maximum number of processes into the critical section; that number could be ‘one’ or more, depending on
section; that number could be ‘one’ or more, depending on
the semaphore initialisation.
the semaphore initialisation.


When a process leaves the critical section it must return its token by
When a process leaves the critical section it must return its token by making a [[Signal and Wait|<strong>signal</strong>]] (V) call.  This increments the semaphore; if the semaphore was 0 there may be a process already waiting, in which case the <em>first</em> waiting process can be unblocked. In principle there could be a queue of waiting processes, thus there needs to be a decision: is the queue on a first-come, first-served basis? is there some [[Process_Priority|priority]] ordering? etc.
making a [[Signal and Wait|<strong>signal</strong>]] (V) call.  This increments the
semaphore; if the semaphore was 0 there may be a process already
waiting, in which case the <em>first</em> waiting process can be unblocked.
In principle there could be a queue of waiting processes, thus there
needs to be a decision: is the queue on a first-come, first-served
basis? is there some [[Process_Priority|priority]] ordering? etc.
<!--
<!--
<span style=color:red>
<span style=color:red>
Line 46: Line 27:
-->
-->


Semaphores provide a means of implementing <strong>mutexes</strong> although with a
Semaphores provide a means of implementing <strong>mutexes</strong> although with a counting semaphore no track is being kept of which specific processes own tokens, only how many.
counting semaphore no track is being kept of which specific processes
own tokens, only how many.


==== Practical example (sort of) ====
==== Practical example (sort of) ====
In <em>principle</em> a [[Queues|FIFO queue]] acts as a form of semaphore,
In <em>principle</em> a [[Queues|FIFO queue]] acts as a form of semaphore, albeit controlling data access rather than code.  The input process can put in ‘so many’ elements § before the queue is full and the input is stalled.  This is influenced by the output removing data elements (V).
albeit controlling data access rather than code.  The input process
can put in ‘so many’ elements § before the queue is
full and the input is stalled.  This is influenced by the output
removing data elements (V).
<!-- ** TODO ** interactive demo? -->
<!-- ** TODO ** interactive demo? -->
----
----
<blockquote>
<blockquote>
It is customary to introduce the [[Dining Philosophers' Problem]] at this point.
It is customary to introduce the [[Dining Philosophers Problem]] at this point.
</blockquote>
</blockquote>
----
----


==== Deadlocks ====
==== Deadlocks ====
Acquiring one semaphore is typically safe; acquiring more than one
Acquiring one semaphore is typically safe; acquiring more than one <em>can</em> lead to deadlock.
<em>can</em> lead to deadlock.


Imagine two processes, A and B which both want exclusive access to two
Imagine two processes, A and B which both want exclusive access to two resources, X and Y.
resources, X and Y.


Process A:  <code>P(X); P(Y);</code> <critical region>; <code>V(Y); V(X);</code>
Process A:  <code>P(X); P(Y);</code> <critical region>; <code>V(Y); V(X);</code>
Line 74: Line 47:
Process B:  <code>P(Y); P(X);</code> <critical region>; <code>V(X); V(Y);</code>
Process B:  <code>P(Y); P(X);</code> <critical region>; <code>V(X); V(Y);</code>


with a particular timing sequence, A can acquire X and B can acquire Y
with a particular timing sequence, A can acquire X and B can acquire Y and then neither process can proceed.
and then neither process can proceed.


This is an example of a process [[Deadlock#4|<strong>deadlock</strong>]].
This is an example of a process [[Deadlock#4|<strong>deadlock</strong>]].
Line 81: Line 53:


==== More details and puzzles ====
==== More details and puzzles ====
If you want to study this area in more depth – or just like some
If you want to study this area in more depth – or just like some challenging puzzles – you could consult [https://greenteapress.com/wp/semaphores/ The Little Book of Semaphores] (291 pp.); this goes <em>somewhat</em> beyond the scope of this site!
challenging puzzles – you could consult [https://greenteapress.com/wp/semaphores/ The Little Book of Semaphores] (291 pp.);
this goes <em>somewhat</em> beyond the scope of this site!
----
----
 
{{BookChapter|6.6|272-276}}
{{PageGraph}}
{{PageGraph}}
{{Category|Deadlock}}
{{Category|Deadlock}}

Revision as of 13:06, 4 August 2019

Depends on Mutual exclusionSynchronisationLocks

Semaphores are a means of implementing access control. They are a ‘classic’ mechanism and are employed in operating systems for various purposes. They can be though of as a count of the number of a particular resource which is currently available.

Here is a description of a ‘counting’ semaphore: this can easily be reduced to a binary semaphore (mutex) if desired.

The semaphore is initialised to a particular value. Service calls allow the semaphore to be decremented or incremented.

The original semaphores were named in Dutch and the etymology has become confused. For non-Dutch speakers here we will use English names with the Dutch abbreviations as the latter are widely used.

A process wanting a resource calls ‘wait’ (P). This uses an atomic operation to decrement the semaphore if it is non-zero. If the semaphore was already zero then the process is (somehow) suspended: let’s assume it is then blocked on that semaphore and some other process can be run.

An English mnemonic could be Procure and Vacate.

This mechanism allows a maximum number of processes into the critical section; that number could be ‘one’ or more, depending on the semaphore initialisation.

When a process leaves the critical section it must return its token by making a signal (V) call. This increments the semaphore; if the semaphore was 0 there may be a process already waiting, in which case the first waiting process can be unblocked. In principle there could be a queue of waiting processes, thus there needs to be a decision: is the queue on a first-come, first-served basis? is there some priority ordering? etc.

Semaphores provide a means of implementing mutexes although with a counting semaphore no track is being kept of which specific processes own tokens, only how many.

Practical example (sort of)

In principle a FIFO queue acts as a form of semaphore, albeit controlling data access rather than code. The input process can put in ‘so many’ elements § before the queue is full and the input is stalled. This is influenced by the output removing data elements (V).


It is customary to introduce the Dining Philosophers Problem at this point.


Deadlocks

Acquiring one semaphore is typically safe; acquiring more than one can lead to deadlock.

Imagine two processes, A and B which both want exclusive access to two resources, X and Y.

Process A: P(X); P(Y); <critical region>; V(Y); V(X);

Process B: P(Y); P(X); <critical region>; V(X); V(Y);

with a particular timing sequence, A can acquire X and B can acquire Y and then neither process can proceed.

This is an example of a process deadlock.


More details and puzzles

If you want to study this area in more depth – or just like some challenging puzzles – you could consult The Little Book of Semaphores (291 pp.); this goes somewhat beyond the scope of this site!


Also refer to: Operating System Concepts, 10th Edition: Chapter 6.6, pages 272-276


Articles on Deadlock
Atomicity • Deadlock • File Locking • Locks • Multi Threading • Multiprocessors • Mutual exclusion • Operation Ordering • Race Conditions • Reentrancy • Semaphores • Starvation • Threads