Вы находитесь на странице: 1из 34

OPERATING SYSTEMS (IS ZC362 LECTURE 9)

DEADLOCK

Process states

The Deadlock Problem

A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set Permanent blocking of a set of processes that either compete for system resources or communicate with each other No efficient solution Involve conflicting needs for resources by two or more processes
3

Examples
Example 1

System has 2 disk drives P1 and P2 each hold one disk drive and each needs another one

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 Note Most OSes do not prevent or deal with deadlocks
5

System Model

Processes P1, P2, , Pn Resource types R1, R2, . . ., Rm


CPU cycles, memory space, I/O devices

Each resource type Ri has Wi instances.

Resource Allocation

Request If the request cannot be granted immediately then the requesting process must wait until it can acquire the resource. Use Process operates on resource. Release Process release the resource. Resources Physical resource -- Printer,Memory space.. Logical resource --- Semaphore,Files..

Reusable Resources

Used by only one process at a time and not depleted by that use Processes obtain resources that they later release for reuse by other processes Processors, I/O channels, main and secondary memory, devices, and data structures such as files, databases, and semaphores Deadlock occurs if each process holds one resource and requests the other

Example of Deadlock

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

P2 Request 70 Kbytes; ...


Request 80 Kbytes;

Request 80 Kbytes; ...


Request 60 Kbytes;

Deadlock occurs if both processes progress to their second request


10

Consumable Resources

Created (produced) and destroyed (consumed)

Interrupts, signals, messages, and information in I/O buffers There is no limit on the number of consumable resources of a particular type.

11

Example of Deadlock

Deadlock occurs if receive is blocking


P1 P2 ... Receive(P1); ...

... Receive(P2);

... Send(P2, M1);

Send(P1, M2);

12

Deadlock Characterization

Mutual exclusion:

only one process at a time can use a resource.

At least one resource must be in a non sharable mode; that is only one process at a time can use a resource. If any other process request the resource, the requesting process must be delayed until the resource has been released.
a process holding at least one resource is waiting to acquire additional resources held by other processes

Hold and wait:

13

Deadlock Characterization
Deadlock can arise if all four conditions hold simultaneously.

No preemption:

a resource can be released only voluntarily by the process holding it, after that process has completed its task .(The resource cannot be preempted). 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, , Pn1 is waiting for a resource that is held by Pn, and Pn is waiting for a resource that is held by P0.
14

Circular wait:

Traffic Deadlock

15

Resource-Allocation Graph
A set of vertices V and a set of edges E.

V is partitioned into two types:

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


16

Resource-Allocation Graph (Cont.)

Process

Resource Type with 4 instances

Pi requests instance of Rj

Pi
Rj

Pi is holding an instance of Rj

Pi
Rj
17

Example of a Resource Allocation Graph

18

Resource Allocation Graph With A Deadlock

19

Graph With A Cycle But No Deadlock

20

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

Most of the Operating Systems are not handling Deadlock problems

It is too costly to prevent / avoid / detect recover deadlock


21

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

22

Deadlock Prevention
Restrain the ways request can be made

Ensure that at least one of the 4 necessary condition be prevented.


Mutual Exclusion not required for sharable resources; must hold for non-sharable resources

Only one process may use a resource at a time

23

Deadlock Prevention (Cont.)

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

Implementation: System calls requesting resources precede all other function calls
Low resource utilization; Starvation possible

24

Deadlock Prevention (Cont.)

Alternative method:

Allow a process to request resources only when the process has no resources. Release all resources before requesting for a new one
A process may be held up for a long time waiting for all of its resource request be filled, when it could have proceeded with only some of the resources. A process may not know in advance all of the resources that it will require.

This method is inefficient

25

No Preemption

Deadlock Prevention (Cont.)

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 Alternate Approach: If a process request some resources,

Allot to the process if the resource is free Else, check whether they are allocated to some other process that is waiting for additional resource. If so, preempt the resource from the waiting process and allot them to the requesting process.
26

Deadlock Prevention (Cont.)

Circular Wait

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

27

Example Assign a unique integer number to individual resource which allows us to compare two resources and determine one proceed other. Define a one to one function F: RN F(tape drive)=1 F(disk drive)=5 F(printer) =12 Process can request resources only in an increasing order of enumeration. So R(j) is acceptable to a process if and only if F(R(i))< F(R(j)). If F(R(i))> F(R(j)) then release (R(i)) before R(j) entered in. Example: Process needs tape drive and printer must request the tape driver first then printer. Disadvantage of Deadlock Prevention Low device Utilization Reduced system throughput.
28

Deadlock Avoidance

Requires that the system has some additional a priori Information available .(Each request requires the information of the resources currently available, the resources currently allocated to each process,future request and release of each process etc) 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 Resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the processes
29

Safe State

A state is a safe state if the system can allocate resources to each process in some order and still avoid a deadlock.

If the system is in safe state only if there exists a safe sequence.

When a process requests an available resource, system must decide if immediate allocation leaves the system in a safe state

30

3 processes and 12 CD drives Max. Needs

Example
Currently holding

P0 P1 P2

10 4 9

5 2 2

At time t0 the system is in safe state <P1,P0,P2> System will go to unsafe state if process P2 request and allocated one more resource. P2 9 3 Avoidance algorithm ensures that the system will never deadlock. Initially the system is in safe state if a process is requesting a resource, the resource is granted only if the allocation leaves the system in safe state. In this scheme if a process request a resource that is currently available, it may still have to wait. So resource utilization can be lower.
31

System is in safe state if there exists a sequence <P1, P2, , Pn> of ALL the processes is the systems such that for each Pi, the resources that Pi can still request can be satisfied by currently available resources + resources held by all the Pj, with j < i That is: If Pi resource needs are not immediately available, then Pi can wait until all Pj have finished When Pj is finished, Pi can obtain needed resources, execute, return allocated resources, and terminate When Pi terminates, Pi +1 can obtain its needed resources, and so on If no such sequence exists, then the system state is said to be unsafe.
32

Basic Facts

If a system is in safe state no deadlocks If a system is in unsafe state possibility of deadlock Avoidance ensure that a system will never enter an unsafe state.

33

Safe, Unsafe , Deadlock State

34

Вам также может понравиться