<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
mutual	B-Operating_System
exclusion	I-Operating_System
is	O
a	O
property	O
of	O
concurrency	B-Operating_System
control	I-Operating_System
,	O
which	O
is	O
instituted	O
for	O
the	O
purpose	O
of	O
preventing	O
race	B-Operating_System
conditions	I-Operating_System
.	O
</s>
<s>
It	O
is	O
the	O
requirement	O
that	O
one	O
thread	B-Operating_System
of	I-Operating_System
execution	I-Operating_System
never	O
enters	O
a	O
critical	B-Operating_System
section	I-Operating_System
while	O
a	O
concurrent	B-Architecture
thread	B-Operating_System
of	I-Operating_System
execution	I-Operating_System
is	O
already	O
accessing	O
said	O
critical	B-Operating_System
section	I-Operating_System
,	O
which	O
refers	O
to	O
an	O
interval	O
of	O
time	O
during	O
which	O
a	O
thread	B-Operating_System
of	I-Operating_System
execution	I-Operating_System
accesses	O
a	O
shared	B-General_Concept
resource	I-General_Concept
or	O
shared	B-Operating_System
memory	I-Operating_System
.	O
</s>
<s>
The	O
shared	B-General_Concept
resource	I-General_Concept
is	O
a	O
data	O
object	O
,	O
which	O
two	O
or	O
more	O
concurrent	B-Architecture
threads	B-Operating_System
are	O
trying	O
to	O
modify	O
(	O
where	O
two	O
concurrent	B-Architecture
read	O
operations	O
are	O
permitted	O
but	O
,	O
no	O
two	O
concurrent	B-Architecture
write	O
operations	O
or	O
one	O
read	O
and	O
one	O
write	O
are	O
permitted	O
,	O
since	O
it	O
leads	O
to	O
data	O
inconsistency	O
)	O
.	O
</s>
<s>
Mutual	B-Operating_System
exclusion	I-Operating_System
algorithms	O
ensure	O
that	O
if	O
a	O
process	O
is	O
already	O
performing	O
write	O
operation	O
on	O
a	O
data	O
object	O
[	O
critical	O
section ]	O
no	O
other	O
process/thread	O
is	O
allowed	O
to	O
access/modify	O
the	O
same	O
object	O
until	O
the	O
first	O
process	O
has	O
finished	O
writing	O
upon	O
the	O
data	O
object	O
[	O
critical	O
section ]	O
and	O
released	O
the	O
object	O
for	O
other	O
processes	O
to	O
read	O
and	O
write	O
upon	O
.	O
</s>
<s>
The	O
requirement	O
of	O
mutual	B-Operating_System
exclusion	I-Operating_System
was	O
first	O
identified	O
and	O
solved	O
by	O
Edsger	O
W	O
.	O
Dijkstra	O
in	O
his	O
seminal	O
1965	O
paper	O
"	O
Solution	O
of	O
a	O
problem	O
in	O
concurrent	B-Architecture
programming	I-Architecture
control	O
"	O
,	O
which	O
is	O
credited	O
as	O
the	O
first	O
topic	O
in	O
the	O
study	O
of	O
concurrent	B-Architecture
algorithms	I-Architecture
.	O
</s>
<s>
A	O
simple	O
example	O
of	O
why	O
mutual	B-Operating_System
exclusion	I-Operating_System
is	O
important	O
in	O
practice	O
can	O
be	O
visualized	O
using	O
a	O
singly	O
linked	B-Data_Structure
list	I-Data_Structure
of	O
four	O
items	O
,	O
where	O
the	O
second	O
and	O
third	O
are	O
to	O
be	O
removed	O
.	O
</s>
<s>
The	O
removal	O
of	O
a	O
node	O
that	O
sits	O
between	O
two	O
other	O
nodes	O
is	O
performed	O
by	O
changing	O
the	O
next	O
pointer	O
of	O
the	O
previous	O
node	O
to	O
point	O
to	O
the	O
next	O
node	O
(	O
in	O
other	O
words	O
,	O
if	O
node	O
is	O
being	O
removed	O
,	O
then	O
the	O
next	O
pointer	O
of	O
node	O
is	O
changed	O
to	O
point	O
to	O
node	O
,	O
thereby	O
removing	O
from	O
the	O
linked	B-Data_Structure
list	I-Data_Structure
any	O
reference	O
to	O
node	O
)	O
.	O
</s>
<s>
When	O
such	O
a	O
linked	B-Data_Structure
list	I-Data_Structure
is	O
being	O
shared	O
between	O
multiple	O
threads	B-Operating_System
of	O
execution	O
,	O
two	O
threads	B-Operating_System
of	O
execution	O
may	O
attempt	O
to	O
remove	O
two	O
different	O
nodes	O
simultaneously	O
,	O
one	O
thread	B-Operating_System
of	I-Operating_System
execution	I-Operating_System
changing	O
the	O
next	O
pointer	O
of	O
node	O
to	O
point	O
to	O
node	O
,	O
while	O
another	O
thread	B-Operating_System
of	I-Operating_System
execution	I-Operating_System
changes	O
the	O
next	O
pointer	O
of	O
node	O
to	O
point	O
to	O
node	O
.	O
</s>
<s>
Although	O
both	O
removal	O
operations	O
complete	O
successfully	O
,	O
the	O
desired	O
state	O
of	O
the	O
linked	B-Data_Structure
list	I-Data_Structure
is	O
not	O
achieved	O
:	O
node	O
remains	O
in	O
the	O
list	O
,	O
because	O
the	O
next	O
pointer	O
of	O
node	O
points	O
to	O
node	O
.	O
</s>
<s>
This	O
problem	O
(	O
called	O
a	O
race	B-Operating_System
condition	I-Operating_System
)	O
can	O
be	O
avoided	O
by	O
using	O
the	O
requirement	O
of	O
mutual	B-Operating_System
exclusion	I-Operating_System
to	O
ensure	O
that	O
simultaneous	O
updates	O
to	O
the	O
same	O
part	O
of	O
the	O
list	O
cannot	O
occur	O
.	O
</s>
<s>
The	O
term	O
mutual	B-Operating_System
exclusion	I-Operating_System
is	O
also	O
used	O
in	O
reference	O
to	O
the	O
simultaneous	O
writing	O
of	O
a	O
memory	O
address	O
by	O
one	O
thread	B-Operating_System
while	O
the	O
aforementioned	O
memory	O
address	O
is	O
being	O
manipulated	O
or	O
read	O
by	O
one	O
or	O
more	O
other	O
threads	B-Operating_System
.	O
</s>
<s>
The	O
problem	O
which	O
mutual	B-Operating_System
exclusion	I-Operating_System
addresses	O
is	O
a	O
problem	O
of	O
resource	B-General_Concept
sharing	I-General_Concept
:	O
how	O
can	O
a	O
software	O
system	O
control	O
multiple	O
processes	O
 '	O
access	O
to	O
a	O
shared	B-General_Concept
resource	I-General_Concept
,	O
when	O
each	O
process	O
needs	O
exclusive	O
control	O
of	O
that	O
resource	O
while	O
doing	O
its	O
work	O
?	O
</s>
<s>
The	O
mutual-exclusion	O
solution	O
to	O
this	O
makes	O
the	O
shared	B-General_Concept
resource	I-General_Concept
available	O
only	O
while	O
the	O
process	O
is	O
in	O
a	O
specific	O
code	O
segment	O
called	O
the	O
critical	B-Operating_System
section	I-Operating_System
.	O
</s>
<s>
It	O
controls	O
access	O
to	O
the	O
shared	B-General_Concept
resource	I-General_Concept
by	O
controlling	O
each	O
mutual	O
execution	O
of	O
that	O
part	O
of	O
its	O
program	O
where	O
the	O
resource	O
would	O
be	O
used	O
.	O
</s>
<s>
It	O
must	O
implement	O
mutual	B-Operating_System
exclusion	I-Operating_System
:	O
only	O
one	O
process	O
can	O
be	O
in	O
the	O
critical	B-Operating_System
section	I-Operating_System
at	O
a	O
time	O
.	O
</s>
<s>
It	O
must	O
be	O
free	O
of	O
deadlocks	B-Operating_System
:	O
if	O
processes	O
are	O
trying	O
to	O
enter	O
the	O
critical	B-Operating_System
section	I-Operating_System
,	O
one	O
of	O
them	O
must	O
eventually	O
be	O
able	O
to	O
do	O
so	O
successfully	O
,	O
provided	O
no	O
process	O
stays	O
in	O
the	O
critical	B-Operating_System
section	I-Operating_System
permanently	O
.	O
</s>
<s>
Deadlock	B-Operating_System
freedom	O
can	O
be	O
expanded	O
to	O
implement	O
one	O
or	O
both	O
of	O
these	O
properties	O
:	O
</s>
<s>
Lockout-freedom	O
guarantees	O
that	O
any	O
process	O
wishing	O
to	O
enter	O
the	O
critical	B-Operating_System
section	I-Operating_System
will	O
be	O
able	O
to	O
do	O
so	O
eventually	O
.	O
</s>
<s>
This	O
is	O
distinct	O
from	O
deadlock	B-Operating_System
avoidance	O
,	O
which	O
requires	O
that	O
some	O
waiting	O
process	O
be	O
able	O
to	O
get	O
access	O
to	O
the	O
critical	B-Operating_System
section	I-Operating_System
,	O
but	O
does	O
not	O
require	O
that	O
every	O
process	O
gets	O
a	O
turn	O
.	O
</s>
<s>
If	O
two	O
processes	O
continually	O
trade	O
a	O
resource	O
between	O
them	O
,	O
a	O
third	O
process	O
could	O
be	O
locked	O
out	O
and	O
experience	O
resource	B-Operating_System
starvation	I-Operating_System
,	O
even	O
though	O
the	O
system	O
is	O
not	O
in	O
deadlock	B-Operating_System
.	O
</s>
<s>
Lockout-freedom	O
ensures	O
every	O
process	O
can	O
access	O
the	O
critical	B-Operating_System
section	I-Operating_System
eventually	O
:	O
it	O
gives	O
no	O
guarantee	O
about	O
how	O
long	O
the	O
wait	O
will	O
be	O
.	O
</s>
<s>
This	O
works	O
by	O
setting	O
a	O
limit	O
to	O
the	O
number	O
of	O
times	O
other	O
processes	O
can	O
cut	O
in	O
line	O
,	O
so	O
that	O
no	O
process	O
can	O
enter	O
the	O
critical	B-Operating_System
section	I-Operating_System
more	O
than	O
k	O
times	O
while	O
another	O
is	O
waiting	O
.	O
</s>
<s>
Non-Critical	O
Section	O
Operation	O
is	O
outside	O
the	O
critical	B-Operating_System
section	I-Operating_System
;	O
the	O
process	O
is	O
not	O
using	O
or	O
requesting	O
the	O
shared	B-General_Concept
resource	I-General_Concept
.	O
</s>
<s>
Trying	O
The	O
process	O
attempts	O
to	O
enter	O
the	O
critical	B-Operating_System
section	I-Operating_System
.	O
</s>
<s>
Critical	B-Operating_System
Section	I-Operating_System
The	O
process	O
is	O
allowed	O
to	O
access	O
the	O
shared	B-General_Concept
resource	I-General_Concept
in	O
this	O
section	O
.	O
</s>
<s>
Exit	O
The	O
process	O
leaves	O
the	O
critical	B-Operating_System
section	I-Operating_System
and	O
makes	O
the	O
shared	B-General_Concept
resource	I-General_Concept
available	O
to	O
other	O
processes	O
.	O
</s>
<s>
If	O
a	O
process	O
wishes	O
to	O
enter	O
the	O
critical	B-Operating_System
section	I-Operating_System
,	O
it	O
must	O
first	O
execute	O
the	O
trying	O
section	O
and	O
wait	O
until	O
it	O
acquires	O
access	O
to	O
the	O
critical	B-Operating_System
section	I-Operating_System
.	O
</s>
<s>
After	O
the	O
process	O
has	O
executed	O
its	O
critical	B-Operating_System
section	I-Operating_System
and	O
is	O
finished	O
with	O
the	O
shared	B-General_Concept
resources	I-General_Concept
,	O
it	O
needs	O
to	O
execute	O
the	O
exit	O
section	O
to	O
release	O
them	O
for	O
other	O
processes	O
 '	O
use	O
.	O
</s>
<s>
On	O
uni-processor	B-Operating_System
systems	O
,	O
the	O
simplest	O
solution	O
to	O
achieve	O
mutual	B-Operating_System
exclusion	I-Operating_System
is	O
to	O
disable	O
interrupts	B-Application
during	O
a	O
process	O
's	O
critical	B-Operating_System
section	I-Operating_System
.	O
</s>
<s>
This	O
will	O
prevent	O
any	O
interrupt	B-General_Concept
service	I-General_Concept
routines	I-General_Concept
from	O
running	O
(	O
effectively	O
preventing	O
a	O
process	O
from	O
being	O
preempted	B-Operating_System
)	O
.	O
</s>
<s>
If	O
a	O
critical	B-Operating_System
section	I-Operating_System
is	O
long	O
,	O
then	O
the	O
system	B-Operating_System
clock	I-Operating_System
will	O
drift	O
every	O
time	O
a	O
critical	B-Operating_System
section	I-Operating_System
is	O
executed	O
because	O
the	O
timer	O
interrupt	B-Application
is	O
no	O
longer	O
serviced	O
,	O
so	O
tracking	O
time	O
is	O
impossible	O
during	O
the	O
critical	B-Operating_System
section	I-Operating_System
.	O
</s>
<s>
Also	O
,	O
if	O
a	O
process	O
halts	O
during	O
its	O
critical	B-Operating_System
section	I-Operating_System
,	O
control	O
will	O
never	O
be	O
returned	O
to	O
another	O
process	O
,	O
effectively	O
halting	O
the	O
entire	O
system	O
.	O
</s>
<s>
A	O
more	O
elegant	O
method	O
for	O
achieving	O
mutual	B-Operating_System
exclusion	I-Operating_System
is	O
the	O
busy-wait	B-Operating_System
.	O
</s>
<s>
Busy-waiting	B-Operating_System
is	O
effective	O
for	O
both	O
uniprocessor	B-Operating_System
and	O
multiprocessor	B-Operating_System
systems	O
.	O
</s>
<s>
The	O
use	O
of	O
shared	B-Operating_System
memory	I-Operating_System
and	O
an	O
atomic	B-General_Concept
test-and-set	B-Operating_System
instruction	O
provide	O
the	O
mutual	B-Operating_System
exclusion	I-Operating_System
.	O
</s>
<s>
A	O
process	O
can	O
test-and-set	B-Operating_System
on	O
a	O
location	O
in	O
shared	B-Operating_System
memory	I-Operating_System
,	O
and	O
since	O
the	O
operation	O
is	O
atomic	B-General_Concept
,	O
only	O
one	O
process	O
can	O
set	O
the	O
flag	O
at	O
a	O
time	O
.	O
</s>
<s>
Preemption	B-Operating_System
is	O
still	O
possible	O
,	O
so	O
this	O
method	O
allows	O
the	O
system	O
to	O
continue	O
to	O
function	O
—	O
even	O
if	O
a	O
process	O
halts	O
while	O
holding	O
the	O
lock	B-Operating_System
.	O
</s>
<s>
Several	O
other	O
atomic	B-General_Concept
operations	I-General_Concept
can	O
be	O
used	O
to	O
provide	O
mutual	B-Operating_System
exclusion	I-Operating_System
of	O
data	O
structures	O
;	O
most	O
notable	O
of	O
these	O
is	O
compare-and-swap	B-Operating_System
(	O
CAS	O
)	O
.	O
</s>
<s>
CAS	O
can	O
be	O
used	O
to	O
achieve	O
wait-free	B-Operating_System
mutual	B-Operating_System
exclusion	I-Operating_System
for	O
any	O
shared	B-Operating_System
data	I-Operating_System
structure	O
by	O
creating	O
a	O
linked	B-Data_Structure
list	I-Data_Structure
where	O
each	O
node	O
represents	O
the	O
desired	O
operation	O
to	O
be	O
performed	O
.	O
</s>
<s>
CAS	O
is	O
then	O
used	O
to	O
change	O
the	O
pointers	O
in	O
the	O
linked	B-Data_Structure
list	I-Data_Structure
during	O
the	O
insertion	O
of	O
a	O
new	O
node	O
.	O
</s>
<s>
Each	O
process	O
can	O
then	O
keep	O
a	O
local	O
copy	O
of	O
the	O
data	O
structure	O
,	O
and	O
upon	O
traversing	O
the	O
linked	B-Data_Structure
list	I-Data_Structure
,	O
can	O
perform	O
each	O
operation	O
from	O
the	O
list	O
on	O
its	O
local	O
copy	O
.	O
</s>
<s>
In	O
addition	O
to	O
hardware-supported	O
solutions	O
,	O
some	O
software	O
solutions	O
exist	O
that	O
use	O
busy	B-Operating_System
waiting	I-Operating_System
to	O
achieve	O
mutual	B-Operating_System
exclusion	I-Operating_System
.	O
</s>
<s>
These	O
algorithms	O
do	O
not	O
work	O
if	O
out-of-order	B-General_Concept
execution	I-General_Concept
is	O
used	O
on	O
the	O
platform	O
that	O
executes	O
them	O
.	O
</s>
<s>
Programmers	O
have	O
to	O
specify	O
strict	O
ordering	O
on	O
the	O
memory	O
operations	O
within	O
a	O
thread	B-Operating_System
.	O
</s>
<s>
It	O
is	O
often	O
preferable	O
to	O
use	O
synchronization	O
facilities	O
provided	O
by	O
an	O
operating	B-General_Concept
system	I-General_Concept
's	O
multithreading	B-Operating_System
library	O
,	O
which	O
will	O
take	O
advantage	O
of	O
hardware	O
solutions	O
if	O
possible	O
but	O
will	O
use	O
software	O
solutions	O
if	O
no	O
hardware	O
solutions	O
exist	O
.	O
</s>
<s>
For	O
example	O
,	O
when	O
the	O
operating	B-General_Concept
system	I-General_Concept
's	O
lock	B-Operating_System
library	O
is	O
used	O
and	O
a	O
thread	B-Operating_System
tries	O
to	O
acquire	O
an	O
already	O
acquired	O
lock	B-Operating_System
,	O
the	O
operating	B-General_Concept
system	I-General_Concept
could	O
suspend	O
the	O
thread	B-Operating_System
using	O
a	O
context	B-Operating_System
switch	I-Operating_System
and	O
swap	O
it	O
out	O
with	O
another	O
thread	B-Operating_System
that	O
is	O
ready	O
to	O
be	O
run	O
,	O
or	O
could	O
put	O
that	O
processor	O
into	O
a	O
low	O
power	O
state	O
if	O
there	O
is	O
no	O
other	O
thread	B-Operating_System
that	O
can	O
be	O
run	O
.	O
</s>
<s>
Therefore	O
,	O
most	O
modern	O
mutual	B-Operating_System
exclusion	I-Operating_System
methods	O
attempt	O
to	O
reduce	O
latency	O
and	O
busy-waits	B-Operating_System
by	O
using	O
queuing	O
and	O
context	B-Operating_System
switches	I-Operating_System
.	O
</s>
<s>
However	O
,	O
if	O
the	O
time	O
that	O
is	O
spent	O
suspending	O
a	O
thread	B-Operating_System
and	O
then	O
restoring	O
it	O
can	O
be	O
proven	O
to	O
be	O
always	O
more	O
than	O
the	O
time	O
that	O
must	O
be	O
waited	O
for	O
a	O
thread	B-Operating_System
to	O
become	O
ready	O
to	O
run	O
after	O
being	O
blocked	O
in	O
a	O
particular	O
situation	O
,	O
then	O
spinlocks	B-Operating_System
are	O
an	O
acceptable	O
solution	O
(	O
for	O
that	O
situation	O
only	O
)	O
.	O
</s>
<s>
One	O
binary	O
test&set	B-Operating_System
register	O
is	O
sufficient	O
to	O
provide	O
the	O
deadlock-free	O
solution	O
to	O
the	O
mutual	B-Operating_System
exclusion	I-Operating_System
problem	O
.	O
</s>
<s>
But	O
a	O
solution	O
built	O
with	O
a	O
test&set	B-Operating_System
register	O
can	O
possibly	O
lead	O
to	O
the	O
starvation	B-Operating_System
of	O
some	O
processes	O
which	O
become	O
caught	O
in	O
the	O
trying	O
section	O
.	O
</s>
<s>
Most	O
algorithms	O
for	O
mutual	B-Operating_System
exclusion	I-Operating_System
are	O
designed	O
with	O
the	O
assumption	O
that	O
no	O
failure	O
occurs	O
while	O
a	O
process	O
is	O
running	O
inside	O
the	O
critical	B-Operating_System
section	I-Operating_System
.	O
</s>
<s>
For	O
example	O
,	O
a	O
sudden	O
loss	O
of	O
power	O
or	O
faulty	O
interconnect	O
might	O
cause	O
a	O
process	O
in	O
a	O
critical	B-Operating_System
section	I-Operating_System
to	O
experience	O
an	O
unrecoverable	O
error	O
or	O
otherwise	O
be	O
unable	O
to	O
continue	O
.	O
</s>
<s>
If	O
such	O
a	O
failure	O
occurs	O
,	O
conventional	O
,	O
non-failure-tolerant	O
mutual	B-Operating_System
exclusion	I-Operating_System
algorithms	O
may	O
deadlock	B-Operating_System
or	O
otherwise	O
fail	O
key	O
liveness	O
properties	O
.	O
</s>
<s>
Many	O
forms	O
of	O
mutual	B-Operating_System
exclusion	I-Operating_System
have	O
side-effects	O
.	O
</s>
<s>
For	O
example	O
,	O
classic	O
semaphores	B-Operating_System
permit	O
deadlocks	B-Operating_System
,	O
in	O
which	O
one	O
process	O
gets	O
a	O
semaphore	B-Operating_System
,	O
another	O
process	O
gets	O
a	O
second	O
semaphore	B-Operating_System
,	O
and	O
then	O
both	O
wait	O
till	O
the	O
other	O
semaphore	B-Operating_System
to	O
be	O
released	O
.	O
</s>
<s>
Other	O
common	O
side-effects	O
include	O
starvation	B-Operating_System
,	O
in	O
which	O
a	O
process	O
never	O
gets	O
sufficient	O
resources	O
to	O
run	O
to	O
completion	O
;	O
priority	B-Operating_System
inversion	I-Operating_System
,	O
in	O
which	O
a	O
higher-priority	O
thread	B-Operating_System
waits	O
for	O
a	O
lower-priority	O
thread	B-Operating_System
;	O
and	O
high	O
latency	O
,	O
in	O
which	O
response	O
to	O
interrupts	B-Application
is	O
not	O
prompt	O
.	O
</s>
<s>
Much	O
research	O
is	O
aimed	O
at	O
eliminating	O
the	O
above	O
effects	O
,	O
often	O
with	O
the	O
goal	O
of	O
guaranteeing	O
non-blocking	B-Operating_System
progress	I-Operating_System
.	O
</s>
<s>
Until	O
such	O
calls	O
became	O
threadsafe	B-Operating_System
,	O
there	O
was	O
no	O
proper	O
mechanism	O
for	O
sleeping	O
a	O
single	B-Operating_System
thread	I-Operating_System
within	O
a	O
process	O
(	O
see	O
polling	B-General_Concept
)	O
.	O
</s>
