operating systems {week 10}

18
Operating Systems {week 10} Rensselaer Polytechnic Institute CSCI-4210 – Operating Systems David Goldschmidt, Ph.D.

Upload: kerri

Post on 21-Feb-2016

50 views

Category:

Documents


0 download

DESCRIPTION

Rensselaer Polytechnic Institute CSCI-4210 – Operating Systems David Goldschmidt, Ph.D. Operating Systems {week 10}. A need for synchronization (i). Without synchronization amongst processes (and threads), results are unpredictable. how do variables x and y become corrupted?. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Operating Systems {week 10}

Operating Systems{week 10}

Rensselaer Polytechnic InstituteCSCI-4210 – Operating SystemsDavid Goldschmidt, Ph.D.

Page 2: Operating Systems {week 10}

A need for synchronization (i) Without synchronization amongst

processes (and threads), results are unpredictable

how do

variables x and y

become corrupted?

Page 3: Operating Systems {week 10}

A need for synchronization (ii) Processes compete for resources

Once obtained, the resource isfully dedicated to a process

Often, mutual exclusion is required▪ No other process is allowed access to the

resource

Processes cooperate with other processes Shared resources Specific ordering or sequencing of

events

all of this applies

to threads, too!

Page 4: Operating Systems {week 10}

Semaphores (i)

A semaphore is a synchronization mechanism Semaphore S is a special integer variable

OS provides two atomic operations on S: wait(S) or P(S):▪ wait for a resource to become available

signal(S) or V(S):▪ signal that we’re done using a resource

Page 5: Operating Systems {week 10}

Semaphores (ii)

The wait(S) operation decrementssemaphore S only when S is available

wait(S) { while ( S <= 0 ) { /** no-op **/ ; } S--; }

this will blockindefinitelyin a busy wait

Page 6: Operating Systems {week 10}

Semaphores (iii)

The signal(S) operation incrementssemaphore S to release a resource

signal(S) { S++; } to protectcritical section

wait(S); // CRITICAL // SECTION signal(S);

Page 7: Operating Systems {week 10}

Binary semaphores

A binary semaphore providesmutually exclusive access toa shared resource Initialize semaphore S to 1 Use wait(S) and signal(S) Possible values of S are 0 and 1

Page 8: Operating Systems {week 10}

Counting semaphores

A counting semaphore controls accessto a finite number of resources: e.g. open files, network connections,

shared buffers, etc.// n instances of a finite resourcesemaphore S = n Write pseudocode for the

producer-consumer problem usingsemaphores to synchronize access

to the shared buffer of size N

Page 9: Operating Systems {week 10}

Starvation

A process faces starvation when it is forced to wait indefinitely for shared resource X as other processes use that shared resource X Also known as indefinite blocking

Page 10: Operating Systems {week 10}

Deadlock

A system enters a deadlock state when multiple processes are unable to obtain a lock on all necessary resources After acquiring

a resource, aprocess holds thatresource indefinitely

// P0 ...wait(S)wait(Q)...signal(Q)signal(S)...

// P1 ...wait(Q)wait(S)...signal(S)signal(Q)...

semaphore S, Q

Deadlock!

Page 11: Operating Systems {week 10}

Conditions for deadlock

Deadlock requires four conditions: Mutual exclusion Hold and wait No preemption Circular wait▪ i.e. a cycle!

Page 12: Operating Systems {week 10}

Resource allocation graph A resource allocation graph is a

directed graph showing processes and resources

Page 13: Operating Systems {week 10}

Deadlock!

Page 14: Operating Systems {week 10}

Deadlock?

Page 15: Operating Systems {week 10}

Rice

Dining philosophers problem (i) Five philosophers at a table

Each philosopher thinks or eats To eat, a philosopher must pick

up the closest two chopsticks A philosopher may only pick

up one chopstick at a time

Represents allocating shared resources to competing and cooperating processes

Page 16: Operating Systems {week 10}

Rice

Dining philosophers problem (ii) Potential solution:

Can deadlock occur?

// philosopheri

while ( true ){ think(); wait( fork[i] ); wait( fork[(i+1)%5] ); eat(); signal( fork[(i+1)%5] ); signal( fork[i] );}

semaphore fork[] = { 1, 1, 1, 1, 1 };

Page 17: Operating Systems {week 10}

Handling deadlocks (i)

Allow the system to enter a deadlock state, then recover by: Terminating one or all deadlocked

processes Rollback deadlocked processes to a safe

checkpointed state

Or....

Page 18: Operating Systems {week 10}

Handling deadlocks (ii)

Guarantee that the system will neverenter a deadlocked state Deadlock prevention ensures that at

least one of the four necessary conditions is never met

Deadlock avoidance allows a system to change state by allocating resource(s) only when it is certain deadlock will not occur as a result