<s>
In	O
concurrent	B-Architecture
programming	I-Architecture
,	O
concurrent	O
accesses	O
to	O
shared	B-General_Concept
resources	I-General_Concept
can	O
lead	O
to	O
unexpected	O
or	O
erroneous	B-Language
behavior	O
,	O
so	O
parts	O
of	O
the	O
program	O
where	O
the	O
shared	B-General_Concept
resource	I-General_Concept
is	O
accessed	O
need	O
to	O
be	O
protected	O
in	O
ways	O
that	O
avoid	O
the	O
concurrent	O
access	O
.	O
</s>
<s>
One	O
way	O
to	O
do	O
so	O
is	O
known	O
as	O
a	O
critical	B-Operating_System
section	I-Operating_System
or	O
critical	O
region	O
.	O
</s>
<s>
This	O
protected	O
section	O
cannot	O
be	O
entered	O
by	O
more	O
than	O
one	O
process	O
or	O
thread	O
at	O
a	O
time	O
;	O
others	O
are	O
suspended	O
until	O
the	O
first	O
leaves	O
the	O
critical	B-Operating_System
section	I-Operating_System
.	O
</s>
<s>
Typically	O
,	O
the	O
critical	B-Operating_System
section	I-Operating_System
accesses	O
a	O
shared	B-General_Concept
resource	I-General_Concept
,	O
such	O
as	O
a	O
data	B-General_Concept
structure	I-General_Concept
,	O
a	O
peripheral	O
device	O
,	O
or	O
a	O
network	O
connection	O
,	O
that	O
would	O
not	O
operate	O
correctly	O
in	O
the	O
context	O
of	O
multiple	O
concurrent	O
accesses	O
.	O
</s>
<s>
In	O
cases	O
where	O
a	O
locking	B-Operating_System
mechanism	O
with	O
finer	O
granularity	O
is	O
not	O
needed	O
,	O
a	O
critical	B-Operating_System
section	I-Operating_System
is	O
important	O
.	O
</s>
<s>
To	O
prevent	O
this	O
,	O
variable	O
is	O
protected	O
by	O
a	O
critical	B-Operating_System
section	I-Operating_System
.	O
</s>
<s>
Once	O
B	O
finishes	O
writing	O
the	O
value	O
,	O
A	O
gets	O
the	O
access	O
to	O
the	O
critical	B-Operating_System
section	I-Operating_System
,	O
and	O
variable	O
can	O
be	O
read	O
.	O
</s>
<s>
By	O
carefully	O
controlling	O
which	O
variables	O
are	O
modified	O
inside	O
and	O
outside	O
the	O
critical	B-Operating_System
section	I-Operating_System
,	O
concurrent	O
access	O
to	O
the	O
shared	O
variable	O
are	O
prevented	O
.	O
</s>
<s>
A	O
critical	B-Operating_System
section	I-Operating_System
is	O
typically	O
used	O
when	O
a	O
multi-threaded	O
program	O
must	O
update	O
multiple	O
related	O
variables	O
without	O
a	O
separate	O
thread	O
making	O
conflicting	O
changes	O
to	O
that	O
data	O
.	O
</s>
<s>
In	O
a	O
related	O
situation	O
,	O
a	O
critical	B-Operating_System
section	I-Operating_System
may	O
be	O
used	O
to	O
ensure	O
that	O
a	O
shared	B-General_Concept
resource	I-General_Concept
,	O
for	O
example	O
,	O
a	O
printer	O
,	O
can	O
only	O
be	O
accessed	O
by	O
one	O
process	O
at	O
a	O
time	O
.	O
</s>
<s>
The	O
implementation	O
of	O
critical	B-Operating_System
sections	I-Operating_System
vary	O
among	O
different	O
operating	O
systems	O
.	O
</s>
<s>
A	O
critical	B-Operating_System
section	I-Operating_System
will	O
usually	O
terminate	O
in	O
finite	O
time	O
,	O
and	O
a	O
thread	O
,	O
task	O
,	O
or	O
process	O
will	O
have	O
to	O
wait	O
for	O
a	O
fixed	O
time	O
to	O
enter	O
it	O
(	O
bounded	O
waiting	O
)	O
.	O
</s>
<s>
To	O
ensure	O
exclusive	O
use	O
of	O
critical	B-Operating_System
sections	I-Operating_System
some	O
synchronization	O
mechanism	O
is	O
required	O
at	O
the	O
entry	O
and	O
exit	O
of	O
the	O
program	O
.	O
</s>
<s>
Critical	B-Operating_System
section	I-Operating_System
is	O
a	O
piece	O
of	O
a	O
program	O
that	O
requires	O
mutual	B-Operating_System
exclusion	I-Operating_System
of	O
access	O
.	O
</s>
<s>
As	O
shown	O
in	O
the	O
figure	O
,	O
in	O
the	O
case	O
of	O
mutual	B-Operating_System
exclusion	I-Operating_System
(	O
mutex	B-Operating_System
)	O
,	O
one	O
thread	O
blocks	O
a	O
critical	B-Operating_System
section	I-Operating_System
by	O
using	O
locking	B-Operating_System
techniques	O
when	O
it	O
needs	O
to	O
access	O
the	O
shared	B-General_Concept
resource	I-General_Concept
,	O
and	O
other	O
threads	O
have	O
to	O
wait	O
to	O
get	O
their	O
turn	O
to	O
enter	O
into	O
the	O
section	O
.	O
</s>
<s>
The	O
simplest	O
method	O
to	O
prevent	O
any	O
change	O
of	O
processor	O
control	O
inside	O
the	O
critical	B-Operating_System
section	I-Operating_System
is	O
implementing	O
a	O
semaphore	B-Operating_System
.	O
</s>
<s>
In	O
uniprocessor	O
systems	O
,	O
this	O
can	O
be	O
done	O
by	O
disabling	O
interrupts	B-Application
on	O
entry	O
into	O
the	O
critical	B-Operating_System
section	I-Operating_System
,	O
avoiding	O
system	O
calls	O
that	O
can	O
cause	O
a	O
context	B-Operating_System
switch	I-Operating_System
while	O
inside	O
the	O
section	O
,	O
and	O
restoring	O
interrupts	B-Application
to	O
their	O
previous	O
state	O
on	O
exit	O
.	O
</s>
<s>
Any	O
thread	O
of	O
execution	O
entering	O
any	O
critical	B-Operating_System
section	I-Operating_System
anywhere	O
in	O
the	O
system	O
will	O
,	O
with	O
this	O
implementation	O
,	O
prevent	O
any	O
other	O
thread	O
,	O
including	O
an	O
interrupt	B-Application
,	O
from	O
being	O
granted	O
processing	O
time	O
on	O
the	O
CPU	O
—	O
and	O
therefore	O
from	O
entering	O
any	O
other	O
critical	B-Operating_System
section	I-Operating_System
or	O
,	O
indeed	O
,	O
any	O
code	O
whatsoever	O
—	O
until	O
the	O
original	O
thread	O
leaves	O
its	O
critical	B-Operating_System
section	I-Operating_System
.	O
</s>
<s>
This	O
brute-force	O
approach	O
can	O
be	O
improved	O
upon	O
by	O
using	O
semaphores	B-Operating_System
.	O
</s>
<s>
To	O
enter	O
a	O
critical	B-Operating_System
section	I-Operating_System
,	O
a	O
thread	O
must	O
obtain	O
a	O
semaphore	B-Operating_System
,	O
which	O
it	O
releases	O
on	O
leaving	O
the	O
section	O
.	O
</s>
<s>
Other	O
threads	O
are	O
prevented	O
from	O
entering	O
the	O
critical	B-Operating_System
section	I-Operating_System
at	O
the	O
same	O
time	O
as	O
the	O
original	O
thread	O
,	O
but	O
are	O
free	O
to	O
gain	O
control	O
of	O
the	O
CPU	O
and	O
execute	B-General_Concept
other	O
code	O
,	O
including	O
other	O
critical	B-Operating_System
sections	I-Operating_System
that	O
are	O
protected	O
by	O
different	O
semaphores	B-Operating_System
.	O
</s>
<s>
Semaphore	B-Operating_System
locking	B-Operating_System
also	O
has	O
a	O
time	O
limit	O
to	O
prevent	O
a	O
deadlock	O
condition	O
in	O
which	O
a	O
lock	O
is	O
acquired	O
by	O
a	O
single	O
process	O
for	O
an	O
infinite	O
time	O
,	O
stalling	O
the	O
other	O
processes	O
that	O
need	O
to	O
use	O
the	O
shared	B-General_Concept
resource	I-General_Concept
protected	O
by	O
the	O
critical	B-Operating_System
section	I-Operating_System
.	O
</s>
<s>
Typically	O
,	O
critical	B-Operating_System
sections	I-Operating_System
prevent	O
thread	O
and	O
process	B-Operating_System
migration	I-Operating_System
between	O
processors	O
and	O
the	O
preemption	B-Operating_System
of	O
processes	O
and	O
threads	O
by	O
interrupts	B-Application
and	O
other	O
processes	O
and	O
threads	O
.	O
</s>
<s>
Critical	B-Operating_System
sections	I-Operating_System
often	O
allow	O
nesting	O
.	O
</s>
<s>
Nesting	O
allows	O
multiple	O
critical	B-Operating_System
sections	I-Operating_System
to	O
be	O
entered	O
and	O
exited	O
at	O
little	O
cost	O
.	O
</s>
<s>
If	O
the	O
scheduler	O
interrupts	B-Application
the	O
current	O
process	O
or	O
thread	O
in	O
a	O
critical	B-Operating_System
section	I-Operating_System
,	O
the	O
scheduler	O
will	O
either	O
allow	O
the	O
currently	O
executing	O
process	O
or	O
thread	O
to	O
run	O
to	O
completion	O
of	O
the	O
critical	B-Operating_System
section	I-Operating_System
,	O
or	O
it	O
will	O
schedule	O
the	O
process	O
or	O
thread	O
for	O
another	O
complete	O
quantum	O
.	O
</s>
<s>
The	O
scheduler	O
will	O
not	O
migrate	O
the	O
process	O
or	O
thread	O
to	O
another	O
processor	O
,	O
and	O
it	O
will	O
not	O
schedule	O
another	O
process	O
or	O
thread	O
to	O
run	O
while	O
the	O
current	O
process	O
or	O
thread	O
is	O
in	O
a	O
critical	B-Operating_System
section	I-Operating_System
.	O
</s>
<s>
Similarly	O
,	O
if	O
an	O
interrupt	B-Application
occurs	O
in	O
a	O
critical	B-Operating_System
section	I-Operating_System
,	O
the	O
interrupt	B-Application
information	O
is	O
recorded	O
for	O
future	O
processing	O
,	O
and	O
execution	O
is	O
returned	O
to	O
the	O
process	O
or	O
thread	O
in	O
the	O
critical	B-Operating_System
section	I-Operating_System
.	O
</s>
<s>
Once	O
the	O
critical	B-Operating_System
section	I-Operating_System
is	O
exited	O
,	O
and	O
in	O
some	O
cases	O
the	O
scheduled	O
quantum	O
completed	O
,	O
the	O
pending	O
interrupt	B-Application
will	O
be	O
executed	O
.	O
</s>
<s>
The	O
concept	O
of	O
scheduling	O
quantum	O
applies	O
to	O
"	O
round-robin	B-Algorithm
"	O
and	O
similar	O
scheduling	O
policies	O
.	O
</s>
<s>
Since	O
critical	B-Operating_System
sections	I-Operating_System
may	O
execute	B-General_Concept
only	O
on	O
the	O
processor	O
on	O
which	O
they	O
are	O
entered	O
,	O
synchronization	O
is	O
only	O
required	O
within	O
the	O
executing	O
processor	O
.	O
</s>
<s>
This	O
allows	O
critical	B-Operating_System
sections	I-Operating_System
to	O
be	O
entered	O
and	O
exited	O
at	O
almost	O
zero	O
cost	O
.	O
</s>
<s>
Most	O
processors	O
provide	O
the	O
required	O
amount	O
of	O
synchronization	O
by	O
the	O
simple	O
act	O
of	O
interrupting	B-Application
the	O
current	O
execution	O
state	O
.	O
</s>
<s>
This	O
allows	O
critical	B-Operating_System
sections	I-Operating_System
in	O
most	O
cases	O
to	O
be	O
nothing	O
more	O
than	O
a	O
per	O
processor	O
count	O
of	O
critical	B-Operating_System
sections	I-Operating_System
entered	O
.	O
</s>
<s>
Performance	O
enhancements	O
include	O
executing	O
pending	O
interrupts	B-Application
at	O
the	O
exit	O
of	O
all	O
critical	B-Operating_System
sections	I-Operating_System
and	O
allowing	O
the	O
scheduler	O
to	O
run	O
at	O
the	O
exit	O
of	O
all	O
critical	B-Operating_System
sections	I-Operating_System
.	O
</s>
<s>
Furthermore	O
,	O
pending	O
interrupts	B-Application
may	O
be	O
transferred	O
to	O
other	O
processors	O
for	O
execution	O
.	O
</s>
<s>
Critical	B-Operating_System
sections	I-Operating_System
should	O
not	O
be	O
used	O
as	O
a	O
long-lasting	O
locking	B-Operating_System
primitive	O
.	O
</s>
<s>
Critical	B-Operating_System
sections	I-Operating_System
should	O
be	O
kept	O
short	O
enough	O
so	O
that	O
it	O
can	O
be	O
entered	O
,	O
executed	O
,	O
and	O
exited	O
without	O
any	O
interrupts	B-Application
occurring	O
from	O
the	O
hardware	B-Architecture
and	O
the	O
scheduler	O
.	O
</s>
<s>
Kernel-level	O
critical	B-Operating_System
sections	I-Operating_System
are	O
the	O
base	O
of	O
the	O
software	B-Operating_System
lockout	I-Operating_System
issue	O
.	O
</s>
<s>
The	O
read-write	B-General_Concept
conflicting	I-General_Concept
variables	O
are	O
split	O
between	O
threads	O
and	O
each	O
thread	O
has	O
a	O
copy	O
of	O
them	O
.	O
</s>
<s>
Data	B-General_Concept
structures	I-General_Concept
like	O
linked	B-Data_Structure
lists	I-Data_Structure
,	O
trees	B-Application
,	O
hash	B-Algorithm
tables	I-Algorithm
etc	O
.	O
</s>
<s>
To	O
improve	O
the	O
efficiency	O
of	O
implementing	O
data	B-General_Concept
structures	I-General_Concept
multiple	O
operations	O
like	O
insertion	O
,	O
deletion	O
,	O
search	O
need	O
to	O
be	O
executed	O
in	O
parallel	O
.	O
</s>
<s>
In	O
such	O
cases	O
,	O
the	O
output	O
may	O
be	O
erroneous	B-Language
.	O
</s>
<s>
To	O
prevent	O
this	O
,	O
one	O
method	O
is	O
that	O
the	O
entire	O
data-structure	O
can	O
be	O
kept	O
under	O
critical	B-Operating_System
section	I-Operating_System
so	O
that	O
only	O
one	O
operation	O
is	O
handled	O
at	O
a	O
time	O
.	O
</s>
<s>
Another	O
method	O
is	O
locking	B-Operating_System
the	O
node	O
in	O
use	O
under	O
critical	B-Operating_System
section	I-Operating_System
,	O
so	O
that	O
other	O
operations	O
do	O
not	O
use	O
the	O
same	O
node	O
.	O
</s>
<s>
Using	O
critical	B-Operating_System
section	I-Operating_System
,	O
thus	O
,	O
ensures	O
that	O
the	O
code	O
provides	O
expected	O
outputs	O
.	O
</s>
<s>
Critical	B-Operating_System
sections	I-Operating_System
also	O
occur	O
in	O
code	O
which	O
manipulates	O
external	O
peripherals	O
,	O
such	O
as	O
I/O	O
devices	O
.	O
</s>
<s>
Storage	O
devices	O
provide	O
a	O
form	O
of	O
memory	O
;	O
the	O
concept	O
of	O
critical	B-Operating_System
sections	I-Operating_System
is	O
equally	O
relevant	O
in	O
the	O
same	O
manner	O
as	O
it	O
is	O
to	O
shared	O
data	B-General_Concept
structures	I-General_Concept
in	O
main	O
memory	O
.	O
</s>
<s>
A	O
process	O
which	O
performs	O
multiple	O
access	O
or	O
update	O
operations	O
on	O
a	O
file	O
is	O
executing	O
a	O
critical	B-Operating_System
section	I-Operating_System
that	O
must	O
be	O
guarded	O
with	O
an	O
appropriate	O
file	O
locking	B-Operating_System
mechanism	O
.	O
</s>
