<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
a	O
semaphore	B-Operating_System
is	O
a	O
variable	O
or	O
abstract	O
data	O
type	O
used	O
to	O
control	O
access	O
to	O
a	O
common	O
resource	O
by	O
multiple	O
threads	B-Operating_System
and	O
avoid	O
critical	B-Operating_System
section	I-Operating_System
problems	O
in	O
a	O
concurrent	B-Architecture
system	O
such	O
as	O
a	O
multitasking	B-Operating_System
operating	I-Operating_System
system	I-Operating_System
.	O
</s>
<s>
Semaphores	B-Operating_System
are	O
a	O
type	O
of	O
synchronization	O
primitive	O
.	O
</s>
<s>
A	O
trivial	O
semaphore	B-Operating_System
is	O
a	O
plain	O
variable	O
that	O
is	O
changed	O
(	O
for	O
example	O
,	O
incremented	O
or	O
decremented	O
,	O
or	O
toggled	O
)	O
depending	O
on	O
programmer-defined	O
conditions	O
.	O
</s>
<s>
A	O
useful	O
way	O
to	O
think	O
of	O
a	O
semaphore	B-Operating_System
as	O
used	O
in	O
a	O
real-world	O
system	O
is	O
as	O
a	O
record	O
of	O
how	O
many	O
units	O
of	O
a	O
particular	O
resource	O
are	O
available	O
,	O
coupled	O
with	O
operations	O
to	O
adjust	O
that	O
record	O
safely	O
(	O
i.e.	O
,	O
to	O
avoid	O
race	B-Operating_System
conditions	I-Operating_System
)	O
as	O
units	O
are	O
acquired	O
or	O
become	O
free	O
,	O
and	O
,	O
if	O
necessary	O
,	O
wait	O
until	O
a	O
unit	O
of	O
the	O
resource	O
becomes	O
available	O
.	O
</s>
<s>
Semaphores	B-Operating_System
are	O
a	O
useful	O
tool	O
in	O
the	O
prevention	O
of	O
race	B-Operating_System
conditions	I-Operating_System
;	O
however	O
,	O
their	O
use	O
is	O
not	O
a	O
guarantee	O
that	O
a	O
program	O
is	O
free	O
from	O
these	O
problems	O
.	O
</s>
<s>
Semaphores	B-Operating_System
which	O
allow	O
an	O
arbitrary	O
resource	O
count	O
are	O
called	O
counting	B-Operating_System
semaphores	I-Operating_System
,	O
while	O
semaphores	B-Operating_System
which	O
are	O
restricted	O
to	O
the	O
values	O
0	O
and	O
1	O
(	O
or	O
locked/unlocked	O
,	O
unavailable/available	O
)	O
are	O
called	O
binary	B-Operating_System
semaphores	I-Operating_System
and	O
are	O
used	O
to	O
implement	O
locks	B-Operating_System
.	O
</s>
<s>
The	O
semaphore	B-Operating_System
concept	O
was	O
invented	O
by	O
Dutch	O
computer	O
scientist	O
Edsger	O
Dijkstra	O
in	O
1962	O
or	O
1963	O
,	O
when	O
Dijkstra	O
and	O
his	O
team	O
were	O
developing	O
an	O
operating	B-General_Concept
system	I-General_Concept
for	O
the	O
Electrologica	B-Device
X8	I-Device
.	O
</s>
<s>
That	O
system	O
eventually	O
became	O
known	O
as	O
THE	B-Operating_System
multiprogramming	I-Operating_System
system	I-Operating_System
.	O
</s>
<s>
In	O
this	O
scenario	O
the	O
front	O
desk	O
count-holder	O
represents	O
a	O
counting	B-Operating_System
semaphore	I-Operating_System
,	O
the	O
rooms	O
are	O
the	O
resource	O
,	O
and	O
the	O
students	O
represent	O
processes/threads	O
.	O
</s>
<s>
The	O
value	O
of	O
the	O
semaphore	B-Operating_System
in	O
this	O
scenario	O
is	O
initially	O
10	O
,	O
with	O
all	O
rooms	O
empty	O
.	O
</s>
<s>
When	O
a	O
student	O
requests	O
a	O
room	O
,	O
they	O
are	O
granted	O
access	O
,	O
and	O
the	O
value	O
of	O
the	O
semaphore	B-Operating_System
is	O
changed	O
to	O
9	O
.	O
</s>
<s>
If	O
someone	O
requests	O
a	O
room	O
and	O
the	O
current	O
value	O
of	O
the	O
semaphore	B-Operating_System
is	O
0	O
,	O
they	O
are	O
forced	O
to	O
wait	O
until	O
a	O
room	O
is	O
freed	O
(	O
when	O
the	O
count	O
is	O
increased	O
from	O
0	O
)	O
.	O
</s>
<s>
If	O
one	O
of	O
the	O
rooms	O
was	O
released	O
,	O
but	O
there	O
are	O
several	O
students	O
waiting	O
,	O
then	O
any	O
method	O
can	O
be	O
used	O
to	O
select	O
the	O
one	O
who	O
will	O
occupy	O
the	O
room	O
(	O
like	O
FIFO	B-Operating_System
or	O
randomly	O
picking	O
one	O
)	O
.	O
</s>
<s>
When	O
used	O
to	O
control	O
access	O
to	O
a	O
pool	B-General_Concept
of	O
resources	O
,	O
a	O
semaphore	B-Operating_System
tracks	O
only	O
how	O
many	O
resources	O
are	O
free	O
;	O
it	O
does	O
not	O
keep	O
track	O
of	O
which	O
of	O
the	O
resources	O
are	O
free	O
.	O
</s>
<s>
Some	O
other	O
mechanism	O
(	O
possibly	O
involving	O
more	O
semaphores	B-Operating_System
)	O
may	O
be	O
required	O
to	O
select	O
a	O
particular	O
free	O
resource	O
.	O
</s>
<s>
The	O
paradigm	O
is	O
especially	O
powerful	O
because	O
the	O
semaphore	B-Operating_System
count	O
may	O
serve	O
as	O
a	O
useful	O
trigger	O
for	O
a	O
number	O
of	O
different	O
actions	O
.	O
</s>
<s>
Fairness	O
and	O
safety	O
are	O
likely	O
to	O
be	O
compromised	O
(	O
which	O
practically	O
means	O
a	O
program	O
may	O
behave	O
slowly	O
,	O
act	O
erratically	O
,	O
hang	B-General_Concept
or	O
crash	B-General_Concept
)	O
if	O
even	O
a	O
single	O
process	O
acts	O
incorrectly	O
.	O
</s>
<s>
Even	O
if	O
all	O
processes	O
follow	O
these	O
rules	O
,	O
multi-resource	O
deadlock	B-Operating_System
may	O
still	O
occur	O
when	O
there	O
are	O
different	O
resources	O
managed	O
by	O
different	O
semaphores	B-Operating_System
and	O
when	O
processes	O
need	O
to	O
use	O
more	O
than	O
one	O
resource	O
at	O
a	O
time	O
,	O
as	O
illustrated	O
by	O
the	O
dining	O
philosophers	O
problem	O
.	O
</s>
<s>
Counting	B-Operating_System
semaphores	I-Operating_System
are	O
equipped	O
with	O
two	O
operations	O
,	O
historically	O
denoted	O
as	O
P	O
and	O
V	O
(	O
see	O
for	O
alternative	O
names	O
)	O
.	O
</s>
<s>
Operation	O
V	O
increments	O
the	O
semaphore	B-Operating_System
S	O
,	O
and	O
operation	O
P	O
decrements	O
it	O
.	O
</s>
<s>
The	O
value	O
of	O
the	O
semaphore	B-Operating_System
S	O
is	O
the	O
number	O
of	O
units	O
of	O
the	O
resource	O
that	O
are	O
currently	O
available	O
.	O
</s>
<s>
The	O
P	O
operation	O
wastes	B-Operating_System
time	I-Operating_System
or	O
sleeps	B-Operating_System
until	O
a	O
resource	O
protected	O
by	O
the	O
semaphore	B-Operating_System
becomes	O
available	O
,	O
at	O
which	O
time	O
the	O
resource	O
is	O
immediately	O
claimed	O
.	O
</s>
<s>
One	O
important	O
property	O
of	O
semaphore	B-Operating_System
S	O
is	O
that	O
its	O
value	O
cannot	O
be	O
changed	O
except	O
by	O
using	O
the	O
V	O
and	O
P	O
operations	O
.	O
</s>
<s>
:	O
Decrements	O
the	O
value	O
of	O
semaphore	B-Operating_System
variable	O
by	O
1	O
.	O
</s>
<s>
If	O
the	O
new	O
value	O
of	O
the	O
semaphore	B-Operating_System
variable	O
is	O
negative	O
,	O
the	O
process	O
executing	O
is	O
blocked	O
(	O
i.e.	O
,	O
added	O
to	O
the	O
semaphore	B-Operating_System
's	O
queue	B-Application
)	O
.	O
</s>
<s>
:	O
Increments	O
the	O
value	O
of	O
semaphore	B-Operating_System
variable	O
by	O
1	O
.	O
</s>
<s>
After	O
the	O
increment	O
,	O
if	O
the	O
pre-increment	O
value	O
was	O
negative	O
(	O
meaning	O
there	O
are	O
processes	O
waiting	O
for	O
a	O
resource	O
)	O
,	O
it	O
transfers	O
a	O
blocked	O
process	O
from	O
the	O
semaphore	B-Operating_System
's	O
waiting	O
queue	B-Application
to	O
the	O
ready	O
queue	B-Application
.	O
</s>
<s>
Many	O
operating	B-General_Concept
systems	I-General_Concept
provide	O
efficient	O
semaphore	B-Operating_System
primitives	O
that	O
unblock	O
a	O
waiting	O
process	O
when	O
the	O
semaphore	B-Operating_System
is	O
incremented	O
.	O
</s>
<s>
This	O
means	O
that	O
processes	O
do	O
not	O
waste	O
time	O
checking	O
the	O
semaphore	B-Operating_System
value	O
unnecessarily	O
.	O
</s>
<s>
The	O
counting	B-Operating_System
semaphore	I-Operating_System
concept	O
can	O
be	O
extended	O
with	O
the	O
ability	O
to	O
claim	O
or	O
return	O
more	O
than	O
one	O
"	O
unit	O
"	O
from	O
the	O
semaphore	B-Operating_System
,	O
a	O
technique	O
implemented	O
in	O
Unix	B-Application
.	O
</s>
<s>
The	O
modified	O
V	O
and	O
P	O
operations	O
are	O
as	O
follows	O
,	O
using	O
square	O
brackets	O
to	O
indicate	O
atomic	B-General_Concept
operations	I-General_Concept
,	O
i.e.	O
,	O
operations	O
which	O
appear	O
indivisible	O
from	O
the	O
perspective	O
of	O
other	O
processes	O
:	O
</s>
<s>
function	O
V(semaphore S, integer I )	O
:	O
</s>
<s>
function	O
P(semaphore S, integer I )	O
:	O
</s>
<s>
However	O
,	O
the	O
remainder	O
of	O
this	O
section	O
refers	O
to	O
semaphores	B-Operating_System
with	O
unary	O
V	O
and	O
P	O
operations	O
,	O
unless	O
otherwise	O
specified	O
.	O
</s>
<s>
To	O
avoid	O
starvation	B-Operating_System
,	O
a	O
semaphore	B-Operating_System
has	O
an	O
associated	O
queue	B-Application
of	O
processes	O
(	O
usually	O
with	O
FIFO	B-Operating_System
semantics	O
)	O
.	O
</s>
<s>
If	O
a	O
process	O
performs	O
a	O
P	O
operation	O
on	O
a	O
semaphore	B-Operating_System
that	O
has	O
the	O
value	O
zero	O
,	O
the	O
process	O
is	O
added	O
to	O
the	O
semaphore	B-Operating_System
's	O
queue	B-Application
and	O
its	O
execution	O
is	O
suspended	O
.	O
</s>
<s>
When	O
another	O
process	O
increments	O
the	O
semaphore	B-Operating_System
by	O
performing	O
a	O
V	O
operation	O
,	O
and	O
there	O
are	O
processes	O
on	O
the	O
queue	B-Application
,	O
one	O
of	O
them	O
is	O
removed	O
from	O
the	O
queue	B-Application
and	O
resumes	O
execution	O
.	O
</s>
<s>
When	O
processes	O
have	O
different	O
priorities	O
the	O
queue	B-Application
may	O
be	O
ordered	O
by	O
priority	O
,	O
so	O
that	O
the	O
highest	O
priority	O
process	O
is	O
taken	O
from	O
the	O
queue	B-Application
first	O
.	O
</s>
<s>
If	O
the	O
implementation	O
does	O
not	O
ensure	O
atomicity	B-General_Concept
of	O
the	O
increment	O
,	O
decrement	O
and	O
comparison	O
operations	O
,	O
then	O
there	O
is	O
a	O
risk	O
of	O
increments	O
or	O
decrements	O
being	O
forgotten	O
,	O
or	O
of	O
the	O
semaphore	B-Operating_System
value	O
becoming	O
negative	O
.	O
</s>
<s>
Atomicity	B-General_Concept
may	O
be	O
achieved	O
by	O
using	O
a	O
machine	O
instruction	O
that	O
is	O
able	O
to	O
read	B-Operating_System
,	I-Operating_System
modify	I-Operating_System
and	I-Operating_System
write	I-Operating_System
the	O
semaphore	B-Operating_System
in	O
a	O
single	O
operation	O
.	O
</s>
<s>
In	O
the	O
absence	O
of	O
such	O
a	O
hardware	O
instruction	O
,	O
an	O
atomic	B-General_Concept
operation	I-General_Concept
may	O
be	O
synthesized	O
through	O
the	O
use	O
of	O
a	O
software	O
mutual	B-Operating_System
exclusion	I-Operating_System
algorithm	O
.	O
</s>
<s>
On	O
uniprocessor	B-Operating_System
systems	I-Operating_System
,	O
atomic	B-General_Concept
operations	I-General_Concept
can	O
be	O
ensured	O
by	O
temporarily	O
suspending	O
preemption	B-Operating_System
or	O
disabling	O
hardware	O
interrupts	B-Application
.	O
</s>
<s>
This	O
approach	O
does	O
not	O
work	O
on	O
multiprocessor	O
systems	O
where	O
it	O
is	O
possible	O
for	O
two	O
programs	O
sharing	O
a	O
semaphore	B-Operating_System
to	O
run	O
on	O
different	O
processors	O
at	O
the	O
same	O
time	O
.	O
</s>
<s>
To	O
solve	O
this	O
problem	O
in	O
a	O
multiprocessor	O
system	O
a	O
locking	B-Operating_System
variable	O
can	O
be	O
used	O
to	O
control	O
access	O
to	O
the	O
semaphore	B-Operating_System
.	O
</s>
<s>
The	O
locking	B-Operating_System
variable	O
is	O
manipulated	O
using	O
a	O
test-and-set-lock	B-Operating_System
command	O
.	O
</s>
<s>
Thus	O
,	O
S	O
is	O
a	O
semaphore	B-Operating_System
for	O
A	O
.	O
</s>
<s>
Whenever	O
a	O
user	O
logs	O
in	O
,	O
P	O
is	O
called	O
,	O
decrementing	O
the	O
semaphore	B-Operating_System
S	O
by	O
1	O
.	O
</s>
<s>
When	O
S	O
is	O
0	O
,	O
any	O
users	O
wishing	O
to	O
log	O
in	O
must	O
wait	O
until	O
S	O
increases	O
;	O
the	O
login	O
request	O
is	O
enqueued	O
onto	O
a	O
FIFO	B-Operating_System
queue	B-Application
until	O
a	O
slot	O
is	O
freed	O
.	O
</s>
<s>
Mutual	B-Operating_System
exclusion	I-Operating_System
is	O
used	O
to	O
ensure	O
that	O
requests	O
are	O
enqueued	O
in	O
order	O
.	O
</s>
<s>
They	O
communicate	O
using	O
a	O
queue	B-Application
of	O
maximum	O
size	O
N	O
and	O
are	O
subject	O
to	O
the	O
following	O
conditions	O
:	O
</s>
<s>
the	O
consumer	O
must	O
wait	O
for	O
the	O
producer	O
to	O
produce	O
something	O
if	O
the	O
queue	B-Application
is	O
empty	O
;	O
</s>
<s>
the	O
producer	O
must	O
wait	O
for	O
the	O
consumer	O
to	O
consume	O
something	O
if	O
the	O
queue	B-Application
is	O
full	O
.	O
</s>
<s>
The	O
semaphore	B-Operating_System
solution	O
to	O
the	O
producer	O
–	O
consumer	O
problem	O
tracks	O
the	O
state	O
of	O
the	O
queue	B-Application
with	O
two	O
semaphores	B-Operating_System
:	O
emptyCount	O
,	O
the	O
number	O
of	O
empty	O
places	O
in	O
the	O
queue	B-Application
,	O
and	O
fullCount	O
,	O
the	O
number	O
of	O
elements	O
in	O
the	O
queue	B-Application
.	O
</s>
<s>
To	O
maintain	O
integrity	O
,	O
emptyCount	O
may	O
be	O
lower	O
(	O
but	O
never	O
higher	O
)	O
than	O
the	O
actual	O
number	O
of	O
empty	O
places	O
in	O
the	O
queue	B-Application
,	O
and	O
fullCount	O
may	O
be	O
lower	O
(	O
but	O
never	O
higher	O
)	O
than	O
the	O
actual	O
number	O
of	O
items	O
in	O
the	O
queue	B-Application
.	O
</s>
<s>
Empty	O
places	O
and	O
items	O
represent	O
two	O
kinds	O
of	O
resources	O
,	O
empty	O
boxes	O
and	O
full	O
boxes	O
,	O
and	O
the	O
semaphores	B-Operating_System
emptyCount	O
and	O
fullCount	O
maintain	O
control	O
over	O
these	O
resources	O
.	O
</s>
<s>
The	O
binary	B-Operating_System
semaphore	I-Operating_System
useQueue	O
ensures	O
that	O
the	O
integrity	O
of	O
the	O
state	O
of	O
the	O
queue	B-Application
itself	O
is	O
not	O
compromised	O
,	O
for	O
example	O
by	O
two	O
producers	O
attempting	O
to	O
add	O
items	O
to	O
an	O
empty	O
queue	B-Application
simultaneously	O
,	O
thereby	O
corrupting	O
its	O
internal	O
state	O
.	O
</s>
<s>
Alternatively	O
a	O
mutex	B-Operating_System
could	O
be	O
used	O
in	O
place	O
of	O
the	O
binary	B-Operating_System
semaphore	I-Operating_System
.	O
</s>
<s>
A	O
single	O
consumer	O
enters	O
its	O
critical	B-Operating_System
section	I-Operating_System
.	O
</s>
<s>
Several	O
producers	O
enter	O
the	O
producer	O
critical	B-Operating_System
section	I-Operating_System
.	O
</s>
<s>
No	O
more	O
than	O
N	O
producers	O
may	O
enter	O
their	O
critical	B-Operating_System
section	I-Operating_System
due	O
to	O
emptyCount	O
constraining	O
their	O
entry	O
.	O
</s>
<s>
The	O
producers	O
,	O
one	O
at	O
a	O
time	O
,	O
gain	O
access	O
to	O
the	O
queue	B-Application
through	O
useQueue	O
and	O
deposit	O
items	O
in	O
the	O
queue	B-Application
.	O
</s>
<s>
Once	O
the	O
first	O
producer	O
exits	O
its	O
critical	B-Operating_System
section	I-Operating_System
,	O
fullCount	O
is	O
incremented	O
,	O
allowing	O
one	O
consumer	O
to	O
enter	O
its	O
critical	B-Operating_System
section	I-Operating_System
.	O
</s>
<s>
Note	O
that	O
emptyCount	O
may	O
be	O
much	O
lower	O
than	O
the	O
actual	O
number	O
of	O
empty	O
places	O
in	O
the	O
queue	B-Application
,	O
for	O
example	O
in	O
the	O
case	O
where	O
many	O
producers	O
have	O
decremented	O
it	O
but	O
are	O
waiting	O
their	O
turn	O
on	O
useQueue	O
before	O
filling	O
empty	O
places	O
.	O
</s>
<s>
Note	O
that	O
emptyCount	O
+	O
fullCount	O
≤	O
N	O
always	O
holds	O
,	O
with	O
equality	O
if	O
and	O
only	O
if	O
no	O
producers	O
or	O
consumers	O
are	O
executing	O
their	O
critical	B-Operating_System
sections	I-Operating_System
.	O
</s>
<s>
In	O
ALGOL	B-Language
68	I-Language
,	O
the	O
Linux	B-Operating_System
kernel	I-Operating_System
,	O
and	O
in	O
some	O
English	O
textbooks	O
,	O
the	O
V	O
and	O
P	O
operations	O
are	O
called	O
,	O
respectively	O
,	O
up	O
and	O
down	O
.	O
</s>
<s>
In	O
software	O
engineering	O
practice	O
,	O
they	O
are	O
often	O
called	O
signal	O
and	O
wait	O
,	O
release	O
and	O
acquire	O
(	O
which	O
the	O
standard	O
Java	B-Language
library	O
uses	O
)	O
,	O
or	O
post	O
and	O
pend	O
.	O
</s>
<s>
A	O
mutex	B-Operating_System
is	O
a	O
locking	B-Operating_System
mechanism	O
that	O
sometimes	O
uses	O
the	O
same	O
basic	O
implementation	O
as	O
the	O
binary	B-Operating_System
semaphore	I-Operating_System
.	O
</s>
<s>
While	O
a	O
binary	B-Operating_System
semaphore	I-Operating_System
may	O
be	O
colloquially	O
referred	O
to	O
as	O
a	O
mutex	B-Operating_System
,	O
a	O
true	O
mutex	B-Operating_System
has	O
a	O
more	O
specific	O
use-case	O
and	O
definition	O
,	O
in	O
that	O
only	O
the	O
task	B-General_Concept
that	O
locked	O
the	O
mutex	B-Operating_System
is	O
supposed	O
to	O
unlock	O
it	O
.	O
</s>
<s>
This	O
constraint	O
aims	O
to	O
handle	O
some	O
potential	O
problems	O
of	O
using	O
semaphores	B-Operating_System
:	O
</s>
<s>
Priority	B-Operating_System
inversion	I-Operating_System
:	O
If	O
the	O
mutex	B-Operating_System
knows	O
who	O
locked	O
it	O
and	O
is	O
supposed	O
to	O
unlock	O
it	O
,	O
it	O
is	O
possible	O
to	O
promote	O
the	O
priority	O
of	O
that	O
task	B-General_Concept
whenever	O
a	O
higher-priority	O
task	B-General_Concept
starts	O
waiting	O
on	O
the	O
mutex	B-Operating_System
.	O
</s>
<s>
Premature	O
task	B-General_Concept
termination	O
:	O
Mutexes	B-Operating_System
may	O
also	O
provide	O
deletion	O
safety	O
,	O
where	O
the	O
task	B-General_Concept
holding	O
the	O
mutex	B-Operating_System
cannot	O
be	O
accidentally	O
deleted	O
.	O
</s>
<s>
Termination	O
deadlock	B-Operating_System
:	O
If	O
a	O
mutex-holding	O
task	B-General_Concept
terminates	O
for	O
any	O
reason	O
,	O
the	B-Operating_System
OS	I-Operating_System
can	O
release	O
the	O
mutex	B-Operating_System
and	O
signal	O
waiting	O
tasks	O
of	O
this	O
condition	O
.	O
</s>
<s>
Recursion	O
deadlock	B-Operating_System
:	O
a	O
task	B-General_Concept
is	O
allowed	O
to	O
lock	O
a	O
reentrant	B-Operating_System
mutex	I-Operating_System
multiple	O
times	O
as	O
it	O
unlocks	O
it	O
an	O
equal	O
number	O
of	O
times	O
.	O
</s>
<s>
Accidental	O
release	O
:	O
An	O
error	O
is	O
raised	O
on	O
the	O
release	O
of	O
the	O
mutex	B-Operating_System
if	O
the	O
releasing	O
task	B-General_Concept
is	O
not	O
its	O
owner	O
.	O
</s>
