CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems...

45
CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido

Transcript of CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems...

Page 1: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Deadlock – Part 2

(Lecture 7a)

CS5002 Operating SystemsDr. Jose M. Garrido

Page 2: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Resource Allocation and De-allocation

When a process needs resources, it will normally follow the sequence:

1. Request a number of instances of one or more resource types. If the resource units requested are not available, then process must wait

2. Acquire the resource(s), the OS allocates resources

3. Use the resource(s) for a finite period.4. Release the resource(s).

Page 3: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Synchronizing Allocation of Resources

Semaphores can be used to synchronize the allocation and de-allocation of resources.

Page 4: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Multiple Resource Allocation Problem

• Several processes may compete for multiple resources

• If the allocation of multiple resources to processes is not done with proper synchronization, deadlock might occur

• This waiting state is much more involved than starvation

Page 5: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Deadlock Problem

• A set of blocked (suspended) processes

• Each process holding one or more resources

• Processes waiting to acquire one or more resources held by other processes

Page 6: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Example of Deadlock

• A system has two tape drives, which processes P1 and P2 need

• Processes P1 and P2 each acquire one tape drive and each wait for the other tape drive

• Processes P1 and P2 are blocked waiting indefinitely to acquire the other tape unit

Page 7: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Deadlock – General Definition

• A set of processes is in a deadlock state when every processes in the set is waiting for an event that can only be caused by another process in the set.

• The events of interest are:– allocations of resources – release of resources

• The resources are: CPU cycles, memory spaces, files, I/O devices in the computer system.

Page 8: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Disadvantage Of Deadlock

• Deadlocked processes can never complete execution because they are waiting indefinitely

• System resources are tied up because they are held by deadlocked processes.

• Deadlocks are undesirable because they normally slow down a system

Page 9: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Another Example Of Deadlock

• Consider a system with one printer and one disk unit. Processes P and Q both need the two resources

• Suppose that process P is holding the printer, and process Q is holding the disk unit. Now P waits for the disk unit and Q waits for the printer.

• Process P is waiting for process Q to release the disk unit, and process Q is waiting for process P to release the printer.

Page 10: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Result

Both processes are deadlocked, they cannot proceed.

Page 11: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Conditions for Deadlock

Conditions For Deadlock: A deadlock arises if four conditions hold simultaneously in a system:

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

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

Page 12: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Conditions for Deadlock (Cont.)

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

4. Circular wait: there exists a set {P0, P1, …Pn} of waiting processes such that P0 is waiting for a resource that is held by P1, but P1 is waiting for a resource held by P2, …, Pn-1 is waiting for a resource held by Pn, and Pn is waiting for a resource held by P0.

Page 13: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Deadlock Conditions

These conditions are necessary but not sufficient for deadlock to occur.

Page 14: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Resource Allocation Graph

A System Deadlock Model.

A process-resource graph shows the relationships between processes and resources. It consists of the following components:

• A set of processes• A set of resources• A set of directed edges

Page 15: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Resource Allocation Graph

• a set of processes P = {P0, P1, …., Pi,…., Pn}• a set of resource types, R = {R0, R1,

…,Rj, ..,Rm}• a set of directed edges

– A request edge (Pi, Rj) from process Pi to resource Rj. Thus, Pi is waiting for an instance of resource type Rj.

– An assignment edge (Rj, Pi) from resource Rj to process Pi. an instance of Rj has been allocated to process Pi.

Page 16: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

A Graph with no Cycles

P3

R3

R2

P2P1

R1

R2

Page 17: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Graph with a Cycle

P3

R3

R2

P2P1

R1

R2

Page 18: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Analysis Of This Graph

• This graph has two minimal cycles• Processes p1, p2, and p3 are deadlocked.

Page 19: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Another Graph

P3

R2 R4

R1

P1P2

R3

Page 20: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Analysis Of Resource Allocation Graph

• If the graph contains no cycles, no deadlock exists.

• If the graph contains cycles

– if each resource type has only 1 instance, then a deadlock exits.

– otherwise, a deadlock may exist.

Page 21: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Methods For Dealing With Deadlocks

• Deadlock prevention: disallow the existence of one of the four necessary conditions for deadlocks to occur.

• Deadlock avoidance: for each resources request which can be satisfied, determine whether the request should be delayed in order to avoid a possible deadlock in the future.

• Deadlock detection and recovery: detect the existence of deadlock; if it has occurred, take actions to remove it.

Page 22: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Deadlock Prevention

• Non-existence of the Hold and wait condition

• Non-existence of the Circular Wait condition

Page 23: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Deadlock Prevention (2)

Non-existence of hold and wait condition • Allocation only if all resources

requested are available• Allocation of resources only when

there is no previous allocation.

– Low resource utilization

– Starvation possible

Page 24: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Deadlock Prevention (3)

Non-existence of the Circular Wait condition

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

Page 25: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Disallow Circular Wait

• Impose a total ordering of all resource types, R = {R1, R2, …, Rm}.

• Define a one to one function, F: R N• Require that each process requests

resources in an increasing order of enumeration.

• Process P can request resources of type Ri, then resources of type Rj, only if:

F( Rj) > F( Ri)

Page 26: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Example of Linear Ordering of Resources

• A system has the following resources types:

R = {Disk, Tape, Printer}• The following ordering is defined for the

resources with function F:

F(Disk) = 1; F(Tape) = 3; F(Printer) = 7• A process, P, has to request first an

instance of Disk, then an instance of Tape, then an instance of Printer.

Page 27: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Problems For Deadlock Prevention

• Low device utilization• Reduced system throughput

Why?

Page 28: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Deadlock Avoidance

Requires that the system has some additional a priori information available

• Simplest and most useful model requires that each process declare the maximum number of resources of each type that it may need.

• The deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure that there can never be a circular-wait condition.

• The system is always kept in a safe state

.

Page 29: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Resource Allocation State

Resource-allocation state is defined by the • Number of available resources• Number of allocated resources• Maximum resource demands of the

processes.

Page 30: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Safe State

• A state is safe if the system can allocate resources to each process (up to its maximum) in some order and still avoid a deadlock

• A system is in a safe state only if there exits a safe sequence of allocations

Page 31: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Banker’s Algorithm

• Allows multiple instances of a resource type

• Each process must a priori claim maximum use.

• The OS must determine whether the allocation of resources will leave the system in a safe state

• If it will, the resources are allocated, otherwise, the process has to wait.

• When a process gets all its resources it must return them in a finite amount of time.

Page 32: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Data Structures for the Algorithm

• Available: Vector of length m. If Available [j] = k, there are k instances of resource type Rj

available.• Max: n x m matrix. If Max [i,j] = k, then process

Pi may request at most k instances of resource type Rj.

• Allocation: n x m matrix. If Allocation[i,j] = k then Pi is currently allocated k instances of Rj.

• Need: n x m matrix. If Need[i,j] = k, then Pi may need k more instances of Rj to complete its task.

Need [i,j] = Max[i,j] – Allocation [i,j].

n = number of processes, m = number of resources types.

Page 33: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Example 1 Bankers Algorithm

Processes: P0, P1, P2; resources: 12 units of magnetic tape (R1) State 1: PROCESS MAX_NEED ALLOCATION P0 10 5

P1 4 2P2 9 2

Resources available in this state: 3 units.

Page 34: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Safe State?

State 1 is a safe state because a safe sequence: <P1, P0, P2> exists.

Page 35: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Example (Cont.)

Assume that when in state 1, process P2 requests And is allocated 1 more unit of tape,

Process Max_need AllocationP0 10 5P1 4 2P2 9 3

Page 36: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Safe State?

This state, state 2 is unsafe, no safe sequence can

be found.

Page 37: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Example 2 Banker’s Algorithm

There are 5 processes P0 through P4; 3 resource types A (10 instances), B (5 instances), and C (7 instances). System state at time T0:

AllocationMax Need Available A B C A B C A B C A B C

P0 0 1 0 7 5 3 7 4 3 3 3 2

P12 0 0 3 2 2 1 2 2

P2 3 0 2 9 0 2 6 0 0

P3 2 1 1 2 2 2 0 1 1

P4 0 0 2 4 3 3 4 3 1

Page 38: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Safe State?

• The system is in a safe state since the sequence:

< P1, P3, P4, P2, P0> found satisfies safety criteria

• There is a safe sequence of allocating resources to the processes and avoiding deadlock

Page 39: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Example 2

• Suppose that in the initial state P1 requests (1, 0, 2) more • Check that Request Available

Allocation Need AvailableA B C A B C A B C

P0 0 1 0 7 4 3 2 3 0

P1 3 0 2 0 2 0

P2 3 0 1 6 0 0

P3 2 1 1 0 1 1

P4 0 0 2 4 3 1

• Is the state safe?

Page 40: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Deadlock Detection

1.Allow system to enter deadlock state

2.Run detection algorithm3.Carry out recovery scheme

Page 41: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Detection-Algorithm Usage

• When, and how often, to invoke the detection check depends on:– How often a deadlock is likely to occur?– How many processes will need to be rolled

back? (one for each disjoint cycle)• If detection algorithm is invoked arbitrarily,

there may be many cycles in the resource graph and so we would not be able to tell which of the many deadlocked processes “caused” the deadlock.

Page 42: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Recovery: Process Termination

• Abort all deadlocked processes.• Abort one process at a time until the

deadlock cycle is eliminated.• In which order should we choose to abort?

– Priority of the process.– How long process has computed, and how

much longer to completion.– Resources the process has used.– Resources process needs to complete.– How many processes will need to be

terminated. – Is process interactive or batch?

Page 43: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Recovery: Resource Preemption

• Selecting a victim – minimize cost.• Rollback – return to some safe state,

restart process from that state.• Starvation – same process may

always be picked as victim, include number of rollback in cost factor.

Page 44: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Selection Criteria

• Least amount of CPU service received

• Least amount of output• Most estimated remaining time• Least total resources allocated so

far• Lowest priority

Page 45: CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.

CS6502 Operating Systems - Dr. J. Garrido

Combined Approach to Deadlock

• Combine the three basic approaches– prevention– avoidance– detection

allowing the use of the optimal approach for each of resources in the system.

• Partition resources into hierarchically ordered classes.

• Use most appropriate technique for handling deadlocks within each class.