Semaphores Flashcards
What is a semaphore?
A semaphore is a signal that indicates whether something can continue or must wait.
Semaphores consist of a queue and an integer value (sem).
What are the two semaphore methods?
semWait:
If sem > 0
Then sem = sem - 1
Else make the process wait
semSignal:
If there is a process waiting in the queue for this semaphore
Then allow the process to resume processing
Else sem = sem + 1
semWait and semSignal should be implemented as atomic actions.
(3 answers)
What are the different types of semaphore?
- Binary
- Mutual exclusive access to a single shared resource
- Counting
- Protect access to multiple instances of a single shared resource
- Blocking
- Synchronise two processes (a producer and a consumer)
(4 answers)
What are binary semaphores and how do they work?
- Several processes share one semaphore
- Semaphore is initialised to 1 (available)
- When a process enters the critical region, the semaphore value is changed to 0
- When the process leaves the critical region, a waiting process enters, or the semaphore value is changed back to 1
(4 answers)
What are counting semaphores and how do they work?
- Several processes share multiple semaphores (or permits)
- Semaphore is initialised to the number of instances of the resource available
- When a process enters the critical region, the semaphore value is decreased by 1
- When the process leaves the critical region, a waiting process enters or the semaphore value is increased by 1
(5 answers)
What are blocking semaphores and how do they work?
- Consumer and producer processes share one semaphore
- Semaphore is initialised to 0 (blocked)
- The consumer calls semWait to check whether data has been written
- When the producer finishes writing, it calls semSignal and the semaphore is changed to 1
- The consumer is then allowed to enter the critical region