Module 4: Monitors & Condition Synchronization Flashcards
1
Q
Semaphore drawbacks
A
- Programmers gotta keep track of wait and signal. Thread has to release a lock before it dies.
- If the calls are done incorrectly => deadlocks.
- Unstructured => no encapsulation & data-hiding (not OOP).
- No connection: semaphore & data controlled by the semaphore.
- The multi-purpose of semahpores makes it difficult to identify what’s happening in the code
- Prone to bugs + hard to debug
2
Q
How can Monitors be an alternative to semaphores?
A
- More structure
- Ties data, methods & sync as a class that encapsulates data & operations
- Solves problem of a global shared data. All data is private.
3
Q
How does the monitor sync data?
A
- Threads trying to access shared data only interact through the monitor’s methods.
- Only 1 thread in the monitor at a time
4
Q
The process of a thread entering the monitor
A
- Entering the monitor (“enters building”. queues in “entry hall”)
- Aquiring the monitor (“enters the room from the entry hall”)
- Owning the monitor (“locks the room” and owns the monitor)
- Releasing the monitor (releases the lock when “leaving the room”. Wait queue)
- Exiting the monitor (“leaves the building”, dequeues)
5
Q
Thread waiting on a condition in a monitor
A
- Suspended thread = waits for an event to occur
- Releases monitor & waits
- Event occurs = thread is notified. Tries to aquire lock ASAP.
6
Q
Why is a monitor good?
A
- Allows threads to have mutual exclusion (only 1 thread is active in the monitor at a time)
- Ability to suspend & have threads wait for a condition
- Encapsulates data & operations
7
Q
What does a monitor encapsulate?
A
- Data: needs private variables (not outside monitor!)
- Methods: accessing shared data only in the monitor
- Synchronization: done thru condition variables (wait & signal capabilities)
8
Q
How does a monitor ensure ME?
A
Each monitor has a lock and condition variable connected to it.
9
Q
Condition variable in monitors
A
- Wait()
- Pulse()
- PulseAll()
10
Q
Wait() condition variable
A
- If a resource not available, enter condition variable queue & sleep
- Releases monitor lock
11
Q
Pulse() condition variable
A
- Wakes up a sleeping (waiting) thread
- Thread resumes its work after the lock is released
12
Q
PulseAll() condition variable
A
- Wakes up ALL waiting threads
- Thread allowed to executed is up to CPU
- No thread waiting=> signal is lost
13
Q
What is a condition variable?
A
- Container of threads waiting for a condition
- Type of queue, NOT boolean
- Like a place for threads to wait
- Waits or signals depending on the value
- Each monitor has 1 or several queues: waiting to enter and waiting after releasing.
- Queues are not directly visible to programmer
14
Q
Thread or Monitor? Active and passive objects
A
- Active: Objects that initiate actions. Implemented as threads.
- Passive: Objects that respond to actions. Implemented as monitors.
- Airport EX:
- plane INITIATES action of arrival/departure (thread)
- control tower RESPONDS to arrival/departure (monitor)
15
Q
Mutual Exclusion through monitor?
A
- No other thread will be in the executing region concurrently
- Threads in entry-set must compete to acquire monitor
- Threads in wait-set must compete to aquite monitor
- Required to: first arrive at entry, get monitor, then enter region.