Deadlock Operating Systems: Internals and Design Principles.

20
Deadlock Operating Systems: Internals and Design Principles

Transcript of Deadlock Operating Systems: Internals and Design Principles.

Page 1: Deadlock Operating Systems: Internals and Design Principles.

DeadlockOperatin

g Systems:Internals

and Design

Principles

Page 2: Deadlock Operating Systems: Internals and Design Principles.

Deadlock

The permanent blocking of a set of processes that either compete for system resources or communicate with each other

A set of processes is deadlocked when each process in the set is blocked awaiting an event that can only be triggered by another blocked process in the set

Permanent

No efficient solution

Page 3: Deadlock Operating Systems: Internals and Design Principles.

Potential Deadlock

I need quad A and B

I need quad B and C

I need quad C and D

I need quad D and A

Page 4: Deadlock Operating Systems: Internals and Design Principles.

Actual Deadlock

HALT until B is

free

HALT until C is

free

HALT until D is

free

HALT until A is

free

Page 5: Deadlock Operating Systems: Internals and Design Principles.

Deadlock - Unpredictable

In the previous example, the actual deadlock was not guaranteed to happen

For example, if one of the cars decides to stop rather than enter the intersection, then the other three cars could cross through with no problem.

Like race conditions, deadlock is a result of a particular sequence of execution steps that may occur infrequently or never

Page 6: Deadlock Operating Systems: Internals and Design Principles.

Resource Categories

Reusable• can be safely used by

only one process at a time and is not depleted by that use• processors, I/O

channels, main and secondary memory, devices, and data structures such as files, databases, and semaphoresConsumable

• one that can be created (produced) and destroyed (consumed)• interrupts,

signals, messages, and information in I/O buffers

Resources are fundamental to deadlock – competition to control them is the cause of the problem.

Page 7: Deadlock Operating Systems: Internals and Design Principles.

Reusable Resources Example

Page 8: Deadlock Operating Systems: Internals and Design Principles.

Example 2:Memory Request

Space is available for allocation of 200Kbytes, and the following sequence of events occur:

Deadlock occurs if both processes progress to their second request

Competition for memory and CPU could cause deadlock, but modern operating systems manage these resources so it doesn’t happen.

P1. . .

. . .Request 80 Kbytes;

Request 60 Kbytes;

P2 . . .

. . .Request 70 Kbytes;

Request 80 Kbytes;

Page 9: Deadlock Operating Systems: Internals and Design Principles.

Consumable Resources Deadlock

Consider a pair of processes, in which each process attempts to receive a message from the other process and then send a message to the other process:

Deadlock occurs if the Receive is blocking

Page 10: Deadlock Operating Systems: Internals and Design Principles.

Resource Allocation Graphs

Page 11: Deadlock Operating Systems: Internals and Design Principles.

Resource Allocation Graphs

Page 12: Deadlock Operating Systems: Internals and Design Principles.

Conditions for Deadlock

Mutual Exclusion

• only one process may use a resource at a time

Hold-and-Wait

• a process may hold allocated resources while awaiting assignment of others

No Pre-emption

• no resource can be forcibly removed from a process holding it

Circular Wait

• a closed chain of processes exists, such that each process holds at least one resource needed by the next process in the chain

Page 13: Deadlock Operating Systems: Internals and Design Principles.

Dealing with Deadlock

Three general approaches exist for dealing with deadlock:

• adopt a policy that eliminates one of the conditions

Prevent Deadlock

• make the appropriate dynamic choices based on the current state of resource allocation

Avoid Deadlock

• attempt to detect the presence of deadlock and take action to recover

Detect Deadlock

Page 14: Deadlock Operating Systems: Internals and Design Principles.

Deadlock Prevention Strategy Design a system in such a way that the possibility of

deadlock is excluded

Two main methods: Indirect

prevent the occurrence of one of the three necessary conditions

Direct prevent the occurrence of a circular wait

Page 15: Deadlock Operating Systems: Internals and Design Principles.

Deadlock ConditionPrevention

Mutual Exclusion

if access to a resource requires mutual exclusion then it must be

supported by the OS

Hold and Wait

require that a process request all

of its required resources at one time; block the process until all requests can be

granted simultaneously

Page 16: Deadlock Operating Systems: Internals and Design Principles.

No Preemption if a process holding certain resources is denied a

further request, that process must release its original resources and request them again

OS may preempt a lower priority process and require it to release its resources

Works for resources whose state can be saved, such as the CPU, but is not a good general solution

Deadlock Condition Prevention

Page 17: Deadlock Operating Systems: Internals and Design Principles.

Circular Wait define a linear ordering of resource types – assume

they are numbered 1, 2, …, n Require all processes to request their resources in

order; for example: if process 1 already holds a resource from type 4, it may not request any resources from types 1, 2, or 3.

If this condition is enforced there can never be a circular wait, although a normal wait is possible.

Deadlock Condition Prevention

Page 18: Deadlock Operating Systems: Internals and Design Principles.

Deadlock Avoidance

A decision is made dynamically whether the current resource allocation request will, if granted, potentially lead to a deadlock

Requires knowledge of future process requests

Page 19: Deadlock Operating Systems: Internals and Design Principles.

Two Approaches to Deadlock Avoidance

Deadlock

Avoidance

Process Initiation Denial• do not

start a process if its demands might lead to deadlock

Resource Allocation Denial• do not

grant an incremental resource request to a process if this allocation might lead to deadlock

Page 20: Deadlock Operating Systems: Internals and Design Principles.

Questions