What is a monitor in operating systems?
Why Interviewers Ask This
Foundational questions like this help interviewers calibrate the rest of the interview. A confident, accurate answer signals that you have solid Operating Systems basics — a prerequisite for any developer role.
Answer
A monitor is a high-level synchronization construct that bundles shared data with the procedures that access it, automatically ensuring mutual exclusion. It's like a class (OOP) with built-in locking — only ONE process/thread can be active inside the monitor at any time. Concept: the monitor handles locking automatically. You simply call monitor procedures; the monitor ensures mutual exclusion. Condition variables: within a monitor, threads can WAIT for conditions using condition variables. If a thread can't proceed (e.g., buffer is full), it calls wait() — releases the monitor lock and blocks. Another thread calls signal() to wake a waiter. Bounded buffer as monitor (Java synchronized): public synchronized void put(Object item) throws InterruptedException { while (count == N) wait(); // Buffer full -- wait buffer[in] = item; in = (in + 1) % N; count++; notifyAll(); // Wake consumers } public synchronized Object take() throws InterruptedException { while (count == 0) wait(); // Buffer empty -- wait Object item = buffer[out]; out = (out + 1) % N; count--; notifyAll(); // Wake producers return item; }. Condition types: (1) Hoare semantics: signaling thread immediately yields to waiting thread — signal guarantees the condition holds for the resumed thread; (2) Mesa semantics (Java uses this): signal just moves waiter to ready queue — waiter must re-check condition (hence while loop, not if). Monitors vs semaphores: monitors are higher-level, easier to use correctly, less error-prone. Semaphores are lower-level, more flexible. Java's synchronized keyword implements a monitor per object.
Common Mistake
Candidates often give textbook answers here. Interviewers are more impressed when you relate the concept to a specific problem you solved in a real Operating Systems project.