Definition
Monitor
A monitor is a high-level synchronisation construct that encapsulates data, procedures, and initialisation code into a single software module. It is designed to provide automatic mutual exclusion for processes accessing shared data.
Unlike semaphores, which require explicit
waitandsignalcalls around every critical section, a monitor ensures that only one process can be active within its boundaries at any given time.
Core Properties
- Internal Data Protection: Local variables of a monitor can only be accessed by its own procedures.
- Entry Queue: A process enters the monitor by calling one of its procedures. If another process is already active in the monitor, the caller is placed in an entry queue and suspended.
- Synchronisation: Monitors use condition variables to handle situations where a process must wait for a specific state (e.g., a buffer to become non-empty).
Monitor Queues
A monitor manages several queues to ensure mutual exclusion and correct synchronisation:
- Entrance Queue: Processes waiting to enter the monitor.
- Urgent Queue: Contains processes that have been deblocked by a
csignaland are waiting to resume execution. These are typically given higher priority than processes in the Entrance Queue to ensure the signaled condition is handled quickly. - Condition Queues: One queue for each condition variable , containing processes currently suspended by
cwait(c).
Advantages
- Robustness: Because mutual exclusion is handled by the compiler/monitor structure, it is less prone to programming errors than semaphores.
- Clarity: The synchronisation logic is centralised within the module rather than being scattered across multiple processes.