



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




1
Tevfik Koşar
February 13th, 2007
Deadlocks - I
2
Roadmap
3
Dining Philosophers Problem
•There are five plates at the table and five chopsticks set between the plates.
•Philosophers do not talk to each other.
•Semaphore chopstick [5] initialized to 1
4
Dining-Philosophers Problem (Cont.)
Do { wait ( chopstick[i] ); wait ( chopStick[ (i + 1) % 5] );
// eat
signal ( chopstick[i] ); signal (chopstick[ (i + 1) % 5] );
// think
} while (true) ;
5
Problems with Semaphores
6
Semaphores
7
Monitors
monitor monitor-name { // shared variable declarations procedure P1 (…) { …. } … procedure Pn (…) {……}
Initialization code ( ….) { … } … } }
8
Monitor - Example
As a simple example, consider a monitor for performing transactions on a bank account.
monitor account { int balance := 0
function withdraw(int amount) { if amount < 0 then error "Amount may not be negative" else if balance < amount then error "Insufficient funds" else balance := balance - amount }
function deposit(int amount) { if amount < 0 then error "Amount may not be negative" else balance := balance + amount } }
9
Schematic view of a Monitor
10
Condition Variables
If no process suspended, x.signal() operation has no effect.
11
Solution to Dining Philosophers using Monitors
monitor DP { enum { THINKING; HUNGRY, EATING) state [5] ; condition self [5]; //to delay philosopher when he is hungry but unable to get chopsticks
initialization_code() { for (int i = 0; i < 5; i++) state[i] = THINKING; }
void pickup (int i) { state[i] = HUNGRY; test(i);//only if both neighbors are not eating if (state[i] != EATING) self [i].wait; }
12
Solution to Dining Philosophers (cont)
void test (int i) { if ((state[i] == HUNGRY) && (state[(i + 1) % 5] != EATING) && (state[(i + 4) % 5] != EATING) ) { state[i] = EATING ; self[i].signal () ; } }
void putdown (int i) { state[i] = THINKING; // test left and right neighbors test((i + 4) % 5); test((i + 1) % 5); } } No two philosophers eat at the same time No deadlock But starvation can occur!
19
Resource-Allocation Graph (Cont.)
Pi Rj
Rj
20
Example of a Resource Allocation Graph
21
Basic Facts
22
Resource Allocation Graph – Example 1
No Cycle, no Deadlock
23
Resource Allocation Graph – example 2
Deadlock Which Processes deadlocked?
P1 & P2 & P
24
Cycle, but no Deadlock
25
Rule of Thumb
26
Methods for Handling Deadlocks
deadlock prevention or avoidance
deadlock detection
Programmers should handle deadlocks (UNIX, Windows)
27
Deadlock Prevention
28
Deadlock Prevention (Cont.)
29
Any Questions?
Hmm.. Hmm..
30
Reading Assignment