Download presentation
Presentation is loading. Please wait.
Published byRudolph Kennedy Modified over 6 years ago
1
Announcement server: server.o common.o $(CC) -o server server.o common.o $(LIBS) -lsock -lpthread and change it to: server: server.o common.o threadpool.o $(CC) -o server server.o common.o threadpool.o $(LIBS) -lsock - pthread
2
Today’s topics Project 2 How to use condition variables in Pthreads.
Classical Synchronization Problems Sample Questions
3
Using Condition Variable
THREAD A .. lock(associated_mutex) If (waiting_condition ) pthread_cond_wait(con_var, associated_mutex) Unlock (associated_mutex ) THREAD B .. lock (associated_mutex ) If (signal_condition) signal(con_var); Unlock(associated_mutex );
4
How to solve a synchorization problem by using con_var
How many condition variables do we need? When to make a thread sleep? When to signal a sleeping thread? How many associated mutexes do we need? The waiting_condition & signal condition. Insert code for wait() & signal(). Insert code for lock(mutex) & unlock(mutex). Verify & check for deadlock, race condition ..
5
How to solve a synchorization problem by using semaphore??
How many semaphore do we need? When to make a thread sleep? When to signal a sleeping thread? How many mutexes(binary semaphore) do we need? The waiting_condition & signal condition. Insert code for up() & down(). Insert code for down(mutex_sem) & up(mutex_sem) Verify & check for deadlock, race condition ..
6
procedure producer() { while (true) { item = produceItem() if (itemCount == BUFFER_SIZE) {sleep() } putItemIntoBuffer(item) itemCount = itemCount + 1 if (itemCount == 1) { wakeup(consumer) } } procedure consumer() { if (itemCount == 0) { sleep() } item = removeItemFromBuffer() itemCount = itemCount – 1 if (itemCount == BUFFER_SIZE - 1) { wakeup(producer) } consumeItem(item) } } semaphore fillCount = 0 semaphore emptyCount = BUFFER_SIZE procedure producer() { while (true) { item = produceItem() down(emptyCount) putItemIntoBuffer(item) up(fillCount) } } procedure consumer() { down(fillCount) item = removeItemFromBuffer() up(emptyCount) consumeItem(item)
7
Using cond_var for SleepingBarber
con_var SleepingBarber, waitingCustomers; mutex accessSeats; int NumOfFreeSeats=MAX; procedure Barber() { while (true) { lock(accessSeats); if (NumOfFreeSeats == MAX) _wait(SleepingBarber, accessSeats); signal (waitingCustomers); NumOfFreeSeats++; unlock(accessSeats); } procedure Custommer() { lock(accessSeats); if (NumOfFreeSeats == 0) { unlock(accessBuff); //Leave withou haircut } else { if (NumOfFreeSeats == MAX) _signal(SleepingBarber); NumOfFreeSeats --; wait (waitingCustomers, accessSeats); unlock(accessSeats);
8
Using semaphore for SleepingBarber
+ Semaphore Customers = 0 + Semaphore Barber = 0 + Semaphore accessSeats (mutex) = 1 + int NumberOfFreeSeats = N //total number of seats BARBER while(true) { down(Customers) // down(accessSeats) NumberOfFreeSeats++ up(Barber); up(accessSeats) //we don't need the lock on the chairs anymore //here the barber is cutting hair } procedure Custommer() { down(accessSeats) //tries to get access to the chairs if ( NumberOfFreeSeats > 0 ) { NumberOfFreeSeats-- up(Customers) up(accessSeats) down(Barber) //now it's this customers turn, but wait if the barber is busy //here the customer is having his hair cut } else { //there are no free seats up(accessSeats) //customer leaves without a haircut }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.