Monitors in Process Synchronization


Monitors are a synchronization tool used in process synchronization to manage access to shared resources and coordinate the actions of numerous threads or processes. When opposed to low-level primitives like locks or semaphores, they offer a higher-level abstraction for managing concurrency. Let's examine monitors to see what they are, why they are utilized, and how process synchronization uses them −

What are Monitors?

A synchronization technique called a monitor unifies operations and data structures into a single entity. They contain both operations that can be carried out on shared resources. By allowing only one thread or process to execute the methods included in the monitor at once, monitors ensure mutual exclusion.

Why are Monitors Used?

Monitors are used to prevent concurrent access to shared resources by numerous threads or processes. When several entities attempt to alter the same resource at once, they can avoid conflicts and data inconsistencies. Monitors give synchronization a formal method, making the design and implementation of concurrent systems simpler.

How are Monitors in Process Synchronization Implemented?

Locks, semaphores, or atomic operations are examples of synchronization primitives that can be used to build monitors. A lock or mutex connected to a monitor ensures mutual exclusion. The monitor can only be accessed by the thread or process holding the lock. Condition variables are employed within the monitor to control synchronization and communication. Using condition variables, threads or processes examine conditions and wait for the conditions to become true. When a modification to the shared resource transforms the condition into true and frees up waiting threads or processes, signaling or notification takes place.

Programming languages support the monitor to achieve process mutual exclusion. Java Synchronized methods, for instance. Java has the constructs wait() and notify().

  • It is a group of procedures and conditional variables assembled into a particular form of module or package.

  • Although they can invoke monitor procedures, programs running outside of the monitor cannot access its internal variables

  • Code inside monitors can only be executed by one process at a time.

Monitors do have certain restrictions, though. For instance, because of their higher-level abstraction, they could incur more overhead than simpler synchronization primitives like semaphores and locks. Additionally, not all synchronization issues can be solved by monitors; in some instances, lower-level primitives are needed for best performance.

Advantages of Monitor

Monitors offer several advantages in process synchronization −

Simplicity − The design and implementation of concurrent systems are made simpler by the high-level abstraction that monitors offer. The difficulty of managing concurrency is lessened by enclosing shared resources and the operations connected with them within a monitor.

Mutual − Monitors enforce mutual exclusion by limiting the concurrent execution of its methods to a single thread or process. By doing this, race situations and data inconsistencies that can arise when several entities use the same shared resources at once are avoided.

Encapsulation − Monitors encapsulate shared resources and the operations connected to them, making it simpler to analyze and control concurrency. A clear interface for accessing and modifying shared resources is provided by the encapsulation, which conceals the internal workings of synchronization

Synchronization − To control coordination and communication across threads or processes, monitors have built-in synchronization methods, such as condition variables. The use of condition variables reduces the requirement for busy waiting and increases resource efficiency by allowing threads or processes to wait until certain conditions are met.

Modularity − Monitors encourage modularity by gathering similar procedures and data together. Given that the logic for utilizing shared resources is localized within the monitor, this improves the organization and maintainability of the code.

Disadvantages of Monitor

While monitors have many benefits, there are a few potential drawbacks when it comes to synchronizing processes −

Limited Expressiveness − Within a single monitor instance, monitors are made to handle mutual exclusion and synchronization. They might not work effectively for more elaborate synchronization patterns or complex synchronization circumstances involving many monitors. Other synchronization primitives or approaches might be more suitable in such circumstances.

Lack of Composition − Most monitors don't come with built-in ways to combine or compose numerous displays. This can make it difficult to synchronize and coordinate actions across various displays or shared resources. In these situations, developers might need to incorporate more coordination mechanisms or use different synchronization methods.

Potential for Deadlocks − Monitors enforce mutual exclusion, which helps prevent deadlocks, although they do not completely rule out the possibility. If the locking and unlocking of monitors is not correctly managed, deadlocks can still happen, resulting in circumstances where threads or processes are waiting interminably for resources that are never released.

Performance Overhead − Because locks must be acquired and released, as well as because there may be conflict between threads or processes attempting to access the monitor, monitors may result in performance overhead. The locking and synchronization techniques of monitors can create bottlenecks in highly concurrent systems with frequent access to shared resources, which can affect overall performance

Conclusion

Process synchronization commonly uses monitors, which offer a high-level abstraction for controlling shared resources and enforcing mutual exclusion. While assuring that only one thread or process can use the resource at a time, monitors provide concurrent access to shared resources.

Updated on: 25-Jul-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements