22
1 Chapter 6: Concurrency: Mutual Exclusion and Synchronization Operating System Spring 2007 Chapter 6 of textbook

Chapter 6: Concurrency: Mutual Exclusion and Synchronization

Embed Size (px)

DESCRIPTION

Chapter 6: Concurrency: Mutual Exclusion and Synchronization. Operating System Spring 2007 Chapter 6 of textbook. Concurrency. An OS has many concurrent processes that run in parallel but share common access - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter 6: Concurrency: Mutual Exclusion and Synchronization

1

Chapter 6: Concurrency: Mutual Exclusion and Synchronization

Operating SystemSpring 2007

Chapter 6 of textbook

Page 2: Chapter 6: Concurrency: Mutual Exclusion and Synchronization

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: Chapter 6: Concurrency: Mutual Exclusion and Synchronization

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: Chapter 6: Concurrency: Mutual Exclusion and Synchronization

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: Chapter 6: Concurrency: Mutual Exclusion and Synchronization

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: Chapter 6: Concurrency: Mutual Exclusion and Synchronization

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 indefinitely

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: Chapter 6: Concurrency: Mutual Exclusion and Synchronization

7

Three Environments

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

1. Solution: Peterson’s algorithm2. not covered in this class

2. Special hardware instructions1. TS2. Exchange(not covered in this class)

3. There is a central program to coordinate the processes.

1. Semaphore

Page 8: Chapter 6: Concurrency: Mutual Exclusion and Synchronization

8

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 9: Chapter 6: Concurrency: Mutual Exclusion and Synchronization

9

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

Problem of this program: It is possible that a process may starve if 2 processes enter the critical section arbitrarily often.

Solution for the starvation won’t be covered in this class.

Page 10: Chapter 6: Concurrency: Mutual Exclusion and Synchronization

10

Semaphores A variable that has an integer value upon

which 3 operations are defined. Three operations:

1. A semaphore may be initialized to a nonnegative value

2. The wait operation decrements the semaphore value. If the value becomes negative, then the process executing the wait is blocked

3. The signal operation increments the semaphore value. If the value is not positive, then a process blocked by a wait operation is unblocked.

Other than these 3 operations, there is no way to inspect or manipulate semaphores.

Page 11: Chapter 6: Concurrency: Mutual Exclusion and Synchronization

11

Wait(s) and Signal(s) Wait(s) – is also called P(s)

{s=s-1;if (s<0) {place this process in a waiting queue}

} Signal(s) – is also called V(s)

{s=s+1;if(s0) {remove a process from the waiting

queue}}

Page 12: Chapter 6: Concurrency: Mutual Exclusion and Synchronization

12

Semaphore as General Synchronization Tool

Counting semaphore – integer value can range over an unrestricted domain

Binary semaphore – integer value can range only between 0 and 1; can be simpler to implement

Also known as mutex locks Wait B(s) s is a binary semaphore

{ if s=1 then s=0 else block this process}

Signal B(s){ if there is a blocked process then unblock a process else s=1}

Can implement a counting semaphore S as a binary semaphore

Page 13: Chapter 6: Concurrency: Mutual Exclusion and Synchronization

13

Note

The wait and signal primitives are assumed to be atomic; they cannot be interrupted and each routine can be treated as an indivisible step.

Page 14: Chapter 6: Concurrency: Mutual Exclusion and Synchronization

14

Mutual Exclusion provided by Semaphores

Semaphore S; // initialized to 1

Pi

prefixi

wait (S);

CSi

signal (S);

suffixi Signal B(s){ if there is a blocked process

then unblock a process

else s=1}

Wait B(s) s is a binary semaphore{ if s=1

then s=0 else block this

process}

Page 15: Chapter 6: Concurrency: Mutual Exclusion and Synchronization

15

Two classical examples

Producer and Consumer Problem Readers/Writers Problem

Page 16: Chapter 6: Concurrency: Mutual Exclusion and Synchronization

16

Two classical examples

Producer and Consumer Problem Readers/Writers Problem

Page 17: Chapter 6: Concurrency: Mutual Exclusion and Synchronization

17

Producer and Consumer Problem Producer can only put something in when there is an

empty buffer Consumer can only take something out when there is a

full buffer Producer and consumer are concurrent processes

0

N-1

N buffersproducer

consumer

Page 18: Chapter 6: Concurrency: Mutual Exclusion and Synchronization

18

Producer and Consumer Problem(cont.) Global Variable

1. B[0..N-1] – an array of size N (Buffer)2. P – a semaphore, initialized to N3. C – a semaphore, initialized to 0

Local Variable1. In – a ptr(integer) used by the producer, in=0 initially2. Out – a ptr(integer) used by the consumer, out=0 initially

Producer Process

producer: produce(w) wait(p) B[in]=w in=(in+1)mod N signal(c) goto producer

Consumer Process

consumer: wait(c) w=B[out] out=(out+1)mod N signal(p) consume(w) goto consumer

W is a local buffer used by the producer to produce

W is a local buffer used by the consumer to store the item to be consumed

Page 19: Chapter 6: Concurrency: Mutual Exclusion and Synchronization

19

Two classical examples

Producer and Consumer Problem Readers/Writers Problem

Page 20: Chapter 6: Concurrency: Mutual Exclusion and Synchronization

20

Readers/Writers Problem

Suppose a data object is to be shared among several concurrent processes. Some of these processes want only to read the data object, while others want to update (both read and write)

Readers – Processes that read only Writers – processes that read and write

If a reader process is using the data object, then other reader processes are allowed to use it at the same time.

If a writer process is using the data object, then no other process (reader or writer) is allowed to use it simultaneously.

Page 21: Chapter 6: Concurrency: Mutual Exclusion and Synchronization

21

Solve Readers/Writers Problem using wait and signal primitives(cont.)

Global Variable: Wrt is a binary semaphore, initialized to 1; Wrt is used by both readers and writers

For Reader Processes: Mutex is a binary semaphore, initialized to 1;Readcount is an integer variable,

initialized to 0 Mutex and readcount used by readers onlyReader Processes

Wait(mutex)Readcount=readcount+1If readcount=1 then wait(wrt)Signal(mutex);…Reading is performed…Wait(mutex)Readcount=readcount-1If readcount=0 then signal(wrt)Signal(mutex)

Writer Processes

Wait(wrt)…Writing is performed…Signal(wrt)

Page 22: Chapter 6: Concurrency: Mutual Exclusion and Synchronization

22

End

Thank you!