lecture11-chapter05

# lecture11-chapter05 - (void semWait(mutex if(customers==n 1...

This preview shows pages 1–9. Sign up to view the full content.

Operating Systems Lecture 11: Barber Shop. Readers/Writers Anda Iamnitchi 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 Sleeping Barber Problem There is one barber, and n chairs for waiting customers If there are no customers, then the barber sits in his chair and sleeps When a new customer arrives and the barber is sleeping, then he will wakeup the barber When a new customer arrives, and the barber is busy, then he will sit on the chairs if there is any available, otherwise (when all the chairs are full) he will leave.
3 Barber Shop Hints Consider the following: Customer threads should invoke a function named getHairCut . If a customer thread arrives when the shop is full, it can invoke balk , which exits. Barber threads should invoke cutHair . When the barber invokes cutHair there should be exactly one thread invoking getHairCut concurrently.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 Sleeping Barber Solution void customer

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: (void){ semWait(mutex); if (customers==n+1) { semSignal(mutex); balk(); } customers +=1; semSignal(mutex); semSignal(customer); semWait(barber); getHairCut(); semWait(mutex); customers ‐ =1; semSignal(mutex); } void barber (void){ semWait(customer); semSignal(barber); cutHair(); } int customers = 0; mutex = Semaphore(1); customer = Semaphore(0); barber = Semaphore(0); Readers/Writers Problem • A data area is shared among many processes – Some processes only read the data area, some only write to the area • Conditions to satisfy: 1. Multiple readers may read the file at once. 2. Only one writer at a time may write 3. If a writer is writing to the file, no reader may read it. Readers have Priority Writers have Priority Writers have Priority (cont’) Writers Have Priority (on one slide)...
View Full Document

{[ snackBarMessage ]}

### Page1 / 9

lecture11-chapter05 - (void semWait(mutex if(customers==n 1...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online