I. Discussion Section Quiz 2 ============================ 1. What are the four conditions of deadlock? • Mutual Exclusion • Hold and Wait • No preemption on resource • There is a cycle in the resource request graph. 2. How to avoid deadlock? By avoiding any one of the above conditions. • Make all resources shareable - Can we do this? • Don't permit mutual exclusion - Can we do this? • Virtualize non-shared resources • Use only uniprogramming - Run only one process at a time? • Don't allow waiting and holding - Phone company example - Process must request everything at once. 1. How would we know the future? 2. May request too much 3. Starvation 4. May not be possible. . . • Process must release all current resources before requesting any new ones. - Can we do this ALL the time? 1. Ex: Process that is looking to read something to disk, but can't get disk resource. . . • Ordered or hierarchical requests - Ask for all R1s, R2s, etc. . . Processes must know what they need in advance. - One way to do this is using a resource request graph algorithm. 3. A given system has three resources available, R1, R2, and R3. Each resource can accommodate one process and takes some undefined amount of time to complete a request. Draw the resource request/allocation graph of the following sequence of events: Process A starts and announces it could potentially ask for R1 and R3. Process B starts and announces it could potentially ask for R1, R2 and R3. Process A asks for R1. Process B asks for R2. Draw the graph. (1) Would you grant these two requests? Yes. There is no cycle in the graph. No deadlock. (2) How would you deal with future requests? Must be careful not to create cycle in the graphy. II. Banker's Algorithm ====================== An algorithm to attmpt to compute a safe sequence and determine if a given request can be granted. m resoruces, n processes. Maintain 3 n X m matrices: Max(j, k) = maximum number of resource k process j will request. Allocation(j ,k) = number of resources currently allocated to process j. Need(j, k) = number of resource k still needed by process j. (Need(j, k) = Max(j, k) - Allocation(j ,k)) Available(k) = number of resource k that are still avaiable. Request(j, k) = number of resource k requested by process j. Given a Request(j, k), (a) Alloc*(j, k) = Allocation(j, k) + Request(j, k); Avail*(k) = Available(k) - Request(j, k); Need*(j, k) = Max(j, k) - Alloc*(j ,k); (b) If for all x = 1, 2, ... m, sum {i = 1 to n} (Need*(i, k)) <= Avail*(k), No deadlock. Grant request and return; // All processes can finish (c) Find an unmarked process i such that for all x = 1, 2, ..., m, Need*(i, k) <= Avail*(k). If there is no such process, unsafe, deny request and return; Otherwise mark process i as "finished". (d) If all processes are marked as finished, no deadlock, grant request and return; (e) For all x = 1, 2, ..., m, Avail*(k) = Avail*(k) + Alloc*(i, k). Goto (c). Advantages and disadvantages of Banker's Algorithm?