CPT104操作系统笔记(L6 Resource Management Deadlocks)

System Model

System consists of resources
Resource types R 1 , R 2 , . . ., R m     e.g. CPU cycles, memory space, I/O devices
Each resource type R i has W i instances.
Each process utilizes a resource as follows: request / use / release

Deadlock Characterization

Deadlock can be defined as the permanent blocking of a set of processes that compete for system resources.
如果同时保持四种条件,就会出现deadlock:
M UTUAL EXCLUSION : only one process at a time can use a resource
H OLD AND WAIT : a process holding at least one resource is waiting to acquire additional resources held by other processes
N O PREEMPTION : a resource can be released only voluntarily by the process holding it after that process has completed its task.
C IRCULAR WAIT : a closed chain of processes exists, such that each process holds at least one resource needed by the next process in the chain

Resource-Allocation Graph

A set of vertices V                                       a set of edges E

V 分为两种类型

P = { P 1 , P 2 , …, P n } , the set consisting of all the processes in the system
R = { R 1 , R 2 , …, R m } , the set consisting of all resource types in the system
request edge – directed edge P i R j
assignment(赋值) edge – directed edge R j P i

 

Resource Allocation Graph With a Deadlock               Graph With a Cycle But No Deadlock

 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

HANDLING DEADLOCKS

three approaches can be used:
Deadlock prevention
Deadlock avoidance
Deadlock detection and recovery

most operating systems ignore the problem and pretend that deadlocks never occur in the system, including UNIX)

Deadlock Prevention

eliminates one of the 4 conditions
Mutual Exclusion – cannot be disallowed
Hold and Wait 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 allocated to it
         Low resource utilization; starvation possible
No Preemption
- if a process holding certain resources is denied a further request, that process must release its original resources and, if necessary, request them again together with the additional resource
- if a process requests a resource that is currently held by another process, the OS may preempt the second process and require it to release its resources.
Circular Wait – can be prevented by defining a linear ordering ofresource types.
        - if a process has been allocated resources of type R, then it may subsequently request only           those resources of types following R in the ordering.

Deadlock Avoidance

constrain resource requests to prevent at least one of the four conditions of deadlock.
Do not start a process if its demands might lead to deadlock.
Do not grant an incremental resource request to a process if this allocation might lead to deadlock.
A safe state is one in which there is at least one sequence of resource allocations to processes that does not result in a deadlock (i.e., all of the processes can be run to completion)
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
The avoidance approach requires the knowledge of:
Max needs = total amount of each resource in the system
Available resources = total amount of each resource not allocated to any process
Need / Resources needed= future requests of the process i for resource j
Allocation / Current allocated resources = the resources allocated presently to process i.
A resource request is feasible, only if the total number of allocated resources of a resource type does not exceed the total number of that resource type in the system.

Safe state. Example

One resource type, multiple instances: 12 magnetic tape drives
At time T0, the system is in a safe state. The sequence < P1, P0, P2 > satisfifies the safety condition.
safe state -> unsafe state.
• If P2 is given 1 more tape drive, then no longer safe -> Av. = 3 -1 = 2
• What if P0 now needs remaining 5 tape drives to complete; since there are only 2 available (3 – 1), P0 waits
• What if P2 also now needs remaining 6 tape drives to complete; since there are only 2 available, P2 waits
• Even if P1 completes and releases resources, P0 & P2 deadlocked.

Deadlock Avoidance in Single Instance of Resources

Claim edge P i R j indicated that process Pi may request resource Rj;(虚线表示)
After the cycle check, if it is confirmed that there will be no circular wait, the claim edge is converted to a request edge. Otherwise, it will be rejected.

Request edge converted to an assignment edge when the resource is allocated to the process

 

Deadlock Avoidance in Multiple Instances of Resources - Banker’s Algorithm

Banker’s algorithm: Safety Test algorithm
The banker’s algorithm has two parts:
Safety Test algorithm that checks the current state of the system for its safe state.
Resource request algorithm that verifies whether the requested resources, when allocated to the process, affect the safe state. If it does, the request is denied.
Data Structures for the Banker s Algorithm
n = number of processes      m = number of resources types
Available : Vector of length m . If Available [ j ] = k , there are k instances of resource type R j available
Max : n*m matrix. If Max [ i,j ] = k , then process P i may request at most k instances of resource type R j
Allocation : n * m matrix. If Allocation[ i,j ] = k then P i is currently allocated k instances of R j
Need : n x m matrix. If Need [ i,j ] = k , then P i may need k more instances of R j to complete its task
Need [ i,j] = Max [ i,j ] – Allocation [ i,j ]
e.g.
5 processes P 0 through P 4 ;
3 resource types :
        A (10 instances ), B (5 instances ), and C (7 instances )
Snapshot at time T 0 :
The system is in a safe state since the sequence < P 1 , P 3 , P 4 , P 2 , P 0 > satisfies safety criteria

 

IF P1 Request (1,0,2)

Check that Request Available (that is, (1,0,2) <= (3,3,2) true

Executing safety algorithm shows that sequence < P 1 , P 3 , P 4 , P 0 , P 2 > satisfies safety requirement

Deadlock Detection

Detection of single instance of resource

 

    Resource-Allocation Graph                        Corresponding wait-for graph

 

Periodically invoke an algorithm that searches for a cycle in the graph.

If there is a cycle, there exists a deadlock

An algorithm to detect a cycle in a graph requires an order of n 2 operations, where n is the number of vertices in the graph

 

Detection for multiple instances of resources

Available : A vector of length m indicates the number of available resources of each type
Allocation : An n x m matrix defines the number of resources of each type currently allocated to each process
Request : An n x m matrix indicates the current request of each process. If Request [ i ][ j ] = k , then process P i is requesting k more instances of resource type R j .

Detection Algorithm

e.g.
5 processes P 0 through P 4 ;
3 resource types
A ( 7 instances), B ( 2 instances), and C ( 6 instances)
Snapshot at time T 0 :

 Sequence <P0 , P2 , P3 , P1 , P4> will result in Finish[i] = true for all i

IF P 2 requests 1 instance of type C
then have a deadlock
Detection-Algorithm Usage
- How often a deadlock is likely to occur?
- How many processes will be affected by deadlock when it happens?
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.

Recovery from Deadlock

2 options for breaking a deadlock

Process Termination / Abort Process
Resource Preemption

Process Termination

Abort all deadlocked processes
Abort one process at a time until the deadlock cycle is eliminated
Many factors may affect which process is chosen
1. Priority of the process
2. How long process has computed, and how much longer to
completion
3. Resources the process has used
4. Resource's process needs to complete
5. How many processes will need to be terminated
6. Is process interactive or batch?

Resource Preemption

Select a victim a process, whose execution has just started and requires many resources to complete, will be the right victim for preemption (minimize cost).
Rollback return the process to some safe state (safe checkpoint), restart it from that state
Starvation it may be possible that the same process is always chosen for resource preemption, resulting in a starvation situation. Thus, it is important to ensure that the process will not starve. This can be done by fixing the number of times a process can be chosen as a victim.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值