14 2012 University of Illinois Slides adapted in part from material accompanying Bryant amp OHallaron Computer Systems A Programmers Perspective 2E Announcements ID: 1002123
Download Presentation The PPT/PDF document "Using Semaphores CS 241 March" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
1. Using SemaphoresCS 241March 14, 2012University of IllinoisSlides adapted in part from material accompanying Bryant & O’Hallaron, “Computer Systems: A Programmer's Perspective”, 2/E
2. AnnouncementsMP6 releasedTodayA few midterm problemsUsing semaphores: the producer-consumer problemUsing semaphores: the readers-writers problem
3. Midterm problem discussion
4. Using Semaphores
5. Before: Basic use of semaphoresvoid * worker( void *ptr ){ int i; for (i = 0; i < ITERATIONS_PER_THREAD; i++) { sem_wait(&cnt_mutex); cnt++; sem_post(&cnt_mutex); }}
6. Today: Advanced use of semaphores[Monty Python’s Flying Circus]
7. Using semaphores:The Producer-Consumer Problem
8. Producer-consumer problemChefs cook items and put them on a conveyer beltWaiters pick items off the belt
9. Producer-consumer problemNow imagine many chefs!...and many waiters!
10. Producer-consumer problemA potential mess!
11. Producer-consumer problemChef (Producer)Waiter (Consumer)inserts itemsremoves itemsShared resource:bounded bufferEfficient implementation:circular fixed-size buffer
12. Shared bufferChef (Producer)Waiter (Consumer)
13. Shared bufferinsertPtrremovePtrWhat does the chef do with a new pizza?Where does the waiter take a pizza from?Chef (Producer)Waiter (Consumer)
14. Shared bufferinsertPtrremovePtrInsert pizzainsertPtrChef (Producer)Waiter (Consumer)
15. Shared bufferinsertPtrremovePtrInsert pizzaChef (Producer)Waiter (Consumer)
16. Shared bufferinsertPtrremovePtrInsert pizzaChef (Producer)Waiter (Consumer)
17. Shared bufferinsertPtrremovePtrRemove pizzaremovePtrChef (Producer)Waiter (Consumer)
18. Shared bufferinsertPtrremovePtrInsert pizzaChef (Producer)Waiter (Consumer)
19. Shared bufferinsertPtrremovePtrInsert pizzaChef (Producer)Waiter (Consumer)
20. Shared bufferinsertPtrremovePtrBUFFER FULL: Producer must wait!Insert pizzaChef (Producer)Waiter (Consumer)
21. Shared bufferinsertPtrremovePtrRemove pizzaChef (Producer)Waiter (Consumer)
22. Shared bufferinsertPtrremovePtrRemove pizzaChef (Producer)Waiter (Consumer)
23. Shared bufferinsertPtrremovePtrRemove pizzaChef (Producer)Waiter (Consumer)
24. Shared bufferinsertPtrremovePtrRemove pizzaChef (Producer)Waiter (Consumer)
25. Shared bufferinsertPtrremovePtrRemove pizzaChef (Producer)Waiter (Consumer)
26. Shared bufferinsertPtrremovePtrRemove pizzaChef (Producer)Waiter (Consumer)
27. Shared bufferinsertPtrremovePtrRemove pizzaChef (Producer)Waiter (Consumer)
28. Shared bufferinsertPtrremovePtrBuffer empty: Consumer must be blocked!Remove pizzaChef (Producer)Waiter (Consumer)
29. Designing a solutionChef (Producer)Waiter (Consumer)Wait for empty slotInsert itemSignal item arrivalWait for item arrivalRemove itemSignal empty slot availableWhat synchronization do we need?
30. Designing a solutionChef (Producer)Waiter (Consumer)Wait for empty slotInsert itemSignal item arrivalWait for item arrivalRemove itemSignal empty slot availableWhat synchronization do we need?Mutex(shared buffer)
31. Designing a solutionChef (Producer)Waiter (Consumer)Wait for empty slotInsert itemSignal item arrivalWait for item arrivalRemove itemSignal empty slot availableWhat synchronization do we need?Semaphore(# empty slots)
32. Designing a solutionChef (Producer)Waiter (Consumer)Wait for empty slotInsert itemSignal item arrivalWait for item arrivalRemove itemSignal empty slot availableWhat synchronization do we need?Semaphore(# filled slots)
33. Producer-Consumer Codebuffer[ insertPtr ] = data;insertPtr = (insertPtr + 1) % N;result = buffer[removePtr];removePtr = (removePtr +1) % N;Critical Section: move insert pointerCritical Section: move remove pointer
34. Producer-Consumer Codesem_wait(&slots);mutex_lock(&mutex);buffer[ insertPtr ] = data;insertPtr = (insertPtr + 1) % N;mutex_unlock(&mutex);sem_post(&items);sem_wait(&items);mutex_lock(&mutex);result = buffer[removePtr];removePtr = (removePtr +1) % N;mutex_unlock(&mutex);sem_post(&slots);Block if there are no free slotsBlock if there are no items to takeCounting semaphore – check and decrement the number of free slotsCounting semaphore – check and decrement the number of available itemsDone – increment the number of available itemsDone – increment the number of free slots
35. Consumer Pseudocode: getItem()sem_wait(&items);pthread_mutex_lock(&mutex);result = buffer[removePtr];removePtr = (removePtr +1) % N;pthread_mutex_unlock(&mutex);sem_signal(&slots);Error checking/EINTR handling not shown
36. Producer Pseudocode: putItem(data)sem_wait(&slots);pthread_mutex_lock(&mutex);buffer[ insertPtr ] = data;insertPtr = (insertPtr + 1) % N;pthread_mutex_unlock(&mutex);sem_signal(&items);Error checking/EINTR handling not shown
37. Readers-Writers Problem
38. Readers-Writers ProblemGeneralization of the mutual exclusion problemProblem statement:Reader threads only read the objectWriter threads modify the objectWriters must have exclusive access to the objectUnlimited number of readers can access the objectOccurs frequently in real systems, e.g.,Online airline reservation systemMultithreaded caching Web proxy
39. Variants of Readers-Writers Favor readersNo reader waits unless a writer is already in critical sectionA reader that arrives after a waiting writer gets priority over writerFavor writersOnce a writer is ready to write, it performs its write as soon as possible A reader that arrives after a writer must wait, even if the writer is also waitingStarvation (thread waits indefinitely) possible in both casesQ: How could we fix this?
40. void writer(void) { while (1) { sem_wait(&w); /* Critical section */ /* Writing here */ sem_post(&w); }}Writers:int readcnt; /* Initially = 0 */sem_t mutex, w; /* Both initially = 1 */Shared:Solution favoring readers
41. (full codeonline)void reader(void) { while (1) { sem_wait(&mutex); readcnt++; if (readcnt == 1) /* First reader in */ sem_wait(&w); /* Lock out writers */ sem_post(&mutex); /* Main critical section */ /* Reading would happen here */ sem_wait(&mutex); readcnt--; if (readcnt == 0) /* Last out */ sem_post(&w); /* Let in writers */ sem_post(&mutex); }}Readers:Solution favoring readers
42. SummarySynchronization: more than just locking a critical sectionSemaphores useful for counting available resourcessem_wait(): wait for resource only if none availablesem_post(): signal availability of another resourceMultiple semaphores / mutexes can work together to solve complex problems