<s>
Lamport	B-Operating_System
's	I-Operating_System
bakery	I-Operating_System
algorithm	I-Operating_System
is	O
a	O
computer	O
algorithm	O
devised	O
by	O
computer	O
scientist	O
Leslie	O
Lamport	O
,	O
as	O
part	O
of	O
his	O
long	O
study	O
of	O
the	O
formal	O
correctness	O
of	O
concurrent	B-Operating_System
systems	I-Operating_System
,	O
which	O
is	O
intended	O
to	O
improve	O
the	O
safety	O
in	O
the	O
usage	O
of	O
shared	O
resources	O
among	O
multiple	O
threads	B-Operating_System
by	O
means	O
of	O
mutual	B-Operating_System
exclusion	I-Operating_System
.	O
</s>
<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
it	O
is	O
common	O
for	O
multiple	O
threads	B-Operating_System
to	O
simultaneously	O
access	O
the	O
same	O
resources	O
.	O
</s>
<s>
Data	O
corruption	O
can	O
occur	O
if	O
two	O
or	O
more	O
threads	B-Operating_System
try	O
to	O
write	O
into	O
the	O
same	O
memory	B-General_Concept
location	O
,	O
or	O
if	O
one	O
thread	B-Operating_System
reads	O
a	O
memory	B-General_Concept
location	O
before	O
another	O
has	O
finished	O
writing	O
into	O
it	O
.	O
</s>
<s>
Lamport	B-Operating_System
's	I-Operating_System
bakery	I-Operating_System
algorithm	I-Operating_System
is	O
one	O
of	O
many	O
mutual	B-Operating_System
exclusion	I-Operating_System
algorithms	O
designed	O
to	O
prevent	O
concurrent	O
threads	B-Operating_System
entering	O
critical	B-Operating_System
sections	I-Operating_System
of	O
code	O
concurrently	O
to	O
eliminate	O
the	O
risk	O
of	O
data	O
corruption	O
.	O
</s>
<s>
According	O
to	O
the	O
analogy	O
,	O
the	O
"	O
customers	O
"	O
are	O
threads	B-Operating_System
,	O
identified	O
by	O
the	O
letter	O
i	O
,	O
obtained	O
from	O
a	O
global	O
variable	O
.	O
</s>
<s>
It	O
is	O
possible	O
that	O
more	O
than	O
one	O
thread	B-Operating_System
will	O
get	O
the	O
same	O
number	O
n	O
when	O
they	O
request	O
it	O
;	O
this	O
cannot	O
be	O
avoided	O
(	O
without	O
first	O
solving	O
the	O
mutual	B-Operating_System
exclusion	I-Operating_System
problem	O
,	O
which	O
is	O
the	O
goal	O
of	O
the	O
algorithm	O
)	O
.	O
</s>
<s>
Therefore	O
,	O
it	O
is	O
assumed	O
that	O
the	O
thread	B-Operating_System
identifier	O
i	O
is	O
also	O
a	O
priority	O
.	O
</s>
<s>
A	O
lower	O
value	O
of	O
i	O
means	O
a	O
higher	O
priority	O
and	O
threads	B-Operating_System
with	O
higher	O
priority	O
will	O
enter	O
the	O
critical	B-Operating_System
section	I-Operating_System
first	O
.	O
</s>
<s>
The	O
critical	B-Operating_System
section	I-Operating_System
is	O
that	O
part	O
of	O
code	O
that	O
requires	O
exclusive	O
access	O
to	O
resources	O
and	O
may	O
only	O
be	O
executed	O
by	O
one	O
thread	B-Operating_System
at	O
a	O
time	O
.	O
</s>
<s>
When	O
a	O
thread	B-Operating_System
wants	O
to	O
enter	O
the	O
critical	B-Operating_System
section	I-Operating_System
,	O
it	O
has	O
to	O
check	O
whether	O
now	O
is	O
its	O
turn	O
to	O
do	O
so	O
.	O
</s>
<s>
It	O
should	O
check	O
the	O
number	O
n	O
of	O
every	O
other	O
thread	B-Operating_System
to	O
make	O
sure	O
that	O
it	O
has	O
the	O
smallest	O
one	O
.	O
</s>
<s>
In	O
case	O
another	O
thread	B-Operating_System
has	O
the	O
same	O
number	O
,	O
the	O
thread	B-Operating_System
with	O
the	O
smallest	O
i	O
will	O
enter	O
the	O
critical	B-Operating_System
section	I-Operating_System
first	O
.	O
</s>
<s>
In	O
pseudocode	B-Language
this	O
comparison	O
between	O
threads	B-Operating_System
a	O
and	O
b	O
can	O
be	O
written	O
in	O
the	O
form	O
:	O
</s>
<s>
Once	O
the	O
thread	B-Operating_System
ends	O
its	O
critical	O
job	O
,	O
it	O
gets	O
rid	O
of	O
its	O
number	O
and	O
enters	O
the	O
non-critical	O
section	O
.	O
</s>
<s>
It	O
represents	O
some	O
thread-specific	O
computation	O
that	O
does	O
n't	O
interfere	O
with	O
other	O
threads	B-Operating_System
 '	O
resources	O
and	O
execution	O
.	O
</s>
<s>
Words	O
choosing	O
 [ i ] 	O
and	O
number	O
 [ i ] 	O
are	O
in	O
the	O
memory	B-General_Concept
of	O
process	O
i	O
,	O
and	O
are	O
initially	O
zero	O
.	O
</s>
<s>
There	O
may	O
then	O
be	O
a	O
period	O
when	O
reading	O
from	O
its	O
memory	B-General_Concept
gives	O
arbitrary	O
values	O
.	O
</s>
<s>
Eventually	O
,	O
any	O
read	O
from	O
its	O
memory	B-General_Concept
must	O
give	O
a	O
value	O
of	O
zero	O
.	O
</s>
<s>
In	O
this	O
example	O
,	O
all	O
threads	B-Operating_System
execute	O
the	O
same	O
"	O
main	O
"	O
function	O
,	O
Thread	B-Operating_System
.	O
</s>
<s>
In	O
real	O
applications	O
,	O
different	O
threads	B-Operating_System
often	O
have	O
different	O
"	O
main	O
"	O
functions	O
.	O
</s>
<s>
Note	O
that	O
as	O
in	O
the	O
original	O
paper	O
,	O
the	O
thread	B-Operating_System
checks	O
itself	O
before	O
entering	O
the	O
critical	B-Operating_System
section	I-Operating_System
.	O
</s>
<s>
Each	O
thread	B-Operating_System
only	O
writes	O
its	O
own	O
storage	O
,	O
only	O
reads	O
are	O
shared	O
.	O
</s>
<s>
compare-and-swap	B-Operating_System
.	O
</s>
<s>
Therefore	O
,	O
this	O
algorithm	O
can	O
be	O
used	O
to	O
implement	O
mutual	B-Operating_System
exclusion	I-Operating_System
on	O
memory	B-General_Concept
that	O
lacks	O
synchronisation	O
primitives	O
,	O
e.g.	O
,	O
a	O
simple	O
SCSI	O
disk	O
shared	O
between	O
two	O
computers	O
.	O
</s>
<s>
If	O
the	O
higher-priority	O
process	O
was	O
preempted	O
before	O
setting	O
Number[i],	O
the	O
low-priority	O
process	O
will	O
see	O
that	O
the	O
other	O
process	O
has	O
a	O
number	O
of	O
zero	O
,	O
and	O
enters	O
the	O
critical	B-Operating_System
section	I-Operating_System
;	O
later	O
,	O
the	O
high-priority	O
process	O
will	O
ignore	O
equal	O
Number[i]	O
for	O
lower-priority	O
processes	O
,	O
and	O
also	O
enters	O
the	O
critical	B-Operating_System
section	I-Operating_System
.	O
</s>
<s>
As	O
a	O
result	O
,	O
two	O
processes	O
can	O
enter	O
the	O
critical	B-Operating_System
section	I-Operating_System
at	O
the	O
same	O
time	O
.	O
</s>
<s>
The	O
bakery	B-Operating_System
algorithm	I-Operating_System
uses	O
the	O
Entering	O
variable	O
to	O
make	O
the	O
assignment	O
on	O
line	O
6	O
look	O
like	O
it	O
was	O
atomic	O
;	O
process	O
i	O
will	O
never	O
see	O
a	O
number	O
equal	O
to	O
zero	O
for	O
a	O
process	O
j	O
that	O
is	O
going	O
to	O
pick	O
the	O
same	O
number	O
as	O
i	O
.	O
</s>
<s>
When	O
implementing	O
the	O
pseudo	B-Language
code	I-Language
in	O
a	O
single	O
process	O
system	O
or	O
under	O
cooperative	B-Operating_System
multitasking	I-Operating_System
,	O
it	O
is	O
better	O
to	O
replace	O
the	O
"	O
do	O
nothing	O
"	O
sections	O
with	O
code	O
that	O
notifies	O
the	O
operating	O
system	O
to	O
immediately	O
switch	O
to	O
the	O
next	O
thread	B-Operating_System
.	O
</s>
<s>
Lamport	B-Operating_System
's	I-Operating_System
bakery	I-Operating_System
algorithm	I-Operating_System
assumes	O
a	O
sequential	O
consistency	O
memory	B-General_Concept
model	O
.	O
</s>
<s>
Few	O
,	O
if	O
any	O
,	O
languages	O
or	O
multi-core	O
processors	O
implement	O
such	O
a	O
memory	B-General_Concept
model	O
.	O
</s>
<s>
Under	O
the	O
Java	B-General_Concept
Memory	I-General_Concept
Model	I-General_Concept
this	O
ensures	O
that	O
writes	O
are	O
immediately	O
visible	O
to	O
all	O
threads	B-Operating_System
.	O
</s>
