Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

What is the monitor in OS?

Last Updated on August 3, 2023 by Mayank Dham

One way to synchronize processes is through the operating system’s monitor. Programming languages assist the monitor in achieving mutual exclusion between various system operations. In the Java programming language, synchronization functions like wait() and notify() are provided.

What is a monitor in OS?

A feature of programming languages called monitors helps control access to shared data. The Monitor is a collection of shared actions, data structures, and synchronization between parallel procedure calls. A monitor is therefore also referred to as a synchronization tool. Some of the languages that support the usage of monitors include Java, C#, Visual Basic, Ada, and concurrent Euclid. Although they can call the monitor’s procedures, processes running outside of the monitor are unable to access its internal variables.

For example, the Java programming language provides synchronization mechanisms like the wait() and notify() constructs.

Syntax of monitor in OS

Monitor in os has a simple syntax similar to how we define a class, it is as follows:

Monitor monitorName{
variables_declaration;
condition_variables;

procedure p1{ ... };
procedure p2{ ... };
...
procedure pn{ ... };

{
    initializing_code;
}

}

In an operating system, a monitor is only a class that includes variable_declarations, condition_variables, different procedures (functions), and an initializing_code block for synchronizing processes.

Characteristics of Monitors in OS

Monitors in operating systems possess several key characteristics that make them valuable tools for managing concurrent access to shared resources. Here are the main characteristics of monitors:

  • Mutual Exclusion: Monitors ensure mutual exclusion, which means only one process or thread can be inside the monitor at any given time. This property prevents concurrent processes from accessing shared resources simultaneously and eliminates the risk of data corruption or inconsistent results due to race conditions.

  • Encapsulation: Monitors encapsulate both the shared resource and the procedures that operate on it. By bundling the resource and the relevant procedures together, monitors provide a clean and organized approach to managing concurrent access. This encapsulation simplifies the design and maintenance of concurrent programs, as the necessary synchronization logic is localized within the monitor.

  • Synchronization Primitives: Monitors often support synchronization primitives, such as condition variables. Condition variables enable threads within the monitor to wait for specific conditions to become true or to signal other threads when certain conditions are met. These primitives allow for efficient coordination among threads and help avoid busy-waiting, which can waste CPU cycles.

  • Blocking Mechanism: When a process or thread attempts to enter a monitor that is already in use, it is blocked and put in a queue (entry queue) until the monitor becomes available. This blocking mechanism avoids busy-waiting and allows other processes to proceed while waiting for their turn to access the monitor.

  • Local Data: Each thread that enters a monitor has its own local data or stack, which means the variables declared within a monitor procedure are unique to each thread’s execution. This feature prevents interference between threads and ensures that data accessed within the monitor remains consistent for each thread.

  • Priority Inheritance: In some advanced implementations of monitors, a priority inheritance mechanism can be used to prevent priority inversion. When a higher-priority thread is waiting for a lower-priority thread to release a resource inside the monitor, the lower-priority thread’s priority may be temporarily elevated to avoid unnecessary delays caused by priority inversion scenarios.

  • High-Level Abstraction: Monitors provide a higher-level abstraction for concurrency management compared to low-level synchronization mechanisms like semaphores or spinlocks. This abstraction reduces the complexity of concurrent programming and makes it easier to write correct and maintainable code.

Components of Monitor in an operating system

In an operating system, a monitor is a synchronization construct that helps manage concurrent access to shared resources by multiple processes or threads. A monitor typically consists of the following main components:

  • Shared Resource:
    The shared resource is the data or resource that multiple processes or threads need to access in a mutually exclusive manner. Examples of shared resources can include critical sections of code, global variables, or any data structure that needs to be accessed atomically.

  • Entry Queue:
    The entry queue is a data structure that holds the processes or threads that are waiting to enter the monitor and access the shared resource. When a process or thread tries to enter the monitor while it is already being used by another process, it is placed in this queue, and its execution is temporarily suspended until the monitor becomes available.

  • Entry Procedures (or Monitor Procedures):
    Entry procedures are special procedures that provide access to the shared resource and enforce mutual exclusion. When a process or thread wants to access the shared resource, it must call one of these entry procedures. The monitor’s implementation ensures that only one process or thread can execute an entry procedure at a time, thus achieving mutual exclusion.

  • Condition Variables:
    Condition variables enable communication and synchronization between processes or threads within the monitor. They allow threads to wait until a specific condition is satisfied or to signal other threads when certain conditions become true. Condition variables are crucial for avoiding busy-waiting, which can be inefficient and wasteful of system resources.

  • Local Data (or Local Variables):
    Each process or thread that enters the monitor has its own set of local data or local variables. These variables are unique to each thread’s execution and are not shared between threads. Local data allows each thread to work independently within the monitor without interfering with other threads’ data.

  • Condition Variables
    The condition variables of the monitor can be subjected to two different types of operations:

    1. Wait
    2. Signal

Consider a condition variable (y) is declared in the monitor:

y.wait(): The activity/process that applies the wait operation on a condition variable will be suspended, and the suspended process is located in the condition variable’s block queue.

y.signal(): If an activity/process applies the signal action on the condition variable, then one of the blocked activity/processes in the monitor is given a chance to execute.

Advantages of Monitor in OS

  • Compared to semaphore-based solutions, monitors have the advantage of making concurrent or parallel programming simpler and less error-prone.
  • It helps in operating system process synchronization.
  • Monitors have mutual exclusion built in.
  • Semaphores are more difficult to set up than monitors.
  • Semaphores can lead to timing errors, which monitors may be able to fix.

Disadvantages of Monitor in OS

  • Monitors must be implemented with the programming language.
  • Monitor puts more work on the compiler.
  • The monitor needs to be aware of the features offered by the operating system for managing critical steps in the parallel processes.

Conclusion:
In conclusion, monitors are essential synchronization constructs in operating systems that play a crucial role in managing concurrent access to shared resources. They provide a high-level abstraction for concurrency control, making it easier to develop correct and efficient concurrent programs. Monitors ensure mutual exclusion, encapsulate shared resources and their relevant procedures, and support synchronization primitives like condition variables. By using monitors, developers can avoid data races, prevent deadlocks, and improve the overall reliability and performance of concurrent applications.

FAQs on Monitor in OS:

1. What is a monitor in an operating system?
A monitor is a synchronization construct used in operating systems to control concurrent access to shared resources by multiple processes or threads. It provides mutual exclusion, encapsulates shared resources and relevant procedures, and supports synchronization primitives to enable efficient coordination among concurrent entities.

2. How does a monitor achieve mutual exclusion?
Monitors ensure mutual exclusion by allowing only one process or thread to be inside the monitor at any given time. When a process wants to access the shared resource, it must call an entry procedure within the monitor. If the monitor is already being used by another process, the calling process is placed in an entry queue and waits until the monitor becomes available.

3. What are condition variables in monitors?
Condition variables are synchronization primitives within a monitor that allow threads to wait until a specific condition becomes true or to signal other threads when certain conditions are met. Condition variables are essential for avoiding busy-waiting and for enabling threads to efficiently coordinate their activities.

4. Why are monitors important in concurrent programming?
Monitors are essential in concurrent programming because they provide a higher-level abstraction for managing shared resources and coordinating concurrent execution. By using monitors, developers can avoid race conditions, data corruption, and deadlocks, leading to more reliable and maintainable concurrent applications.

5. Are monitors the only way to achieve mutual exclusion?
No, monitors are not the only way to achieve mutual exclusion. Other synchronization primitives, such as semaphores, spinlocks, and locks, can also be used. However, monitors provide a more structured and convenient approach to concurrency control, making them a preferred choice in many programming languages and systems.

Leave a Reply

Your email address will not be published. Required fields are marked *