<s>
Peterson	B-Operating_System
's	I-Operating_System
algorithm	I-Operating_System
(	O
or	O
Peterson	B-Operating_System
's	I-Operating_System
solution	I-Operating_System
)	O
is	O
a	O
concurrent	B-Architecture
programming	I-Architecture
algorithm	O
for	O
mutual	B-Operating_System
exclusion	I-Operating_System
that	O
allows	O
two	O
or	O
more	O
processes	O
to	O
share	O
a	O
single-use	O
resource	O
without	O
conflict	O
,	O
using	O
only	O
shared	O
memory	O
for	O
communication	O
.	O
</s>
<s>
A	O
flag[n]	O
value	O
of	O
true	O
indicates	O
that	O
the	O
process	O
n	O
wants	O
to	O
enter	O
the	O
critical	B-Operating_System
section	I-Operating_System
.	O
</s>
<s>
Entrance	O
to	O
the	O
critical	B-Operating_System
section	I-Operating_System
is	O
granted	O
for	O
process	O
P0	O
if	O
P1	O
does	O
not	O
want	O
to	O
enter	O
its	O
critical	B-Operating_System
section	I-Operating_System
and	O
if	O
P1	O
has	O
given	O
priority	O
to	O
P0	O
by	O
setting	O
turn	O
to	O
0	O
.	O
</s>
<s>
The	O
three	O
criteria	O
are	O
mutual	B-Operating_System
exclusion	I-Operating_System
,	O
progress	O
,	O
and	O
bounded	O
waiting	O
.	O
</s>
<s>
P0	O
and	O
P1	O
can	O
never	O
be	O
in	O
the	O
critical	B-Operating_System
section	I-Operating_System
at	O
the	O
same	O
time	O
.	O
</s>
<s>
If	O
P0	O
is	O
in	O
its	O
critical	B-Operating_System
section	I-Operating_System
,	O
then	O
flag[0]	O
is	O
true	O
.	O
</s>
<s>
In	O
addition	O
,	O
either	O
flag[1]	O
is	O
false	O
(	O
meaning	O
that	O
P1	O
has	O
left	O
its	O
critical	B-Operating_System
section	I-Operating_System
)	O
,	O
or	O
turn	O
is	O
0	O
(	O
meaning	O
that	O
P1	O
is	O
just	O
now	O
trying	O
to	O
enter	O
the	O
critical	B-Operating_System
section	I-Operating_System
,	O
but	O
graciously	O
waiting	O
)	O
,	O
or	O
P1	O
is	O
at	O
label	O
P1_gate	O
(	O
trying	O
to	O
enter	O
its	O
critical	B-Operating_System
section	I-Operating_System
,	O
after	O
setting	O
flag[1]	O
to	O
true	O
but	O
before	O
setting	O
turn	O
to	O
0	O
and	O
busy	O
waiting	O
)	O
.	O
</s>
<s>
So	O
if	O
both	O
processes	O
are	O
in	O
their	O
critical	B-Operating_System
sections	I-Operating_System
,	O
then	O
we	O
conclude	O
that	O
the	O
state	O
must	O
satisfy	O
flag[0]	O
and	O
flag[1]	O
and	O
turn	O
=	O
0	O
and	O
turn	O
=	O
1	O
.	O
</s>
<s>
No	O
state	O
can	O
satisfy	O
both	O
turn	O
=	O
0	O
and	O
turn	O
=	O
1	O
,	O
so	O
there	O
can	O
be	O
no	O
state	O
where	O
both	O
processes	O
are	O
in	O
their	O
critical	B-Operating_System
sections	I-Operating_System
.	O
</s>
<s>
Progress	O
is	O
defined	O
as	O
the	O
following	O
:	O
if	O
no	O
process	O
is	O
executing	O
in	O
its	O
critical	B-Operating_System
section	I-Operating_System
and	O
some	O
processes	O
wish	O
to	O
enter	O
their	O
critical	B-Operating_System
sections	I-Operating_System
,	O
then	O
only	O
those	O
processes	O
that	O
are	O
not	O
executing	O
in	O
their	O
remainder	O
sections	O
can	O
participate	O
in	O
making	O
the	O
decision	O
as	O
to	O
which	O
process	O
will	O
enter	O
its	O
critical	B-Operating_System
section	I-Operating_System
next	O
.	O
</s>
<s>
Note	O
that	O
for	O
a	O
process	O
or	O
thread	O
,	O
the	O
remainder	O
sections	O
are	O
parts	O
of	O
the	O
code	O
that	O
are	O
not	O
related	O
to	O
the	O
critical	B-Operating_System
section	I-Operating_System
.	O
</s>
<s>
A	O
process	O
cannot	O
immediately	O
re-enter	O
the	O
critical	B-Operating_System
section	I-Operating_System
if	O
the	O
other	O
process	O
has	O
set	O
its	O
flag	O
to	O
say	O
that	O
it	O
would	O
like	O
to	O
enter	O
its	O
critical	B-Operating_System
section	I-Operating_System
.	O
</s>
<s>
Bounded	O
waiting	O
,	O
or	O
bounded	O
bypass	O
,	O
means	O
that	O
the	O
number	O
of	O
times	O
a	O
process	O
is	O
bypassed	O
by	O
another	O
process	O
after	O
it	O
has	O
indicated	O
its	O
desire	O
to	O
enter	O
the	O
critical	B-Operating_System
section	I-Operating_System
is	O
bounded	O
by	O
a	O
function	O
of	O
the	O
number	O
of	O
processes	O
in	O
the	O
system	O
.	O
</s>
<s>
In	O
Peterson	B-Operating_System
's	I-Operating_System
algorithm	I-Operating_System
,	O
a	O
process	O
will	O
never	O
wait	O
longer	O
than	O
one	O
turn	O
for	O
entrance	O
to	O
the	O
critical	B-Operating_System
section	I-Operating_System
.	O
</s>
<s>
The	O
filter	O
algorithm	O
generalizes	O
Peterson	B-Operating_System
's	I-Operating_System
algorithm	I-Operating_System
to	O
processes	O
.	O
</s>
<s>
Instead	O
of	O
a	O
boolean	O
flag	O
,	O
it	O
requires	O
an	O
integer	O
variable	O
per	O
process	O
,	O
stored	O
in	O
a	O
single-writer/multiple	O
-reader	O
(	O
SWMR	O
)	O
atomic	O
register	B-Application
,	O
and	O
additional	O
variables	O
in	O
similar	O
registers	O
.	O
</s>
<s>
The	O
registers	O
can	O
be	O
represented	O
in	O
pseudocode	B-Language
as	O
arrays	O
:	O
</s>
<s>
The	O
variables	O
take	O
on	O
values	O
up	O
to	O
,	O
each	O
representing	O
a	O
distinct	O
"	O
waiting	O
room	O
"	O
before	O
the	O
critical	B-Operating_System
section	I-Operating_System
.	O
</s>
<s>
Processes	O
advance	O
from	O
one	O
room	O
to	O
the	O
next	O
,	O
finishing	O
in	O
room	O
,	O
which	O
is	O
the	O
critical	B-Operating_System
section	I-Operating_System
.	O
</s>
<s>
To	O
release	O
the	O
lock	O
upon	O
exiting	O
the	O
critical	B-Operating_System
section	I-Operating_System
,	O
process	O
sets	O
to	O
−1	O
.	O
</s>
<s>
That	O
this	O
algorithm	O
achieves	O
mutual	B-Operating_System
exclusion	I-Operating_System
can	O
be	O
proven	O
as	O
follows	O
.	O
</s>
<s>
Similarly	O
,	O
at	O
the	O
next	O
level	O
,	O
will	O
proceed	O
,	O
etc.	O
,	O
until	O
at	O
the	O
final	O
level	O
,	O
only	O
one	O
process	O
is	O
allowed	O
to	O
leave	O
the	O
waiting	O
room	O
and	O
enter	O
the	O
critical	B-Operating_System
section	I-Operating_System
,	O
giving	O
mutual	B-Operating_System
exclusion	I-Operating_System
.	O
</s>
<s>
When	O
working	O
at	O
the	O
hardware	B-Architecture
level	O
,	O
Peterson	B-Operating_System
's	I-Operating_System
algorithm	I-Operating_System
is	O
typically	O
not	O
needed	O
to	O
achieve	O
atomic	O
access	O
.	O
</s>
<s>
Some	O
processors	O
have	O
special	O
instructions	O
,	O
like	O
test-and-set	B-Operating_System
or	O
compare-and-swap	B-Operating_System
,	O
which	O
,	O
by	O
locking	O
the	O
memory	O
bus	O
,	O
can	O
be	O
used	O
to	O
provide	O
mutual	B-Operating_System
exclusion	I-Operating_System
in	O
SMP	B-Operating_System
systems	O
.	O
</s>
<s>
Most	O
modern	O
CPUs	O
reorder	O
memory	O
accesses	O
to	O
improve	O
execution	O
efficiency	O
(	O
see	O
memory	B-General_Concept
ordering	I-General_Concept
for	O
types	O
of	O
reordering	O
allowed	O
)	O
.	O
</s>
<s>
Such	O
processors	O
invariably	O
give	O
some	O
way	O
to	O
force	O
ordering	O
in	O
a	O
stream	O
of	O
memory	O
accesses	O
,	O
typically	O
through	O
a	O
memory	B-General_Concept
barrier	I-General_Concept
instruction	O
.	O
</s>
<s>
Note	O
that	O
reordering	O
of	O
memory	O
accesses	O
can	O
happen	O
even	O
on	O
processors	O
that	O
do	O
n't	O
reorder	O
instructions	O
(	O
such	O
as	O
the	O
PowerPC	B-Architecture
processor	I-Architecture
in	O
the	B-Operating_System
Xbox	I-Operating_System
360	I-Operating_System
)	O
.	O
</s>
<s>
Most	O
such	O
CPUs	O
also	O
have	O
some	O
sort	O
of	O
guaranteed	O
atomic	B-General_Concept
operation	I-General_Concept
,	O
such	O
as	O
XCHG	O
on	O
x86	B-Operating_System
processors	O
and	O
load-link/store	B-Operating_System
-conditional	I-Operating_System
on	O
Alpha	B-Device
,	O
MIPS	B-Device
,	O
PowerPC	B-Architecture
,	O
and	O
other	O
architectures	O
.	O
</s>
