02/27/2004csci 315 operating systems design1 process synchronization deadlock notice: the slides for...

21
02/27/2004 CSCI 315 Operating Systems Design 1 Process Synchronization Deadlock Notice: The slides for this lecture have been largely based on those accompanying the textbook Operating Systems Concepts with Java, by Silberschatz, Galvin, and Gagne (2003). Many, if not all, the illustrations contained in this presentation come from this source.

Upload: ashleigh-mitchel

Post on 16-Dec-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 02/27/2004CSCI 315 Operating Systems Design1 Process Synchronization Deadlock Notice: The slides for this lecture have been largely based on those accompanying

02/27/2004 CSCI 315 Operating Systems Design 1

Process SynchronizationDeadlock

Notice: The slides for this lecture have been largely based on those accompanying the textbook Operating Systems Concepts with Java, by Silberschatz, Galvin, and Gagne (2003). Many, if not all, the illustrations contained in this presentation come from this source.

Page 2: 02/27/2004CSCI 315 Operating Systems Design1 Process Synchronization Deadlock Notice: The slides for this lecture have been largely based on those accompanying

02/27/2004 CSCI 315 Operating Systems Design 2

MonitorDefinition: High-level synchronization construct that allows the safe sharing of an abstract

data type among concurrent processes.

A procedure within a monitor can access only local variables defined within the monitor.

There cannot be concurrent access to procedures within the monitor (only one thread can be active in the monitor at any given time).

Condition variables: queues are associated with variables. Primitives for synchronization are wait and signal.

monitor monitor-name{

shared variablesprocedure body P1 (…) {

. . .}procedure body P2 (…) {

. . .} procedure body Pn (…) {

. . .} {

initialization code}

}

Page 3: 02/27/2004CSCI 315 Operating Systems Design1 Process Synchronization Deadlock Notice: The slides for this lecture have been largely based on those accompanying

02/27/2004 CSCI 315 Operating Systems Design 3

Monitor• To allow a process to wait within the monitor, a condition

variable must be declared, ascondition x, y;

• Condition variable can only be used with the operations wait and signal.– The operation

x.wait();means that the process invoking this operation is suspended until another process invokes

x.signal();

– The x.signal operation resumes exactly one suspended process. If no process is suspended, then the signal operation has no effect.

Page 4: 02/27/2004CSCI 315 Operating Systems Design1 Process Synchronization Deadlock Notice: The slides for this lecture have been largely based on those accompanying

02/27/2004 CSCI 315 Operating Systems Design 4

Monitor and Condition Variables

Page 5: 02/27/2004CSCI 315 Operating Systems Design1 Process Synchronization Deadlock Notice: The slides for this lecture have been largely based on those accompanying

02/27/2004 CSCI 315 Operating Systems Design 5

Dining Philosophers with Monitormonitor dp {

enum {thinking, hungry, eating} state[5];condition self[5];void pickup(int i); void putdown(int i);void test(int i);void init() {

for (int i = 0; i < 5; i++)state[i] = thinking;

}}

Page 6: 02/27/2004CSCI 315 Operating Systems Design1 Process Synchronization Deadlock Notice: The slides for this lecture have been largely based on those accompanying

02/27/2004 CSCI 315 Operating Systems Design 6

Dining Philosophersvoid pickup(int i) {

state[i] = hungry;test[i];if (state[i] != eating)

self[i].wait();}

void putdown(int i) {state[i] = thinking;/* test left and right

neighbors */test((i+4) % 5);test((i+1) % 5);

}

void test(int i) {if ( (state[(I + 4) % 5] != eating) && (state[i] == hungry) && (state[(i + 1) % 5] != eating)) {

state[i] = eating;self[i].signal();

}}

Page 7: 02/27/2004CSCI 315 Operating Systems Design1 Process Synchronization Deadlock Notice: The slides for this lecture have been largely based on those accompanying

02/27/2004 CSCI 315 Operating Systems Design 7

Monitor via Semaphores• Variables

semaphore mutex; // (initially = 1)semaphore next; // (initially = 0)int next-count = 0;

• Each external procedure F will be replaced bywait(mutex);…

body of F;…if (next-count > 0) signal(next)else signal(mutex);

For each condition variable x: semaphore x-sem; // (initially = 0) int x-count = 0;

Operation x.wait: x-count++; if (next-count > 0)

signal(next); else

signal(mutex); wait(x-sem); x-count--;

Operation x.signal:if (x-count > 0) { next-count++; signal(x-sem); wait(next); next-count--;}

Page 8: 02/27/2004CSCI 315 Operating Systems Design1 Process Synchronization Deadlock Notice: The slides for this lecture have been largely based on those accompanying

02/27/2004 CSCI 315 Operating Systems Design 8

Concepts to discuss

Deadlock

Livelock

Spinlock vs. Blocking

Page 9: 02/27/2004CSCI 315 Operating Systems Design1 Process Synchronization Deadlock Notice: The slides for this lecture have been largely based on those accompanying

02/27/2004 CSCI 315 Operating Systems Design 9

Deadlock: Bridge Crossing Example

• Traffic only in one direction.• Each section of a bridge can be viewed as a resource.• If a deadlock occurs, it can be resolved if one car backs

up (preempt resources and rollback).• Several cars may have to be backed up if a deadlock

occurs.• Starvation is possible.

Page 10: 02/27/2004CSCI 315 Operating Systems Design1 Process Synchronization Deadlock Notice: The slides for this lecture have been largely based on those accompanying

02/27/2004 CSCI 315 Operating Systems Design 10

Deadlock: Dining-Philosophers ExampleImagine all philosophers start out hungry and that they all pick up their left chopstick at the same time.

Assume that when a philosopher manages to get a chopstick, it is not released until a second chopstick is acquired and the philosopher has eaten his share.

Question: Why did deadlock happen? Try to enumerate all the conditions that have to be satisfied for deadlock to occur.

Question: How could be done to guarantee deadlock won’t happen?

Page 11: 02/27/2004CSCI 315 Operating Systems Design1 Process Synchronization Deadlock Notice: The slides for this lecture have been largely based on those accompanying

02/27/2004 CSCI 315 Operating Systems Design 11

A System Model

• Resource types R1, R2, . . ., Rm

CPU cycles, memory space, I/O devices

• Each resource type Ri has Wi instances.

• Each process utilizes a resource as follows:– request – use – release

Page 12: 02/27/2004CSCI 315 Operating Systems Design1 Process Synchronization Deadlock Notice: The slides for this lecture have been largely based on those accompanying

02/27/2004 CSCI 315 Operating Systems Design 12

Deadlock Characterization

• Mutual exclusion: only one process at a time can use a resource.

• Hold and wait: a process holding at least one resource is waiting to acquire additional resources held by other processes.

• No preemption: a resource can be released only voluntarily by the process holding it, after that process has completed its task.

• Circular wait: there exists a set {P0, P1, …, P0} of waiting processes such that P0 is waiting for a resource that is held by P1, P1 is waiting for a resource that is held by

P2, …, Pn–1 is waiting for a resource that is held by Pn, and P0 is waiting for a resource that is held by P0.

Deadlock can arise if four conditions hold simultaneously:

Page 13: 02/27/2004CSCI 315 Operating Systems Design1 Process Synchronization Deadlock Notice: The slides for this lecture have been largely based on those accompanying

02/27/2004 CSCI 315 Operating Systems Design 13

Resource Allocation Graph

• The nodes in V can be of two types (partitions):– P = {P1, P2, …, Pn}, the set consisting of all the processes

in the system.

– R = {R1, R2, …, Rm}, the set consisting of all resource types in the system.

• request edge – directed edge P1 Rj

• assignment edge – directed edge Rj Pi

Graph: G=(V,E)

Page 14: 02/27/2004CSCI 315 Operating Systems Design1 Process Synchronization Deadlock Notice: The slides for this lecture have been largely based on those accompanying

02/27/2004 CSCI 315 Operating Systems Design 14

Resource Allocation Graph• Process

• Resource Type with 4 instances

• Pi requests instance of Rj

• Pi is holding an instance of Rj

Pi

Pi

Rj

Rj

Page 15: 02/27/2004CSCI 315 Operating Systems Design1 Process Synchronization Deadlock Notice: The slides for this lecture have been largely based on those accompanying

02/27/2004 CSCI 315 Operating Systems Design 15

Example of a Resource Allocation Graph

Page 16: 02/27/2004CSCI 315 Operating Systems Design1 Process Synchronization Deadlock Notice: The slides for this lecture have been largely based on those accompanying

02/27/2004 CSCI 315 Operating Systems Design 16

Resource Allocation Graph With A Deadlock

Page 17: 02/27/2004CSCI 315 Operating Systems Design1 Process Synchronization Deadlock Notice: The slides for this lecture have been largely based on those accompanying

02/27/2004 CSCI 315 Operating Systems Design 17

Resource Allocation Graph With A Cycle But No Deadlock

Page 18: 02/27/2004CSCI 315 Operating Systems Design1 Process Synchronization Deadlock Notice: The slides for this lecture have been largely based on those accompanying

02/27/2004 CSCI 315 Operating Systems Design 18

Basic Facts

• If graph contains no cycles no deadlock.

• If graph contains a cycle – if only one instance per resource type, then

deadlock.– if several instances per resource type,

possibility of deadlock.

Page 19: 02/27/2004CSCI 315 Operating Systems Design1 Process Synchronization Deadlock Notice: The slides for this lecture have been largely based on those accompanying

02/27/2004 CSCI 315 Operating Systems Design 19

Methods for Handling Deadlocks

• Ensure that the system will never enter a deadlock state.

• Allow the system to enter a deadlock state and then recover.

• Ignore the problem and pretend that deadlocks never occur in the system; used by most operating systems, including UNIX.

Page 20: 02/27/2004CSCI 315 Operating Systems Design1 Process Synchronization Deadlock Notice: The slides for this lecture have been largely based on those accompanying

02/27/2004 CSCI 315 Operating Systems Design 20

Deadlock Prevention

• Mutual Exclusion – not required for sharable resources; must hold for nonsharable resources.

• Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any other resources.– Require process to request and be allocated all its

resources before it begins execution, or allow process to request resources only when the process has none.

– Low resource utilization; starvation possible.

Restrain the ways request can be made.

Page 21: 02/27/2004CSCI 315 Operating Systems Design1 Process Synchronization Deadlock Notice: The slides for this lecture have been largely based on those accompanying

02/27/2004 CSCI 315 Operating Systems Design 21

Deadlock Prevention

• No Preemption –– If a process that is holding some resources requests another

resource that cannot be immediately allocated to it, then all resources currently being held are released.

– Preempted resources are added to the list of resources for which the process is waiting.

– Process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting.

• Circular Wait – impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration.

Restrain the ways request can be made.