60 likes | 197 Vues
Semaphores are crucial synchronization tools that allow multiple concurrent threads to coordinate through signaling. They support four main operations: initialize, get value, increment (signal/post), and decrement (wait). The thread that locks a semaphore isn't always the one that unlocks it, and blocked threads are maintained in a list. Binary semaphores restrict access to one thread at a time, akin to mutex locks, while counting semaphores can hold any integer value, facilitating complex synchronization scenarios. Examples include managing critical sections and the producer/consumer model.
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