Part III Synchronization Critical Section and Mutual Exclusion The - - PowerPoint PPT Presentation

part iii synchronization
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

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

slide-2
SLIDE 2

2

Pr Proc

  • ces

ess s Sy Sync nchr hron

  • niza

zation

  • n To

Topi pics cs

  • Why is synchronization needed?
  • Race Conditions
  • Critical Sections
  • Pure Software Solutions
  • Hardware Support
  • Semaphores
  • Race Conditions, Revisited
  • Monitors
slide-3
SLIDE 3

3

Syn Synch chron

  • niza

zation tion Ne Need eded ed! 1/ 1/6

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

  • n inter

erlea leaving ving

slide-4
SLIDE 4

4

Syn Synch chron

  • niza

zation tion Ne Need eded ed! 2/ 2/6

int a[3] = { 3, 4, 5};

Process 1 Process 2

a[1] = a[0] + a[1]; a[2] = a[1] + a[2];

  • If process 1 updates a[1] first, a[1] is 7, and a[ ]={3,7,5}
  • Then, process 2 uses the new a[1] to computes a[2], and

a[ ]={3,7,12}

  • If process 2 uses a[1] first, now a[2] is 9, and a[ ]={3,4,9}
  • Then, process 1 computes a[1], and a[ ]={3,7,9}

Res esul ults ts ar are e no non-de dete termini nist stic! c!

slide-5
SLIDE 5

5

Syn Synch chron

  • niza

zation tion Ne Need eded ed! 3/ 3/6

int Count = 10; Process 1 Process 2 Count++; Count--;

Count = 9, 10 or 11?

Higher-level language statements are not not atomic

slide-6
SLIDE 6

6

Sy Sync nchr hron

  • niza

zatio tion n Ne Need eded ed! 4/ 4/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

  • me

e no non-de dete termini nist stic! c!

instr structi ction n level el executi tion

  • n inter

erlea leaving ving

slide-7
SLIDE 7

7

Syn Synch chron

  • niza

zation tion Ne Need eded ed! 5/ 5/6

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

  • verwrites the previous value 11

Always ays use instr truction uction level el inter erlea leaving ving to show w race condit itions ions

slide-8
SLIDE 8

8

Syn Synch chron

  • niza

zation tion Ne Need eded ed! 6/ 6/6

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

  • verwrites the previous value 9

Always ays use instr truction uction level el inter erlea leaving ving to show w race condit itions ions

slide-9
SLIDE 9

9

Ra Race ce Con Condi dition

  • ns
  • A Rac

ace e Con

  • ndi

diti tion

  • n occurs, if

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.

  • Synchronization is needed to prevent race

conditions from happening.

  • Synchronization is a difficult topic. Don’t miss

classes; otherwise, you will miss a lot of things.

slide-10
SLIDE 10

10

Ex Exec ecut ution

  • n Se

Sequ quen ence ce Not Notes es

  • You
  • u sho

shoul uld d us use e ins nstr truc ucti tion

  • n l

lev evel el int nter erlea eavi ving ng to to de demon

  • nst

stra rate te the the ex exist sten ence ce o

  • f ra

race ce co cond nditi tion

  • ns, because

a) higher-level language statements are not atomic and can be switched in the middle

  • f execution

b) instruction level interleaving can show clearly the “sharing” of a resource among processes and threads.

slide-11
SLIDE 11

11

Cr Critica ical l Se Sect ction

  • n
  • A cr

criti tica cal sec secti tion

  • n, CS

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

slide-12
SLIDE 12

12

Mu Mutua ual Ex Excl clus usion

  • n
  • To avoid race conditions, the execution of

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).

  • The cr

criti tica cal-se sect ction

  • n pr

prob

  • blem

em is to design a protocol with which processes can use to cooperate and ensure mutual exclusion.

slide-13
SLIDE 13

13

Th The Cr e Critica cal l Se Sect ction

  • n Pr

Prot

  • toc
  • col
  • l
  • A critical section protocol

consists of two parts: an entry section and an exit section.

  • Between them is the

critical section that must run in a mutually exclusive way.

do { } while (1); entry section exit section critical section

slide-14
SLIDE 14

14

Good d Solutions tions to to the the CS CS Pro roblem lem

  • A good solution to the critical section problem

must satisfy the following three conditions: Mutual Exclusion Progress Bounded Waiting

  • Moreover, the solution cannot depend on

CPU’s relative speed, timing, scheduling policy and other external factors.

slide-15
SLIDE 15

15

Mu Mutua ual Ex Excl clus usion

  • n
  • If a process P is executing in its critical section,

no other processes can be executing in their corresponding critical sections.

  • The entry protocol should be able to block

processes that wish to enter but cannot.

  • When the process that is executing in its critical

section exits, the entry protocol must be able to know this fact and allows a waiting process to enter.

slide-16
SLIDE 16

16

Pr Prog

  • gres

ess

  • If no

no process is executing in its critical section and some processes want to enter their corresponding critical sections, then

  • 1. Only those processes that are waiting to enter

can participate in the competition (to enter their critical sections) and no other processes can influence this decision.

  • 2. This decision cannot be postponed indefinitely

(i.e., finite decision time). Thus, one of the waiting processes can enter its critical section.

slide-17
SLIDE 17

17

Bounded Bounded Waiting Waiting

  • After a process made a request to enter its

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.

  • Fi

Fini nite te is no s not t th the e sa same e as as b bou

  • und

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).

slide-18
SLIDE 18

18

Pr Prog

  • gres

ess s vs

  • vs. B

Bou

  • und

nded ed W Wai aiting ng

  • Pro

rogr gres ess does not imply Bou

  • und

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.

  • Bo

Boun unde ded d Wai aiti ting ng does not imply Pr Prog

  • gre

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).

  • Therefore, Pro

rogr gres ess and Bou

  • und

nded ed W Wai aiti ting ng are independent of each other.

slide-19
SLIDE 19

19

A Fe A Few R w Rel elat ated ed Te Terms: s: 1/ 1/7

  • Dea

eadl dloc

  • ck-Freedom

Freedom: If two or more processes are trying to enter their critical sections,

  • ne of them will eventually enter. This is

Pro rogr gres ess without the “outsiders having no influence” condition.

  • Since the enter section is able to select a process

to enter, the decision time is certainly finite.

slide-20
SLIDE 20

20

A Fe A Few R w Rel elat ated ed Te Terms: s: 2/ 2/7

  • r-Bou
  • und

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

  • ther processes are allowed to enter.
  • Therefore, bounded waiting means there is a r

such that the waiting is r-bounded.

slide-21
SLIDE 21

21

A Fe A Few R w Rel elat ated ed Te Terms: s: 3/ 3/7

  • FI

FIFO FO: No process that is about to enter its critical section can pass an already waiting

  • process. FI

FIFO FO is usually referred to as 0- bounded bounded.

  • Li

Line near ar-Wai aiti ting ng (1-Bou

  • und

nded ed Wai aiti ting ng): No process can enter its critical section twice while there is a process waiting.

slide-22
SLIDE 22

22

A Fe A Few R w Rel elat ated ed Te Terms: s: 4/ 4/7

  • Sta

tarv rvat ation

  • n-Freedom

Freedom: If a process is trying to enter its critical section, it will eventually enter.

  • Que

uest stion

  • ns:
  • 1. Does starvation-freedom imply deadlock-freedom?
  • 2. Does starvation-freedom imply bounded-waiting?
  • 3. Does bounded-waiting imply starvation-freedom?
  • 4. Does bounded-waiting AND deadlock-freedom imply

starvation-freedom?

slide-23
SLIDE 23

23

A Fe A Few R w Rel elat ated ed Te Terms: s: 5/ 5/7

  • Que

uest stion

  • n (

(1) 1): Does starvation-freedom imply deadlock-freedom?

  • Yes

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

  • finite. Otherwise, all processes would wait in the

enter section.

slide-24
SLIDE 24

24

A Fe A Few R w Rel elat ated ed Te Terms: s: 6/ 6/7

  • Que

uest stion

  • n (

(2) 2): Does starvation-freedom imply bounded-waiting?

  • No!
  • ! This is because the waiting time may not be

bounded even though each process can enter its critical section.

slide-25
SLIDE 25

25

A Fe A Few R w Rel elat ated ed Te Terms: s: 7/ 7/7

  • Que

uest stion

  • n (

(3) 3): Does bounded-waiting imply starvation-freedom?

  • No.
  • . Bounded-Waiting does not say if a process

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).

  • We need Pro

rogr gres ess + Bou

  • und

nded ed-Wai aiti ting ng to imply Sta tarv rvat ation

  • n-Fr

Free eedom

  • m (Que

uest stion

  • n (4)

(4)). In fact, Pro rogr gres ess + Bou

  • und

nded ed-Wai aiti ting ng is stronger than Sta tarv rvat ation

  • n-Freedom
  • Freedom. Why

hy?

slide-26
SLIDE 26

26

The End