60 likes | 183 Views
CS 311/C350. Semaphores. Semaphores – General Idea. Allows two or more concurrent threads to coordinate through signaling/waiting Has four main operations initialize, getvalue , increment ( signal ), decrement ( wait ) The thread who “locks” is not necessarily the thread who “unlocks”
E N D
CS 311/C350 Semaphores
Semaphores – General Idea • Allows two or more concurrent threads to coordinate through signaling/waiting • Has four main operations • initialize, getvalue, increment (signal), 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_signal • 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