Mod 3: Semaphores Flashcards
What is “busy waiting”?
(1) A thread is spinning in a loop waiting for a resource to become available, instead of waiting for it to be signaled
(2) Synchronization technique that can be quite wasteful of resources
What is a semaphore?
a Synchronization method
(1) Counter
(2) Abstract data type, not integer
(3) Shared between threads
What is a semaphore used for?
Semaphores are used to synchronize access to shared resources, and can solve a wide range of synchronization issues
Which to atomic operations does semaphores use?
Wait( ) (or P, Down)
Signal( ) (or V, Up)
How does a semaphore work?
It’s a counter that keeps tabs on available resources, when a thread requests access to resource the counter is decremented, and when the threads release the semaphore the counter is incremented. If the counter is negative, the thread has to wait
What are the attributes of a semaphore?
(1) Value, a integer
(2) A queue, holding blocked threads, not accessible for the programmer
What are the methods/operations of a semaphore?
(1) Initialize(s), where s is number of available resources
(2) P or Wait, called by threat who want to enter the CS
(3) V or Signal, called by thread who want to exit the CS, if the in a blocked thread, it is unblocked once the counter is incremented
Does a semaphore have history?
Yes, a semaphore remember which thread has released the ressource
Does the thread owns the semaphore if it uses it?
No, a semaphore is not owned by a thread
What are some real-life examples of semaphore uses?
- Traffic light,
- Parking slots in a parking lot,
- Counters in an bank office (nbr of service ppl available)
What are the rule for number of semaphore required?
One for each constraint
What are the two types of semaphore?
(1) Counting semaphore - used for a resource with many units or when several threat are allowed to access the resource (eg. for reading)
(2) Binary semaphore - can only have value 0 (wait) or 1 (signal), used as lock mechanism or where only on thread can access a resource
Which values can a counting semaphore have?
0, and positive or negative value (-1 means no resources are available).
What is a binary semaphore used for?
Mutual Exclusion, ME
What are the main differences between a lock and a binary semaphore?
(1) Locking usually disables interrupts inside CS, Semaphore does not