Download Operating Systems Lecture Notes: Process Synchronization and Resource Allocation - Prof. T and more Study notes Operating Systems in PDF only on Docsity!
1
CSC 4103 - Operating Systems
Spring 2007
Tevfik Koşar
Louisiana State University
February 8th, 2007
Lecture - VI
Process Synchronization - II
2
Bounded Buffer Problem
- The structure of the producer process
do {
// produce an item
wait (empty); wait (mutex);
// add the item to the buffer
signal (mutex); signal (full); } while (true);
3
Bounded Buffer Problem (Cont.)
- The structure of the consumer process
do { wait (full); wait (mutex);
// remove an item from buffer
signal (mutex); signal (empty);
// consume the removed item
} while (true);
4
Bounded Buffer – 1 Semaphore Soln
- The structure of the producer process int empty=N, full=0; do { // produce an item wait (mutex); if (empty> 0){ // add the item to the buffer empty --; full++; } signal (mutex);
} while (true);
7
Bounded Buffer – 1 Semaphore Soln - II
- The structure of the consumer process
do { while (full == 0){} wait (mutex); // remove an item from buffer full--; empty++; signal (mutex);
// consume the removed item
} while (true);
8
Bounded Buffer – 2 Semaphore Soln
- The structure of the producer process
do {
// produce an item wait (empty); // add the item to the buffer signal (full);
} while (true);
9
Bounded Buffer – 2 Semaphore Soln
- The structure of the consumer process
do { wait (full); // remove an item from buffer signal (empty);
// consume the removed item
} while (true);
10
Readers-Writers Problem
- The structure of a writer process
do {
wait (wrt) ;
// writing is performed
signal (wrt) ;
} while (true)
13
Dining-Philosophers Problem (Cont.)
- The structure of Philosopher i:
Do { wait ( chopstick[i] ); wait ( chopStick[ (i + 1) % 5] );
// eat
signal ( chopstick[i] ); signal (chopstick[ (i + 1) % 5] );
// think
} while (true) ;
14
To Prevent Deadlock
- Ensures mutual exclusion, but does not prevent
deadlock
- Allow philosopher to pick up her chopsticks only if both
chopsticks are available (i.e. in critical section)
- Use an asymmetric solution: an odd philosopher picks
up first her left chopstick and then her right chopstick;
and vice versa
15
Disjkstra’s Solution
For each philosopher i:
- Think
- Wait(room_semaphore) //max value = 4
- Wait(left_fork_i)
- Wait(right_fork_i)
- Eat spaghetti
- Signal(left_fork_i)
- Signal(right_fork_i)
- Signal(room_semaphore)
- Repeat 1.
- Only four philosophers would be able to enter the room simultaneously and no deadlock can occur.
16
Chandy/Misra Solution (1984)
- For every pair of philosophers contending for a resource, create a fork and give it to the philosopher with the lower ID. Each fork can either be dirty or clean. Initially, all forks are dirty.
- When a philosopher wants to use a set of resources (i.e., eat), it must obtain the forks from its contending neighbors. For all such forks it does not have, it sends a request message.
- When a philosopher with a fork receives a request message, it keeps the fork if it is clean, but gives it up when it is dirty. If it sends the fork over, it cleans the fork before doing so.
- After a philosopher is done eating, all its forks become dirty. If another philosopher had previously requested one of the forks, it cleans the fork and sends it.
19
Implementation:
- Semaphore Customers
- Semaphore Barber
- Semaphore accessSeats (mutex)
- int NumberOfFreeSeats The Barber(Thread): while(true) //runs in an infinite loop { Customers.wait() //tries to acquire a customer - if none is available he's going to sleep accessSeats.wait() //at this time he has been awaken -> want to modify the number of available seats NumberOfFreeSeats++ //one chair gets free Barber.signal() // the barber is ready to cut accessSeats.signal() //we don't need the lock on the chairs anymore //here the barber is cutting hair }
20
The Customer(Thread):
while (notCut) //as long as the customer is not cut { accessSeats.wait() //tries to get access to the chairs if (NumberOfFreeSeats>0) { //if there are any free seats NumberOfFreeSeats -- //sitting down on a chair Customers.signal() //notify the barber, who's waiting that there is a customer accessSeats.signal() // don't need to lock the chairs anymore Barber.wait() // now it's this customers turn, but wait if the barber is busy notCut = false } else // there are no free seats //tough luck accessSeats.signal() //but don't forget to release the lock on the seats }
21
Any Questions?
Hmm.. Hmm..
22
Reading Assignment
- Read chapter 6 from Silberschatz.