MODULE 3 - PART 1 - Process Coordination and Deadlock..
MODULE 3 - PART 1 - Process Coordination and Deadlock..
Exit Section-
• It acts as an exit gate for a process to leave the critical
section.
• When a process takes exit from the critical section, some
changes are made so that other processes can enter
inside the critical section.
Peterson’s Solution
• Peterson’s Solution is restricted to two processes that alternate
execution between their critical sections. Let’s call the processes –
Process i and Process j.
• We consider two data items – turn and flag.
• The data item turn indicates whose turn it is to enter its critical
section.
• The data item flag can be either true or false. The value true indicates
that the process is ready to enter its critical section.
Synchronization Hardware
• This is a hardware solution to the synchronization problem.
• We use the Test Lock and Set Lock instructions.
• The shared lock variable can take either of the two values – 0 or 1.
• If lock value is 0 (unlocked), a process can take the lock, change its
value to 1, and then execute in its critical section.
• If the lock value is 1 (locked), a process cannot take the lock. It has to
wait till the lock value becomes 0.
Semaphores
• Semaphores are a synchronization mechanism used to coordinate the
activities of multiple processes in a computer system.
• They are used to enforce mutual exclusion, avoid race conditions and
implement synchronization between processes.
• Semaphores provide two operations: wait (P) and signal (V).
• The wait operation decrements the value of the semaphore, and the signal
operation increments the value of the semaphore.
• When the value of the semaphore is zero, any process that performs a wait
operation will be blocked until another process performs a signal
operation.
Types of Semaphores
Binary Semaphore:
• It is a special form of semaphore used for implementing mutual exclusion,
hence it is often called a Mutex.
• A binary semaphore is initialized to 1 and only takes the values 0 and 1
during the execution of a program.
• In Binary Semaphore, the wait operation works only if the value of
semaphore = 1, and the signal operation succeeds when the semaphore=
0.
• Binary Semaphores are easier to implement than counting semaphores.
Counting Semaphore:
• These are used to implement bounded concurrency.
• These can be used to control access to a given resource that consists of a
finite number of Instances.
• Here the semaphore count is used to indicate the number of available
resources.
• If the resources are added then the semaphore count automatically gets
incremented and if the resources are removed, the count is decremented.
• Counting Semaphore has no mutual exclusion.
• The value of semaphore can vary from –infinity to +infinity.
Monitors
• The monitor is one of the ways to achieve Process synchronization. The monitor is
supported by programming languages to achieve mutual exclusion between
processes.
• It is the collection of shared variables, condition variables and procedures
combined together in a special kind of module or a package.
• Only the procedures defined inside the monitor can access the shared variables
declared in the monitor.
• Only one process at a time can be active inside the monitor.
Difference between Semaphores and Monitors
• In Semaphores, processes can directly access shared variables unlike Monitors
where processes have to access procedures to access shared variables.
• In semaphores, we have to code cooperative processes to implement mutual
exclusion unlike monitors where the monitor contains the code to implement
mutual exclusion.
• Two different operations are performed on the condition variables of the monitor – Wait and
Signal
• let say we have 1 condition variables – x
Wait operation
• x.wait() : Process performing wait operation on any condition variable are suspended if any other
process is accessing it. The suspended processes are placed in block queue of that condition
variable.
• Note: Each condition variable has its unique block queue.
Signal operation
• x.signal(): When a process performs signal operation on condition variable, one of the blocked
processes is given chance.
• If (x block queue empty)
• // Ignore signal
• else
• // Resume a process from block queue.