Monitors
Notes
- Monitors are the next level of abstraction
- A monitor is a design pattern, not an os construct but rather a design pattern.
- RAII - Resource Allocation Is Initialization
- This is one of Stroustrup's designs in C++
- And possibly elsewhere
- This can actually apply as low as mutex locks
- The lock is created in the constructor
- The lock is destroyed/released in the destructor.
- But the pattern can be applied higher
- With a mutex we can get into a problem if we don't call the signal/wait pair in the proper order, only once.
- Two signals in a row, without a wait will cause a deadlock with yourself
- Two waits without a signal can cause a lock to be unlocked when it should not.
- Or only calling one of the two will cause a problem as well.
- These could occur because the calls using mutex/semiphores could be spread across a program (very large program)
- Encapsulation in a data structure will handle this.
- For example, for the readers and writers problem we have been playing with
- The monitor could
- Declare the three synchronization variables as class members
- Implement an init function that initializes them to the correct value
- Implement the following methods
- BeginRead
- EndRead
- BeginWrite
- EndWrite
- Implement a destructor that cleans things up.
- In each of the read/write methods, the logic for the mutex/semaphore is contained.
- We still have the problem if creating a monitor, but that could be
- Global
- Or a global using a singleton pattern.