


















































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



















































Background
The Critical-Section Problem
Synchronization Hardware
Semaphores
Classical Problems of Synchronization
Critical Regions
Monitors
Synchronization in Solaris 2
Atomic Transactions
Operating System Concepts
Silberschatz and Galvin
c
©
1998
inconsistency.Concurrent access to shared data may result in data
the orderly execution of cooperating processes.Maintaining data consistency requires mechanisms to ensure
n
1 items in buffer at the same time. A
solution, were all
buffers are used is not simple.
adding a variableSuppose that we modify the producer-consumer code by
counter
, initialized to 0 and incremented
each time a new item is added to the buffer.
Operating System Concepts
Silberschatz and Galvin
c
©
1998
Consumer process
repeat
while
counter
do
no-op
nextc
buffer
out
out
out
mod
n
;
counter
counter
consume the item in
nextc
until
false
The statements:
-
counter
counter
counter
counter
must be executed
atomically
Operating System Concepts
Silberschatz and Galvin
c
©
1998
n
processes all competing to use some shared data
Each process has a code segment, called
critical section
, in
which the shared data is accessed.
critical section.critical section, no other process is allowed to execute in itsProblem – ensure that when one process is executing in its
Structure of process
i
repeat
entry section
critical section
exit section
remainder section
until
false
Operating System Concepts
Silberschatz and Galvin
c
©
1998
Only 2 processes,
0
and
1
General structure of process
i^
(other process
j (^) )
repeat
entry section
critical section
exit section
remainder section
until
false
their actions.Processes may share some common variables to synchronize
Operating System Concepts
Silberschatz and Galvin
c
©
1998
Shared variables:
- var
turn
initially
turn
turn
i
⇒
i
can enter its critical section
Process
i
repeat
while
turn
i
do
no-op
critical section
turn
j ;
remainder section
until
false
Satisfies mutual exclusion, but not progress.
Operating System Concepts
Silberschatz and Galvin
c
©
1998
Combined shared variables of algorithms 1 and 2.
Process
i
repeat
flag
i ] :=
true
turn
j ;
while
flag
j ]
and
turn
j )
do
no-op
critical section
flag
i ] :=
false
remainder section
until
false
problem for two processes.Meets all three requirements; solves the critical-section
Operating System Concepts
Silberschatz and Galvin
c
©
1998
Critical section for
n
processes
Holder of the smallest number enters the critical section.Before entering its critical section, process receives a number.
If processes
i
and
j
receive the same number, if
i
j , then
i
is served first; else
j
is served first.
increasing order of enumeration; i.e., 1,2,3,3,3,3,4,5...The numbering scheme always generates numbers in
Operating System Concepts
Silberschatz and Galvin
c
©
1998
repeat
choosing
i ] :=
true
number
i ] :=
max
number
number
number
n
choosing
i ] :=
false
for
j
:= 0
to
n
do begin
while
choosing
j ]
do
no-op
while
number
j ]
= 0
and
number
j ],
j )
number
i ],
i )
do
no-op
end
critical section
number
i ] := 0;
remainder section
until
false
Operating System Concepts
Silberschatz and Galvin
c
©
1998
Test and modify the content of a word atomically.
function
Test-and-Set
var
target
boolean
boolean
begin
Test-and-Set
target
target
true
end
Operating System Concepts
Silberschatz and Galvin
c
©
1998
Synchronization tool that does not require busy waiting.
Semaphore
can only be accessed via two indivisible (atomic) operations
wait
while
do
no-op
signal
Operating System Concepts
Silberschatz and Galvin
c
©
1998
Shared variables
- var
mutex
semaphore
initially
mutex
Process
i
repeat
wait
mutex
critical section
signal
mutex
remainder section
until
false
Operating System Concepts
Silberschatz and Galvin
c
©
1998
Semaphore operations now defined as
wait
value
value
if
value
then begin
add this process to
block
end
signal
value
value
if
value
then begin
remove a process
from
wakeup
end
Operating System Concepts
Silberschatz and Galvin
c
©
1998
Execute
in
j
only after
executed in
i
Use semaphore
flag
initialized to 0
Code:
i
j
wait
flag
signal
flag
Operating System Concepts
Silberschatz and Galvin
c
©
1998