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?

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).