






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 - XXVI April 27th, 2006
2
3
4
Task 1 Task 2 Task 3
Memory 1 Memory 2 (^) Memory 3
7
8
9
Typedef bool lock;
bool test_and_set (lock *l){ //needs to be an atomic HW instruction!! if ( *l == false){ *l = true; return true; } else return false; }
void acquire_lock (lock l) { while !test_and_set (l) {} / wait */; }
void release_lock (lock *l) { *l = false; }
10
13
14
15
P(Semaphore s) { await s > 0, then s := s-1; /* must be atomic once s > 0 is detected */ }
V(Semaphore s) { s := s+1; /* must be atomic */ }
Init(Semaphore s, Integer v) { s := v; }
16
•There are five plates at the table and five forks set between the plates.
•possibility of deadlock: if every philosopher holds a left fork and waits perpetually for a right fork (or vice versa) may lead to starvation
19
20
21
22
Implementation:
The Barber(Thread):
while(true) //runs in an infitie loop { Customers.p() //tries to acquire a customer - if none is available he's going to sleep accessSeats.p() //at this time he has been awaken -> want to modify the number of available seats NumberOfFreeSeats++ //one chair gets free Barber.v() // the barber is ready to cut accessSeats.v() //we don't need the lock on the chairs anymore //here the barber is cutting hair }