If you can't read please download the document
Upload
homer-stewart
View
231
Download
0
Embed Size (px)
DESCRIPTION
Mutual Exclusion and Synchronization Principles of concurrency. Mutual Exclusion: Software approaches and Hardware Support. Semaphores、Monitors、Message Passing. Tradition question: Producer/Consumer、Readers/Writers Problem.
Citation preview
Processes and Scheduling (II)
Chapter 2 Processes and Scheduling (II) Mutual Exclusion and
Synchronization
Principles of concurrency. Mutual Exclusion:Software approaches and
Hardware Support. SemaphoresMonitorsMessage Passing. Tradition
question:Producer/ConsumerReaders/Writers Problem. Difficulties
Arise in Concurrency
Types: interleaving and overlapping. The sharing of global
resources is fraught with peril. It is difficult for the OS to
manage the allocation of resources optimally. It is difficult to
locate a programming error, because results are typically not
reproducible. Sharing can lead to problems
Process P1 . input (in, keyboard) out:=in output (out, display)
Process P2 . input (in, keyboard) out:=in output (out, display) So,
it is necessary to protect shared global variables. Problems of
Concurrent
Resource competition: How to allocate the resource, and how to
mutual exclusion access the critical resources. Execution sequence.
Communication cooperation. Process Interaction (table5.1)
Processes unaware of each other:OS needs to be concerned about
competition for resources. Exp. Two independent applications may
both want access to the same disk or file or printer. Processes
indirectly aware of each other(e.g., shared object):The processes
share access to some object such as I/O buffer. Processes directly
aware of each other:Communicate with each other by name. Mutual
Exclusion(P193) entry section critical section exit section
Critical resource:Only one process can access it at a time.
Critical sectionthe portion of the program that uses critical
resource. entry section critical section exit section P194fig5.1
Program mutualexclusion Const n-; (*num of processes)
Procedure P(i:integer) Begin repeat enter critical(R) ; exit
critical(R) forever End; Begin(*main program*) parbegin p1; p2; pn;
parend End. Deadlock and Starvation(P194)
Exp. Two processes P1P2, both request to resource R1R2. At a time:
P1 gets R2 andP2 gets R1, in the meantime P1 requests R1 andP2
requests R2 Then P1 and P2 wait for each other forever ---deadlock.
Starvation P1 always communication with P2, p3 is starved.
Cooperation among Processes by Sharing(p195)
Data integrity Reading and writing, and only writing operations
must be exclusive. Data coherence:a=b no longer holds P1:a:=a+1
b:=b+1 P2: b:=2*b a:=2*a a:=a+1 b:=2*b b:=b+1 a:=2*a Cooperation
among Processes by Communication
Synchronize: The processes send and receive data between each other
coordinately. Requirements for Mutual Exclusion ()(P196
Mutual exclusion must be enforced. A process that halts in its
noncritical section must do so without interfering with other
processes. It must not be possible for a process requiring access
to a critical section to be delayed indefinitely. Requirements for
Mutual Exclusion
When no process is in a critical section, any process that requests
entry to its critical section must be permitted to enter without
delay. No assumptions are made about relative process speeds or
number of processors. A process remains inside its critical section
for a finite time only. Mutual Exclusion: Software Approaches
Dekkers Algorithm Petersons Algorithm Dekkers AlgorithmFirst
Attempt
Fig 5.2 An Igloo for Mutual Exclusion The entrance and the igloo
itself are small enough that only one person can be in the igloo at
a time. Inside, there is a blackboard on which a single value can
be written. A process wishing to execute its critical section first
enters the igloo and examines the blackboard. Dekkers Algorithm
First Attempt
If its number is on the blackboard, then the process may leave the
igloo and proceed to its critical section. After has completed the
critical section, it should place the number of the other process
on the board If not its number, it leaves the igloo and is forced
to wait. From time to time, the process reenters the igloo to check
the blackboard until it is allowed to enter its critical section.
(busy waiting) Dekkers Algorithm First Attempt
var turn:0..1; PROCESS 0 while turn0 do {nothing}; ; turn:=1;
PROCESS 1 while turn 1 do {nothing}; turn:=0; Processes must
strictly alternate in their use of their critical sections. Dekkers
Algorithm Second Attempt
Fig5.3:A two-Igloo solution for mutual exclusion (P199) Each
process may examine the others board but cannot alter it. When a
process wishes to enter its critical section, it periodically
checks the others board until it finds FALSE written on it,
indicating that the other process is not in its critical section.
Dekkers Algorithm Second Attempt
If false, writes true on its own board and enter. After finish,
alter its board to show false If truebusy waiting Dekkers Algorithm
Second Attempt
var flag : array [0..1] of boolean :false ; PROCESS 0 while flag[1]
do {nothing}; flag[0]:=true; ; flag[0]:=false; PROCESS 1 while
flag[0] do {nothing}; flag[1]:=true; ; flag[1]:=false; Dekkers
Algorithm Second Attempt
Analysis Cannot guarantee mutual exclusion: P0 flag[1]=false; do
while flag[1]; P1flag[0]=false; do while flag[0]; P0flag[0]=true; ;
P1flag[1]=true; ; Dekkers Algorithm Third Attempt
var flag : array [0..1] of boolean : false; PROCESS 0
flag[0]:=true; while flag[1] do {nothing}; ; flag[0]:=false;
PROCESS 1 flag[1]:=true; while flag[0] do {nothing};
flag[1]:=false; Dekkers Algorithm Third Attempt
Analysis P0set flag[0]=true; P1set flag[1]=true; P0do: while
flag[1]; block P1do: while flag[0]; block =>causing deadlock
Dekkers Algorithm Fourth Attempt
PROCESS 0 flag[0]:=true; while flag[1] do begin flag[0] :=false; ;
end; ; flag[0]:=false; PROCESS 1 flag[1]:=true; while flag[0] do
begin flag[1] :=false; ; end; flag[1]:=false; Dekkers Algorithm
Fourth Attempt
Analysis P0set flag[0]=true; P1set flag[1]=true; P0do, while
flag[1]; P1do,while flag[0]; P0set flag[0]=false; P1set
flag[1]=false; P1set flag[1]=true; Repeat this sequence forever,
both process cannot enter the critical section. (not deadlock)
Dekkers Algorithm A Correct Solution
Var flag:array[0..1] of boolean; turn:0..1; Procedure P1; Begin
repeat flag[1]:=true; while flag[0] do if turn=0 then begin
flag[1]:=false; while turn=0 do {nothing} end ; turn:=0;
flag[1]:=false; forever End; Procedure P0; Begin repeat
flag[0]:=true; while flag[1] do if turn=1 then begin
flag[0]:=false; while turn=1 do {nothing} end ; turn:=1;
flag[0]:=false; forever End; Dekkers Algorithm A Correct
Solution
Begin flag[0]:=false; flag[1]:=false; turn:=1; parbegin P0; P1;
parend end Dekkers Algorithm A Correct Solution
Analysis (P0 Set flag[0]:=true; Do, while flag[1] false, P0
enters,when finish,set turn:=1; flag[0]:=false; true, check value
of turn 1,busy waiting 0, P1 has finished (but has not modified
flag), P0 enters Peterson Algorithm Var flag:array[0..1] of
boolean; turn: 0..1;
Procedure P0; Begin repeat flag[0]:=true; turn:=1; while flag[1]
and turn =1 do {nothing} flag[0]:=false; forever End; Procedure P1;
Begin repeat flag[1]:=true; turn:=0; while flag[0] and turn =0 do
{nothing} flag[1]:=false; forever End; Peterson Algorithm Begin
flag[0]:=false; flag[1]:=false; turn:=1;
parbegin P0; P1; parend End. Peterson Algorithm Analysis Flag is
used for who wants to enter.
Turn is used to avoid deadlock. Mutual Exclusion: Hardware
Support
Interrupt Disabling Only suitable for single processor, and the
efficiency of execution is degraded. Special Machine Instructions :
These actions are performed in a single instruction cycle, they are
not subject to interference from other instrcutions. Test and Set
Exchange Instruction Test and Set (P205) function testset ( var
i:integer ) : boolean ;
begin if i = 0 then i := 1; testset := true; end else testset
:=false; end. Notes: when i=0 presents resource is free. When i=1
presents resource is used Application(fig 5.7a) program
mutualexclusion;
const n= ; (*number of processes*); var bolt: integer; procedure P(
i:integer); begin repeat repeat {nothing} until testset(bolt);
bolt:=0; forever end; begin (*main program*) bolt:=0; parbegin
p(1); p(2); p(n); parend end. Exchange(P205) procedure exchange (
var r :register; var m :memory );
var temp; begin temp := m; m := r; r := temp; end. Application
(Fig5.7b) begin (*main program*) bolt:=0; parbegin p(1);
program mutualexclusion const n=; (*number of processes*); var
bolt: integer; procedure P(i:integer) var keyi: integer; begin
repeat keyi:=1; repeat exchange (keyi, bolt) until keyi=0;
exchange(keyi, bolt); forever end; begin (*main program*) bolt:=0;
parbegin p(1); p(2); p(n); parend end. Characteristics of Hardware
Command (P207)
Adv. Simple Be suitable for multi-processes to share memory in
single process or multi processors environment Be suitable for
accessing multi-critical sections. Every critical section can
define a variable. Disadv. Busy for waiting Maybe induce hungry
Maybe induce deadlock. Exp, when a process, which is in a critical
section, is interrupted, then deadlock happens Semaphores (OS
provide)
Function: Be used to implementmutual exclusion and synchronization
Operations of semaphore: Assuming the semaphore is a integer
Semaphore can be initialed as a no-negative integer wait(s): s1; if
s0, block the correspondent process signal(s): s1; if s0, wakeup
the correspondent process Types of Semaphore General semaphore:
Fig5.8 Binary semaphore: Fig5.9 F5.8 A Definition of Semaphore
Primitives
Type semaphore= record count: integer queue:list of process FCFS
end; Var s: semaphore Wait(s): s.count=s.count-1; if s.count