1
Part III Synchronization
Critical Section and Mutual Exclusion
Fall 2015
The question of whether computers can think is just like the question of whether submarines can swim Edsger W. Dijkstra
Part III Synchronization Critical Section and Mutual Exclusion The - - PowerPoint PPT Presentation
Part III Synchronization Critical Section and Mutual Exclusion The question of whether computers can think is just like the question of whether submarines can swim 1 Fall 2015 Edsger W. Dijkstra Pr Proc oces ess s Sy Sync nchr hron
1
Fall 2015
The question of whether computers can think is just like the question of whether submarines can swim Edsger W. Dijkstra
2
3
int a[3] = { 3, 4, 5};
Process 1 Process 2
a[1] = a[0] + a[1]; a[2] = a[1] + a[2];
a[3] = { 3, ?, ? } St State temen ment t level l executi tion
erlea leaving ving
4
int a[3] = { 3, 4, 5};
Process 1 Process 2
a[1] = a[0] + a[1]; a[2] = a[1] + a[2];
a[ ]={3,7,12}
Res esul ults ts ar are e no non-de dete termini nist stic! c!
5
int Count = 10; Process 1 Process 2 Count++; Count--;
Count = 9, 10 or 11?
Higher-level language statements are not not atomic
6
int Count = 10;
Process 1 Process 2
LOAD Reg, Count LOAD Reg, Count ADD #1 SUB #1 STORE Reg, Count STORE Reg, Count
The problem is that the execution flow may be switched in the middle. Res esul ults ts bec becom
e no non-de dete termini nist stic! c!
instr structi ction n level el executi tion
erlea leaving ving
7
LOAD 10 10 LOAD 10 10 SUB 9 10 ADD 11 10 STORE 11 11 STORE 9 9
Inst Reg Memory Inst Reg Memory
Process 1 Process 2
Always ays use instr truction uction level el inter erlea leaving ving to show w race condit itions ions
8
LOAD 10 10 ADD 11 10 LOAD 10 10 SUB 9 10 STORE 9 9 STORE 11 11
Inst Reg Memory Inst Reg Memory
Process 1 Process 2
Always ays use instr truction uction level el inter erlea leaving ving to show w race condit itions ions
9
ace e Con
diti tion
two or more processes/threads manipulate a shared resource concurrently, and the outcome of the execution depends on the particular order in which the access takes place.
conditions from happening.
classes; otherwise, you will miss a lot of things.
10
shoul uld d us use e ins nstr truc ucti tion
lev evel el int nter erlea eavi ving ng to to de demon
stra rate te the the ex exist sten ence ce o
race ce co cond nditi tion
a) higher-level language statements are not atomic and can be switched in the middle
b) instruction level interleaving can show clearly the “sharing” of a resource among processes and threads.
11
criti tica cal sec secti tion
CS, is a section of code in which a process accesses shared resources.
int count; // shared count++; count--; cout << count; These are critical sections since count is a shared resource
process 1 process 2 process 3
12
critical sections must be mut utua ually y ex excl clus usive ve (e.g., at most one process can be in its critical section at any time).
criti tica cal-se sect ction
prob
em is to design a protocol with which processes can use to cooperate and ensure mutual exclusion.
13
consists of two parts: an entry section and an exit section.
critical section that must run in a mutually exclusive way.
do { } while (1); entry section exit section critical section
14
Good d Solutions tions to to the the CS CS Pro roblem lem
must satisfy the following three conditions: Mutual Exclusion Progress Bounded Waiting
CPU’s relative speed, timing, scheduling policy and other external factors.
15
no other processes can be executing in their corresponding critical sections.
processes that wish to enter but cannot.
section exits, the entry protocol must be able to know this fact and allows a waiting process to enter.
16
no process is executing in its critical section and some processes want to enter their corresponding critical sections, then
can participate in the competition (to enter their critical sections) and no other processes can influence this decision.
(i.e., finite decision time). Thus, one of the waiting processes can enter its critical section.
17
critical section and before it is granted the permission to enter, there exists a bound bound on the number of turns that other processes are allowed to enter.
Fini nite te is no s not t th the e sa same e as as b bou
nded ed. The former means any value you can write down (e.g., billion, trillion, etc) while the latter means this value has to be no larger than a particular one (i.e., the bound).
18
rogr gres ess does not imply Bou
nded ed Wai aiti ting ng: Pro rogr gres ess says a process can enter with a finite decision time. It does not say which process can enter, and there is no guarantee for bounded waiting.
Boun unde ded d Wai aiti ting ng does not imply Pr Prog
ress ss: Even through we have a bound, all processes may be locked up in the enter section (i.e., failure of Pro rogr gres ess).
rogr gres ess and Bou
nded ed W Wai aiti ting ng are independent of each other.
19
eadl dloc
Freedom: If two or more processes are trying to enter their critical sections,
Pro rogr gres ess without the “outsiders having no influence” condition.
to enter, the decision time is certainly finite.
20
nded ed Wa Waiti ting ng: There exists a fixed value r such that after a process made a request to enter its critical section and before it is granted the permission to enter, no more than r
such that the waiting is r-bounded.
21
FIFO FO: No process that is about to enter its critical section can pass an already waiting
FIFO FO is usually referred to as 0- bounded bounded.
Line near ar-Wai aiti ting ng (1-Bou
nded ed Wai aiti ting ng): No process can enter its critical section twice while there is a process waiting.
22
tarv rvat ation
Freedom: If a process is trying to enter its critical section, it will eventually enter.
uest stion
starvation-freedom?
23
uest stion
(1) 1): Does starvation-freedom imply deadlock-freedom?
es! If every process can eventually enter its critical section, although waiting time may vary, it means the decision time of selecting a process is
enter section.
24
uest stion
(2) 2): Does starvation-freedom imply bounded-waiting?
bounded even though each process can enter its critical section.
25
uest stion
(3) 3): Does bounded-waiting imply starvation-freedom?
can actually enter. It only says there is a bound. For example, all processes are locked up in the enter section (i.e., failure of Pro rogr gres ess).
rogr gres ess + Bou
nded ed-Wai aiti ting ng to imply Sta tarv rvat ation
Free eedom
uest stion
(4)). In fact, Pro rogr gres ess + Bou
nded ed-Wai aiti ting ng is stronger than Sta tarv rvat ation
hy?
26