60 likes | 190 Views
CS 311/350/550. Semaphores. Semaphores – General Idea. Allows two or more concurrent threads to coordinate through signaling/waiting Has four main operations initialize, getvalue , increment ( signal/post ), decrement ( wait )
E N D
CS 311/350/550 Semaphores
Semaphores – General Idea • Allows two or more concurrent threads to coordinate through signaling/waiting • Has four main operations • initialize, getvalue, increment (signal/post), decrement (wait) • The thread who “locks” is not necessarily the thread who “unlocks” • Those blocked-waiting are always put in a list
Semaphores – Signal/Wait • Integer counter • coordination and synchronization depends on this value • sem_wait • decrements the counter • then blocked if counter is < 0 • thread placed in the blocked list • sem_post • increments the counter • unblocks the next thread in the blocked list
Binary Semaphores • Binary Semaphores • Counter can only be one or zero • Access to the critical section is one at a time • Similar to a mutex lock
Counting Semaphores • Counter can be any integer value at any time • Used for more complex synchronization • Examples • Prioritizing access to the critical section • Producer/Consumer model with a bound buffer • Multiple counting semaphores can be used to coordinate multiple Readers/Writers