









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
Lecture - XXVII May 2nd, 2006
2
3
4
while true {
wait(T); choose smokers i and j randomly, making the third smoker k signal(A[k]);
}
7
8
A monitor consists of:
A monitor procedure takes the lock before doing anything else, and holds it until it either finishes or waits for a condition
If every procedure guarantees that the invariant is true before it releases the lock, then no task can ever find the resource in a state that might lead to a race condition.
9
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 } }
10
class Monitor{ boolean forks[]; public Monitor(int size) { forks=new boolean[size]; for(int i=0;i<forks.length;i++) forks[i]=true; } public synchronized void getForks(int id) throws Exception{ int index1; int index2; if(id==0) // Philosopher 0 { index1=0; index2=forks.length-1; } else { index1=id; index2=id-1; } while(!forks[index1] || !forks[index2]) wait(); forks[index1]=false; forks[index2]=false; System.out.println("Philospher "+id+" got the forks: "+index1+" "+index2); }
13
Task 1 (^) Task 2 Task 3
Memory 1 Memory 2 (^) Memory 3
14
15
Process 0 Data
Process 1
May I Send?
Yes
Data Data Data Data Data Data Data Data
Time
16
Process 0 (^) Process 1 Send(data) Receive(data)
19
20
21
22
25
26
27
MPI_BCast(…);
28
MPI_Reduce(…);
31
32