Monitor vs Semaphore Last Updated : 20 May, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Both semaphores and monitors are used to solve the critical section problem (as they allow processes to access the shared resources in mutual exclusion) and to achieve process synchronization in the multiprocessing environment. Monitor: A Monitor type high-level synchronization construct. It is an abstract data type. The Monitor type contains shared variables and the set of procedures that operate on the shared variable. When any process wishes to access the shared variables in the monitor, it needs to access it through the procedures. These processes line up in a queue and are only provided access when the previous process release the shared variables. Only one process can be active in a monitor at a time. Monitor has condition variables. Syntax: monitor { //shared variable declarations data variables; Procedure P1() { ... } Procedure P2() { ... } . . . Procedure Pn() { ... } } Semaphore: A Semaphore is a lower-level object. A semaphore is a non-negative integer variable. The value of Semaphore indicates the number of shared resources available in the system. The value of semaphore can be modified only by two functions, namely wait() and signal() operations (apart from the initialization). When any process accesses the shared resources, it performs the wait() operation on the semaphore and when the process releases the shared resources, it performs the signal() operation on the semaphore. Semaphore does not have condition variables. When a process is modifying the value of the semaphore, no other process can simultaneously modify the value of the semaphore. The Semaphore is further divided into 2 categories: Binary semaphore Counting semaphore Syntax: // Wait Operation wait(Semaphore S) { while (S<=0); S--; } // Signal Operation signal(Semaphore S) { S++; } Advantages of Monitors: Monitors are easy to implement than semaphores. Mutual exclusion in monitors is automatic while in semaphores, mutual exclusion needs to be implemented explicitly. Monitors can overcome the timing errors that occur while using semaphores. Shared variables are global to all processes in the monitor while shared variables are hidden in semaphores. Advantages of Semaphores: Semaphores are machine independent (because they are implemented in the kernel services). Semaphores permit more than one thread to access the critical section, unlike monitors. In semaphores there is no spinning, hence no waste of resources due to no busy waiting. Comment More infoAdvertise with us Next Article Binary Semaphore in Operating System V verma_anushka Follow Improve Article Tags : Operating Systems GATE CS Process Synchronization Similar Reads Mutex vs Semaphore In the Operating System, Mutex and Semaphores are kernel resources that provide synchronization services (also known as synchronization primitives). Synchronization is required when multiple processes are executing concurrently, to avoid conflicts between processes using shared resources. In this ar 8 min read Semaphores and its types A semaphore is a tool used in computer science to manage how multiple programs or processes access shared resources, like memory or files, without causing conflicts. Semaphores are compound data types with two fields one is a Non-negative integer S.V(Semaphore Value) and the second is a set of proce 6 min read Binary Semaphore in Operating System Semaphores are one of the easiest and best process synchronization mechanisms founded by Dijkstra in the mid-'90s. Binary Semaphore provides mutual synchronization between the processes in an operating system. It has an integer range of values from 0 to 1. Basically, Binary Semaphores have two opera 3 min read Difference between Semaphore and Condition Variable 1. Semaphore :Semaphore, as name suggests, is basically an object that includes counter, waiting list of process and supports two different operations i.e., wait and signal. Its type includes counting semaphores and binary semaphores. It is simply a synchronization tool that can be used to deal with 2 min read Dining Philosopher Problem Using Semaphores The Dining Philosopher Problem states that K philosophers are seated around a circular table with one chopstick between each pair of philosophers. There is one chopstick between each philosopher. A philosopher may eat if he can pick up the two chopsticks adjacent to him. One chopstick may be picked 11 min read Semaphores Solutions in Operating System A Semaphore can be described as an object that consists of a counter, a waiting list of processes, Signal and Wait functions. The most basic use of semaphore is to initialize it to 1. When a thread want to enter a critical section, it calls down and enter the section. When another thread tries to do 3 min read Like