Lecture 26 Syed Mansoor Sarwar Operating Systems Lecture 26 Syed Mansoor Sarwar
© Copyright Virtual University of Pakistan Agenda for Today Review of previous lecture Monitors—Dining Philosophers The deadlock problem Deadlock characterization Deadlock handling Deadlock prevention Recap of lecture 19 April 2019 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Review of Lecture 25 Dining philosophers problem High-level language constructs Critical region Monitor 19 April 2019 © Copyright Virtual University of Pakistan
Dining Philosophers Example monitor dp { enum {thinking, hungry, eating} state[5]; condition self[5]; void pickup(int i) // Following slides void putdown(int i) // Following slides void test(int i) // Following slides void init() { for (int i = 0; i < 5; i++) state[i] = thinking; } 19 April 2019 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Dining Philosophers void pickup(int i) { state[i] = hungry; test(i); if (state[i] != eating) self[i].wait(); } 19 April 2019 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Dining Philosophers void putdown(int i) { state[i] = thinking; // test left and right // neighbors test((i+4) % 5); test((i+1) % 5); } 19 April 2019 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Dining Philosophers void test(int i) { if ((state[(i+4)%5]!= eating) && (state[i] == hungry) && (state[(i+1)%5]!= eating)) { state[i] = eating; self[i].signal(); } 19 April 2019 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan The Deadlock Problem A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set. 19 April 2019 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan The Deadlock Problem Example System has 2 tape drives. P1 and P2 each hold one tape drive and each needs another one. 19 April 2019 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan The Deadlock Problem Example: semaphores A and B, initialized to 1 P0 P1 wait (A); wait(B); wait (B); wait(A); 19 April 2019 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan The Deadlock Problem signal(A); P0 P1 signal(B); 19 April 2019 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan The Deadlock Problem The Dining Philosophers problem … all philosophers become hungry at the same time, pick up the chopsticks on their right and wait for getting the chopsticks on their left. 19 April 2019 © Copyright Virtual University of Pakistan
Bridge Crossing Example Traffic only in one direction. Each section of a bridge can be viewed as a resource. 19 April 2019 © Copyright Virtual University of Pakistan
Bridge Crossing Example If a deadlock occurs, it can be resolved if one car backs up (preempt resources and rollback). Several cars may have to be backed up if a deadlock occurs. Starvation is possible. 19 April 2019 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan System Model Resource types R1, R2, . . ., Rm (CPU cycles, memory space, I/O devices) Each resource type Ri has Wi instances. Each process utilizes a resource as follows: request → use → release 19 April 2019 © Copyright Virtual University of Pakistan
Deadlock Characterization Deadlock can arise if four conditions hold simultaneously. Mutual exclusion: only one process at a time can use a resource. 19 April 2019 © Copyright Virtual University of Pakistan
Deadlock Characterization Hold and wait: a process holding at least one resource is waiting to acquire additional resources held by other processes. 19 April 2019 © Copyright Virtual University of Pakistan
Deadlock Characterization No preemption: a resource can be released only voluntarily by the process holding it, after that process has completed its task. 19 April 2019 © Copyright Virtual University of Pakistan
Deadlock Characterization Circular wait: There exists a set {P0, P1, …, P0} of waiting processes such that P0 is waiting for a resource that is held by P1, P1 is waiting for a resource that is held by P2, …, Pn–1 is waiting for a resource that is held by Pn, and P0 is waiting for a resource that is held by P0. P0 → P1 → P2 → … → Pn → P0 19 April 2019 © Copyright Virtual University of Pakistan
Resource Allocation Graph A set of vertices V and a set of edges E. V is partitioned into two types: P = {P1, P2, …, Pn} R = {R1, R2, …, Rm} E = {Request Edges, Assignment Edges} Request Edge: P1 Rj Assignment Edge: Rj Pi 19 April 2019 © Copyright Virtual University of Pakistan
Resource Allocation Graph Process Resource Type with 4 instances Pi requests instance of Rj Pi is holding an instance of Rj Pi Pi 19 April 2019 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Example Graph 19 April 2019 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Graph with a Deadlock 19 April 2019 © Copyright Virtual University of Pakistan
Graph with a Cycle but No Deadlock 19 April 2019 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan 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. 19 April 2019 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Recap of Lecture Monitor-based implementation of the dining philosophers problem The deadlock problem The system model Deadlock characterization The resource allocation graph 19 April 2019 © Copyright Virtual University of Pakistan
Lecture 26 Syed Mansoor Sarwar Operating Systems Lecture 26 Syed Mansoor Sarwar