Top Banner
1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization Advanced Operating System Fall 2012
21

1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization Advanced Operating System Fall 2012.

Jan 18, 2016

Download

Documents

Amelia Hardy
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization Advanced Operating System Fall 2012.

1

Lecture 8: Concurrency: Mutual Exclusion and Synchronization

Advanced Operating SystemFall 2012

Page 2: 1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization Advanced Operating System Fall 2012.

2

Concurrency

An OS has many concurrent processes that run in parallel but share common access

Race Condition: A situation where several processes access and manipulate the same data concurrently and the outcome of the execution depends on the particular order in which the access takes place.

Page 3: 1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization Advanced Operating System Fall 2012.

3

Example for Race condition

Suppose a customer wants to book a seat on UAL 56. Ticket agent will check the #-of-seats. If it is greater than 0, he will grab a seat and decrement #-of-seats by 1.

UAL 56: #-of-seats=12Main memory

Terminal Terminal Terminal…Ticket Agent 1Ticket Agent 2 Ticket Agent n

Page 4: 1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization Advanced Operating System Fall 2012.

4

Example for Race condition(cont.)

Ticket Agent 1

P1: LOAD #-of-seatsP2: DEC 1P3: STORE #-of-seats

Ticket Agent 2

Q1: LOAD #-of-seatsQ2: DEC 1Q3: STORE #-of-seats

Ticket Agent 3

R1: LOAD #-of-seatsR2: DEC 1R3: STORE #-of-seats

Suppose, initially, #-of-seats=12Suppose instructions are interleaved as P1,Q1,R1,P2,Q2,R2,P3,Q3,R3The result would be #-of-seats=11, instead of 9

To solve the above problem, we must make sure that:P1,P2,P3 must be completely executed before we execute Q1 or R1, orQ1,Q2,Q3 must be completely executed before we execute P1 or R1, orR1,R2,R3 must be completely executed before we execute P1 or Q1.

Page 5: 1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization Advanced Operating System Fall 2012.

5

Critical Section Problem

Goal: To program the processes so that, at any moment of time, at most one of the processes is in its critical section.

Prefix0

CS0

Suffix0

P0

Prefix1

CS1

Suffix1

P1

Prefixn-1

CSn-1

Suffixn-1

Pn-1

Critical section: a segment of code in which the process may be changing common variables, updating a table, writing a file, and so on.

Page 6: 1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization Advanced Operating System Fall 2012.

6

Solution to Critical-Section Problem

Any facility to provide support for mutual exclusion should meet the following requirements:

1. Mutual exclusion must be enforced: Only one process at a time is allowed into its critical section

2. A process that halts in its noncritical section must do so without interfering with other processes.

3. A process waiting to enter its critical section cannot be delayed infinitely

4. When no process is in a critical section, any process that requests entry to its critical section must be permitted to enter without delay.

5. No assumption are made about the relative process speeds or the number of processors.

6. A process remains inside its critical section for a finite time only.

Page 7: 1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization Advanced Operating System Fall 2012.

7

Three Environments

1. There is no central program to coordinate the processes. The processes communicate with each other through global variable.

2. Special hardware instructions3. There is a central program to coordinate the

processes.

Page 8: 1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization Advanced Operating System Fall 2012.

8

Three Environments

1. There is no central program to coordinate the processes. The processes communicate with each other through global variable.

2. Special hardware instructions3. There is a central program to coordinate the

processes.

Page 9: 1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization Advanced Operating System Fall 2012.

9

1st AttemptStart with just 2 processes, P0 and p1

Global variable turn, initially turn=0

Prefix0

While (turn0) do {} CS0

turn=1 suffix0

Prefix1

While (turn1) do {} CS1

turn=0 suffix1

The processes take turn to enter its critical sectionIf turn=0, P0 entersIf turn=1, P1 enters

This solution guarantees mutual exclusion.

But the drawback is that the method is not fair, because P0 is priviledged.Worse yet, until P0 executed its CS, P1 is blocked.

Page 10: 1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization Advanced Operating System Fall 2012.

10

2st AttemptGlobal variable flag[0] and flag[1], initially flag[0] and flag[1] are both false

Prefix0

While (flag[1]) do {}flag[0]=true CS0

flag[0]=false suffix0

Prefix1

While (flag[0]) do {}flag[1]=true CS1

flag[1]= false suffix1

If P0 is in critical section, flag[0] is true; If P1 is in critical section, flag[1] is true

If one process leaves the system, it will not block the other process.

However, mutual exclusion is not guaranteed.P0 executes the while statement and finds that flag[1] is false;P1 executes the while statement and finds that flag[0] is false.P0 sets flag[0] to true and enters its critical section;P1 sets flag[1] to true and enters its critical section.

Page 11: 1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization Advanced Operating System Fall 2012.

11

3st AttemptGlobal variable flag[0] and flag[1], initially flag[0] and flag[1] are both false

Prefix0

flag[0]=trueWhile (flag[1]) do {} CS0

flag[0]=false suffix0

Prefix1

flag[1]=trueWhile (flag[0]) do {} CS1

flag[1]= false suffix1

If P0 is in critical section, flag[0] is true; If P1 is in critical section, flag[1] is true

Guarantees mutual exclusion.

But mutual blocking can occur.P0 sets flag[0] to be true;P1 sets flag[1] to be true;Both will be hung in the while loop.

Page 12: 1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization Advanced Operating System Fall 2012.

12

4st AttemptGlobal variable flag[0] and flag[1], initially flag[0] and flag[1] are both false

Prefix0

L0: flag[0]=trueIf (flag[1]) then {

flag[0]=false;goto L0}

CS0

flag[0]=false suffix0

Prefix1

L1: flag[1]=trueIf (flag[0]) then {

flag[1]=false;goto L1}}

CS1

flag[1]= false suffix1

Guarantees mutual exclusion.

mutual blocking can occur if they execute at the same speed.

Page 13: 1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization Advanced Operating System Fall 2012.

13

Correct Solution (Dekker’s Alg) – The first correct mutual exclusion alg (early 1960’s)

Initially, flag[0]=flag[1]=false; turn=0

Prefix0

flag[0]=truewhile (flag[1]) do { if (turn=1){

flag[0]=false;while(turn=1) do{}flag[0]=true;}

}CS0

turn=1flag[0]=falsesuffix0

Prefix1

flag[1]=truewhile (flag[0]) do { if (turn=0){

flag[1]=false;while(turn=0) do{}flag[1]=true;}

}CS1

turn=0flag[1]=falsesuffix1

Page 14: 1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization Advanced Operating System Fall 2012.

14

Peterson’s Algorithm for 2 processes – The simplest and most compact mutual exclusion alg.

Initially, flag[0]=flag[1]=false

Prefix0

flag[0]=trueturn=1while (flag[1] and turn=1) do{}CS0

flag[0]=falsesuffix0

Prefix1

flag[1]=trueturn=0while (flag[0] and turn=0) do{} CS1

flag[1]=falsesuffix1

Page 15: 1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization Advanced Operating System Fall 2012.

15

Solution for n processes Global Variable

1. Flag[0..n-1] – array of size n.

2. Turn. Initially, Turn=some no. between 0 and n-1

Idle if Pi is outside CsiWant-in if Pi wants to be in CSiin-CS if Pi is in CSi

Flag[i]=

Page 16: 1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization Advanced Operating System Fall 2012.

16

Solutions for n processes

PiPrefixi

Repeat Flag[i]=want-in; j=Turn; while ji do {if Flag[j]idle then j=Turn else j=(j+1) mod n} Flag[i]=in-CS j=0 while (j<n) and (j=i or Flag[j]in-CS) do {j=j+1}Until (jn) and (Turn=i or Flag[Turn]=idle)Turn=i;

CSi

j=(Turn+1)mod nWhile (jTurn) and (Flag[j]=idle) do{j=(i+1) mod n}Turn=jFlag[i]=idle

Page 17: 1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization Advanced Operating System Fall 2012.

17

Three Environments

1. There is no central program to coordinate the processes. The processes communicate with each other through global variable.

2. Special hardware instructions3. There is a central program to coordinate the

processes.

Page 18: 1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization Advanced Operating System Fall 2012.

18

Hardware SupportDisable interrupt CSEnable interrupt

Won’t work if we have multiprocessors

Page 19: 1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization Advanced Operating System Fall 2012.

19

Special Machine Instructions Modern machines provide special atomic hardware instructions

Atomic = non-interruptable Either test memory word and set value Or swap contents of two memory words

Page 20: 1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization Advanced Operating System Fall 2012.

20

TS – Test and SetBoolean TS(i)= true if i=0; it will also set i to 1 false if i=1

Initially, lock=0 Pi

Prefixi

While(¬ TS(lock)) do {} CSi

Lock=0 suffixi

It is possible that a process may starve if 2 processes enter the critical section arbitrarily often.

Page 21: 1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization Advanced Operating System Fall 2012.

21

End of lecture 8

Thank you!