Presentation is loading. Please wait.

Presentation is loading. Please wait.

Deadlocks pp. 309. System Model 1.Request 2.Use 3.Release Necessary Conditions 1.Mutual exclusion (non-sharable) 2.Hold and wait (holding at least one.

Similar presentations


Presentation on theme: "Deadlocks pp. 309. System Model 1.Request 2.Use 3.Release Necessary Conditions 1.Mutual exclusion (non-sharable) 2.Hold and wait (holding at least one."— Presentation transcript:

1 Deadlocks pp. 309

2 System Model 1.Request 2.Use 3.Release Necessary Conditions 1.Mutual exclusion (non-sharable) 2.Hold and wait (holding at least one resource, and wait) 3.No preemption (resources cannot be preempted) 4.Circular wait A process may utilize a resource in only the following sequence: necessary condition necessary condition event A event A necessary cond necessary cond event A ( )

3 Resource-Allocation Graph allocated wait

4 Resource-Allocation Graph Necessary but not sufficient!

5 Methods for Handling Deadlocks 1.Deadlock prevention Prevent at least one of the necessary conditions. 2.Deadlock avoidance Information in advance (wait/no wait requests). 3.Deadlock detection and recovery Deadlocks arise, detected, and recovered. 4.Do nothing Performance deterioration, stop functioning, and need to be manually restarted. necessary condition

6 Deadlock Prevention 1.Mutual exclusion Intrinsically non-sharable. 2.Hold and wait Request resources only when having none. 3.No preemption If a request fails, all resources are preempted. 4.Circular wait Request resources in increasing order. Read-only file (sharable) Memory (non-sharable) Whenever a process requests a resource, it does not hold any other resources. 2.1 Request all resources at the beginning (low utilization) 2.2 Release all resources before making a request (cannot always release) Example copy file from DVD to disk, sort, and print. A process that needs several popular resources may have to wait indefinitely (starvation). Often applied to resources whose state can be easily saved and restored later. CPU registers and memory.

7 F(tape drive) = 1 F(disk drive) = 5 F(printer) = 12 Each process can request resources only in an increasing order. 1. Process can request R j that F(R j ) > F(R i ) 2. If requesting R j, process must have released any R i that F(R i ) F(R j ). It must also be noted that if several instances of the same resource type are needed, a single request for all of them must be issued. If these two protocols are used, then the circular-wait condition cannot hold (proof by contradiction).

8 pp. 294 void transaction(Account from, Account to, double amount) { Semaphore lock1, lock2; lock1 = getLock(from); lock2 = getLock(to); wait(lock1); wait(lock2); withdraw(from, amount); deposit(to, amount); signal(lock1); signal(lock2); } Transaction(AccX, AccY, 25); Transaction(AccY, AccX, 50); AccX AccY It is also important to note that imposing a lock ordering does not guarantee deadlock prevention if locks can be acquired dynamically. Keep in mind that developing an ordering, or hierarchy, does not in itself prevent deadlock. It is up to application developers to write programs that follow the ordering. How to correct the program to solve deadlock?

9 Deadlock Avoidance

10 pp. 295

11 Total 12 tape drives. Safe sequence: Suppose that P 2 requests 1 more tape drive and is allocated. System is no longer in safe state. Deadlock may occur, for instance, P 1 is allocated and returns all tape drives (4 tapes are available). P 0 requests 5 and P 2 requests 6 tape drives. Deadlock! 4 tapes P0P0 P2P2 56 3

12 Resource-Allocation-Graph Algorithm pp. 297 Cycle-detection algorithm O(n 2 ) where n is number of processes. Go ahead Avoid it resource multiple instance !!!

13 Bankers Algorithm nnumber of processes mnumber of resource types Availablevector of length m (number of available instances) Maxn x m matrix (maximum demand) Allocationn x m matrix (number of allocated instances) Needn x m matrix (remaining resource need)

14 Safety? Algorithm 1.Work = Available// vector of length m Finish = (false, false, …, false)// vector of length n 2.Find an index i such that both a. Finish[i] = false b. Need i Work 3.Work = Work + Allocation i Finish[i] = true; Goto step 2 4.If Finish[i] == true for all i, then the system is in safe state. Time complexity = O(mn 2 ) cycle detection algorithm Main idea: safe sequence true process, false

15 Resource-Request Algorithm pp. 299 P 1 requests (1, 0, 2) and granted. Safe ( safe seq > 1) Safe, then granted P 4 requests (3, 3, 0)not enough available resources P 0 requests (0, 2, 0)unsafe 1 3 2 4 Main idea: request safety algo safe

16 Deadlock Detection Single instance Resource-allocation graph called wait-for graph. Several instance Deadlock-detection algorithm (similar to Safety algo). Cycle = Deadlock

17 Deadlock-Detection Algorithm 1.Work = Available// vector of length m Finish = (true, false, … true)// vector of length n false if Allocation i 0 2.Find an index i such that both a. Finish[i] = false b. Request i Work 3.Work = Work + Allocation i Finish[i] = true; Goto step 2 4.If Finish[i] == false for some i, 0 i < n, then P i is deadlocked. Time complexity = O(mn 2 ) Main idea: best case process request process deadlock

18 pp. 303 No deadlock Deadlock! max need current request

19 Detection-Algorithm Usage 1.How often is a deadlock likely to occur? 2.How many processes will be effected by deadlock when it happens? Recovery from Deadlock 1.Process Termination Abort all deadlocked processes. Abort one process at a time until the deadlock cycle is eliminated. 2.Resource Preemption Selecting a victim. Rollback (due to resource preempted, total rollback = restart). Starvation (re-preempt from the same process over and over).


Download ppt "Deadlocks pp. 309. System Model 1.Request 2.Use 3.Release Necessary Conditions 1.Mutual exclusion (non-sharable) 2.Hold and wait (holding at least one."

Similar presentations


Ads by Google