© janice regan, cmpt 300, may 2007 0 cmpt 300 introduction to operating systems mutual exclusion...

40
© Janice Regan, CMPT 300, May 2007 1 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

Upload: hope-powers

Post on 13-Dec-2015

222 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: © Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

© Janice Regan, CMPT 300, May 2007 1

CMPT 300Introduction to Operating

Systems

Mutual Exclusion

Mutexes, Semaphores

Page 2: © Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

© Janice Regan, CMPT 300, May 2007 2

General form, mutual exclusion No matter how we implement mutual exclusion,

conceptually the following happens in each process using the resource protected my mutal exclusion

NonCriticalRegion()StartCriticalRegion()CriticalRegion()EndCriticalRegion()NonCritical Regtion()

Page 3: © Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

© Janice Regan, CMPT 300, May 2007 3

Implementation Now we have the basic ideas we need How do we actually implement mutual exclusion? There are several approaches

Interrupt disabling Lock variables Strict alternation Special instructions Peterson’s solution Message passing Semaphores and Mutexes Monitors

Page 4: © Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

© Janice Regan, CMPT 300, May 2007 4

More about implementation We have looked at the first five approaches (both

hardware and software solutions). All these solutions Are based on indirect sharing Processes do not directly communicate Processes may be competing or indirectly

collaborating Busy waiting (or interrupt disabling) is used to

prevent processes from entering critical zones when the resource is busy.

Priority inversion is a possible problem How does our solution change when we find methods

that will avoid busy waiting

Page 5: © Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

© Janice Regan, CMPT 300, May 2007 5

No busy waiting: sleep/wakeup How do we avoid busy waiting? For cooperating processes can use

sleep/wakeup (based on signaling) Need some method to make a process wait for

another process to finish its critical region Put process to ‘sleep’, suspend the process and stop

execution Process does not use CPU while it is ‘asleep’ and

waiting its turn When the process’s turn arrives then ‘wakeup’ the

process and allow it to enter and execute its critical region

Page 6: © Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

© Janice Regan, CMPT 300, May 2007 6

How to How to we put a process to ‘sleep’, or ‘wakeup’

a process? Use signaling to a mutual exclusion structure

(variable) called a mutex (a binary semaphore) or semaphore (counting semaphore)

The Semaphore (mutex) is the structure including the shared variable that is shared between processes. The execution of a semaphore is an atomic action.

Use system calls (or thread library) to suspend or restart the process

Two signaling functions are needed (to make system calls) one to put a process to sleep one to wake it up

Page 7: © Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

© Janice Regan, CMPT 300, May 2007 7

Mutex vs Semaphore When using a mutex (binary semaphore)

Only a single resource can be managed (for example one DMA)

Only useful for mutual exclusion A mutex is a special case of a semaphore

When using a semaphore May manage more than one device of the same type

with a counting semaphore Can be used for queuing (sharing or multiple similar

resources)

Page 8: © Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

© Janice Regan, CMPT 300, May 2007 8

Counting semaphore signalsStruct semaphore{

int countblocked;queueType queueUpOrDown;

};

void semWait( semaphore s) {s.countblocked--;if(s.countblocked < 0) {

/*put process in blocked.queue

block process */}

}

void semSignal( semaphore s) {s.countblocked++;if(s.countblocked <= 0) {

/*remove a process from blocked.queue

put process in ready queue*/}

}

Page 9: © Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

© Janice Regan, CMPT 300, May 2007 9

Semaphore wait operation: go to sleep The semWait operation

Decrements the semaphore value If the value is >=0, the process is allowed to

run its critical region If the value is negative then the process is

blocked (put to sleep) and placed in the blocked queue

Page 10: © Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

© Janice Regan, CMPT 300, May 2007 10

Semaphore signal operation: wakeup The semSignal operation

Increments the semaphore value If the semaphore value is not positive (<=0)

the first process in the blocked queue is woken up and placed in the ready queue

Page 11: © Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

© Janice Regan, CMPT 300, May 2007 11

Example: counting semaphore Assume 4 processes A, B, C, D Processes A B C and D share a single resource

semWait(A) // A claims resource or queues for it A uses resource semSignal(A) // A gives back resource

Before beginning process A has run and is presently using the resource (may be waiting for interrupt, or ready to continue)

Process A reaches the end of its time slice and is replaced by D Begin with D running. D executes a semWait( ) while preparing to

use the shared resource. The semWait( ) decrements the semaphore. Because the

semaphore is now -1(<0) process D is blocked and placed in the blocked queue. (D has not yet accessed the resource)

Page 12: © Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

© Janice Regan, CMPT 300, May 2007 12

Counting semaphore operation

C D

processorA

Semaphore1

Blocked queue Ready queueB

C D

processorA

Semaphore0

Blocked queue Ready queue

SemWait for A: Step 1 Decrement semaphore

B

Page 13: © Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

© Janice Regan, CMPT 300, May 2007 13

Counting semaphore operation

B C

processorD

Semaphore0

Blocked queue Ready queue

D A B

processorC

Semaphore-1

Blocked queue Ready queue

After SemWait for D

A

Proc A ready to use resource but swapped at end of time slice

Page 14: © Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

© Janice Regan, CMPT 300, May 2007 14

Example: counting semaphore This allows Process C to be loaded into CPU C executes a semWait( ) while preparing to access the

shared resource The semWait( ) decrements the semaphore. Because the semaphore is now -2(<0) the process is

blocked and placed in the blocked queue. This allows Process B to be loaded into the CPU B executes a semWait( ) while preparing to access the

shared resource The semWait( ) decrements the semaphore. Because the semaphore is now -3(<0) the process is

blocked and placed in the blocked queue.

Page 15: © Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

© Janice Regan, CMPT 300, May 2007 15

Counting semaphore operation

C D A

processorB

semaphore-2

Blocked queue Ready queue

CB D

processorA

semaphore-3

Blocked queue Ready queue

After semWait for C

After semWait for B

Page 16: © Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

© Janice Regan, CMPT 300, May 2007 16

Example: counting semaphore This allows Process A to be loaded into the CPU. Process A has already completed its semWait() and is

ready to complete use the resource. A uses the resource and then executes a semSignal( )

to indicate that it no longer needs the resource The semSignal( ) increments the semaphore. Because the semaphore is now -2(<=0) the first process

in the blocked queue, D, is woken up and placed at the end of the ready queue.

Process A continues until it is removed by the scheduler an replaced with process D (from the ready queue).

Page 17: © Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

© Janice Regan, CMPT 300, May 2007 17

Counting Semaphore operation

B C D

processorA

Semaphore-2

Blocked queue Ready queue

B C A

processorD

Semaphore-2

Blocked queue Ready queue

After semSignal for A

After A completes its time slice

Page 18: © Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

© Janice Regan, CMPT 300, May 2007 18

Example: counting semaphore This allows Process D to be loaded into the CPU. Process D has already completed its semWait() and is

ready to use the resource. D uses the resource and then executes a semSignal( )

to indicate that it no longer needs the resource The semSignal( ) increments the semaphore. Because the semaphore is now -1(<=0) the first process

in the blocked queue, C, is woken up and placed at the end of the ready queue.

Process D continues until it is removed by the scheduler an replaced with process A (from the ready queue). D is placed at the end of the ready queue.

Page 19: © Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

© Janice Regan, CMPT 300, May 2007 19

Counting Semaphore operation

B C A

processorD

Semaphore-1

Blocked queue Ready queue

B D C

processorA

Semaphore-1

Blocked queue Ready queue

After semSignal for D

After D completes its time slice

Page 20: © Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

© Janice Regan, CMPT 300, May 2007 20

Example: counting semaphore This allows Process A to be loaded into the CPU. Process A continues until it is removed by the scheduler

an replaced with process C (from the ready queue). A goes to the end of the ready queue

Process C has already completed its semWait() and is ready to use the resource.

C uses the resource and then executes a semSignal( ) to indicate that it no longer needs the resource

The semSignal( ) increments the semaphore. Because the semaphore is now 0(<=0) the first process

in the blocked queue, B, is woken up and placed at the end of the ready queue.

Page 21: © Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

© Janice Regan, CMPT 300, May 2007 21

Counting Semaphore operation

B A D

processorC

Semaphore-1

Blocked queue Ready queue

AB D

processorC

Semaphore0

Blocked queue Ready queue

After A completes its time slice

After C’s semSignal

Page 22: © Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

© Janice Regan, CMPT 300, May 2007 22

Example: counting semaphore Process C continues until it is removed by the scheduler and

replaced with process D (from the ready queue). Process C will be placed at the end of the ready queue

Process D continues until it is removed by the scheduler an replaced with process A (from the ready queue). Process D will be placed at the end of the ready queue

Process A continues until it is removed by the scheduler an replaced with process B (from the ready queue). Process A will be placed at the end of the ready queue

Process B has already completed its semWait() and is ready to use the resource. After using the resource process C executes a semSignal().

The semSignal( ) increments the semaphore. Because the semaphore is now 1 (not <=0) execution of process B

continues

Page 23: © Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

© Janice Regan, CMPT 300, May 2007 23

Counting semaphore operation

B C A

processorD

Semaphore0

Blocked queue Ready queue

CD B

processorA

Semaphore1

Blocked queue Ready queue

After C completes its time slice

After D completes its time slice

Page 24: © Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

© Janice Regan, CMPT 300, May 2007 24

Counting semaphore operation

D A C

processorB

Semaphore0

Blocked queue Ready queue

DA C

processorB

Semaphore1

Blocked queue Ready queue

semSignal for B

After A completes its time slice

Page 25: © Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

Using Semaphores In the previous example we started with the

semaphore value being 1. If we were sharing 5 DMAs, we could have set

the initial value of the semaphore to 5. Then each time a process asked for a DMA it would be given one and the number of available DMAs would be decremented. When the number of available DMAs reached 0 subsequent processes would be blocked until one of the DMAs completed and signaled the semaphore

© Janice Regan, CMPT 300, May 2007 25

Page 26: © Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

© Janice Regan, CMPT 300, May 2007 26

Mutex signalsStruct mutex{

enum[zero, one] runBlock;queueType queueRunOrBlock;

};

void mutexWait( mutex s) {if(s.runBlock == 1) {

s.runBlock = 0;}else {

//put process in sleep.queue

//block process}

}

void mutexSignal( mutex s) {if(sleepQueueEmpty() ) {

s.runBlock= 1;}else {

//remove a process from sleep.queue

//put process in ready queue}

}

Page 27: © Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

© Janice Regan, CMPT 300, May 2007 27

Mutex wait operation: go to sleep

The mutexWait operation Checks the semaphore value If the value is 1, the value is changed to 0

and the process is allowed to run its critical region

If the value is 0 then the process is blocked (put to sleep) and placed in the blocked queue

Page 28: © Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

© Janice Regan, CMPT 300, May 2007 28

Mutex signal operation: wakeup

The mutexSignal operation Checks if there are any processes that are

presently blocked (in the blocked queue) If there are processes in the blocked

queue,unblock the first process in the queue If there are no processes in the blocked

queue set the semaphore value to 1

Page 29: © Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

© Janice Regan, CMPT 300, May 2007 29

Mutex operation

C D

processoridle

Mutex1

Blocked queue Ready queue

B C

processorD

Mutex1

Blocked queue Ready queue

Initial State

D begins to run

A B

A

Page 30: © Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

© Janice Regan, CMPT 300, May 2007 30

Mutex operation

B C

processorD

Mutex0

Blocked queue Ready queueA

A B

processorC

Mutex0

Blocked queue Ready queue

MutexWait for D: Step 1 Check and Set Mutex to 0

D

Proc D ready to use resource but swapped at end of time slice

Check mutex value then set to 0

Page 31: © Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

© Janice Regan, CMPT 300, May 2007 31

Mutex operation

A B

processorC

Mutex0

Blocked queue Ready queueD

C D A

processorB

Mutex0

Blocked queue Ready queue

MutexWait for C: Step 1 Check Mutex is 0

Mutex wait for C: step 2 Add C to blocked queue

Check mutex value

Page 32: © Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

© Janice Regan, CMPT 300, May 2007 32

Mutex operation

C D A

processorB

Mutex0

Blocked queue Ready queue

B C D

processorA

Mutex0

Blocked queue Ready queue

MutexWait for B: Step 1 Check Mutex is 0

Mutex wait for B step 2 Add B to blocked queue

Check mutex value

Page 33: © Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

© Janice Regan, CMPT 300, May 2007 33

Mutex operation

B C D

processorA

Mutex0

Blocked queue Ready queue

B C

processorD

Mutex0

Blocked queue Ready queue

MutexWait for A: Step 1 Check Mutex is 0

A

Mutex wait for A step 2 Add A to blocked queue

Check mutex value

Page 34: © Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

© Janice Regan, CMPT 300, May 2007 34

Mutex operation

BA C

processorD

Mutex0

Blocked queue Ready queue

A B C

processorD

Mutex0

Blocked queue Ready queue

D uses resource: MutexSignal D: step1

MutexSignal for D step 2 move process to ready queue

Check for processes in blocked queue

Page 35: © Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

© Janice Regan, CMPT 300, May 2007 35

Mutex operation

A B D

processorC

Mutex0

Blocked queue Ready queue

Time slice for D complete

Page 36: © Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

© Janice Regan, CMPT 300, May 2007 36

Mutex operation

A B D

processorC

Mutex0

Blocked queue Ready queue

A B D

processorC

Mutex0

Blocked queue Ready queue

C uses resource: MutexSignal C: step1

MutexSignal for C step 2 move to ready queue

Check for processes in blocked queue

Page 37: © Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

© Janice Regan, CMPT 300, May 2007 37

Mutex operation

A C B

processorD

Mutex0

Blocked queue Ready queue

Time slice for C complete

A D C

processorB

Mutex0

Blocked queue Ready queue

Time slice for D complete

Page 38: © Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

Check for processes in blocked queue

© Janice Regan, CMPT 300, May 2007 38

Mutex operation

A D C

processorB

Mutex0

Blocked queue Ready queue

DA C

processorB

Mutex0

Blocked queue Ready queue

B uses resource: MutexSignal B: step1

MutexSignal for B step 2 move process to ready queue

Page 39: © Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

© Janice Regan, CMPT 300, May 2007 39

Mutex operation

AB D

processorC

Mutex0

Blocked queue Ready queue

Time slice for B complete

BC A

processorD

Mutex0

Blocked queue Ready queue

Time slice for C complete

Page 40: © Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores

Check for processes in blocked queue

© Janice Regan, CMPT 300, May 2007 40

Mutex operation

CD B

processorA

Mutex0

Blocked queue Ready queue

CD B

processorA

Mutex1

Blocked queue Ready queue

Auses resource: MutexSignalA: step1

MutexSignal for A step 2 set mutex value to 1