ch4 Deadlock
Deadlock:
A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set.
–The system model for deadlocks first requires a process request a resource, then use the resource, and finally release the resource.
Basic Facts:
1.If graph contains a cycle -> no deadlock
2.If graph contains a cycle ->
if only one instance per resource type, then deadlock
if several instances per resource type, possibility of deadlock.
产生死锁的四个必要条件:
(1) 互斥条件:一个资源每次只能被一个进程使用。
(2) 请求和保持条件:一个进程因请求资源而阻塞时,对已获得的资源保持不放。
(3) 不可抢占条件:进程已获得的资源,在末使用完之前,不能强行剥夺,只能在进程使用完时由自己释放。
(4) 循环等待条件:若干进程之间形成一种头尾相接的循环等待资源关系。
这四个条件是死锁的必要条件,只要系统发生死锁,这些条件必然成立,而只要上述条件之一不满足,就不会发生死锁。
Deadlock Prevention:
Restrain the ways request can be made
1.Mutual Exclusion - not required for sharable
resources ; must hold for non-sharable resources
2. Hold ans Wait - must guarantee that whenever a process requests a resource , it does not hold any other resources
Resource Allocation Graph with a Deadlock:
Graph with a Cycle but No Deadlock
Avoidance Algorithms
1.Single instance of a resource type:
Use a resource-allocation graph
2.Multiple instances of a resource type:
Use the banker’s algorithm
Banker’s Algorithm:
Data Structures:
Example of Banker’s Algorithm:
1.
Need = Max - Allocation
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1
safe state sequence is:
P1 -> P3 -> P4 -> P2 -> P0
2.
系统安全
3.
(1)
(2)Safe.
(3)Yes.
(1)Safe State.
(2)No.
Question:
1.
Safe State.
Banker’s algorithm.
2.
Resource Preemption:
Selecting a victim - minimize cost
Rollback - return to some safe state, restart process for that state
Starvation - same process may always be picked as victim, including number of rollback in cost factor.