Week 10: Semaphores Flashcards
What is a race condition?
A situation where the outcome depends on the sequence or timing of processes accessing shared resources.
How can race conditions be prevented?
By ensuring only one process accesses a shared resource at a time (mutual exclusion).
What is a critical section?
A part of a process that accesses shared resources
Why does using a critical_region flag fail?
A process may be interrupted between checking and setting the flag, allowing another process to enter the critical section.
Why does disabling interrupts fail as a solution?
It:
- Cannot work in multi-processor
- Risks forgetting to re-enable
- May be abused by selfish users.
What does strict alternation enforce?
Processes take turns entering their critical sections based on a shared turn variable.
What are the drawbacks of strict alternation?
It forces processes to wait unnecessarily and prevents a process from entering its critical section twice in a row.
What is Peterson’s solution?
A mutual exclusion algorithm with the following variables:
Each process has an integer id number
Two shared variables are needed:
- turn: Indicates whose turn it is.
- interested[]: Flags whether a process wants to enter its critical section.
Each process must know the other’s process id
Why is Peterson’s solution effective?
It combines lock variables with turn-taking, ensuring fairness and preventing simultaneous access.
What is busy waiting?
A process repeatedly checks a condition in a loop, wasting CPU time.
Why is busy waiting problematic?
It wastes resources and may prevent lower-priority processes from running in priority scheduling systems.
What is the purpose of the sleep and wakeup system calls?
To prevent busy waiting by blocking a process (sleep) until it is explicitly awakened (wakeup) by another process.
What is the difficulty with using sleep and wakeup?
A wakeup call can be lost if it occurs before the sleeping process actually starts sleeping, leading to deadlocks.
What is the producer-consumer problem?
A synchronization problem where a producer adds data to a shared buffer, and a consumer removes it, requiring coordination to avoid overflows and underflows.
How do sleep and wakeup address the producer-consumer problem?
The producer sleeps if the buffer is full, and the consumer wakes it up when space is available, and vice versa.