Module_2_Process Synchronization_ Part_2
Module_2_Process Synchronization_ Part_2
in
Process Synchronization
Process Synchronization
• Process Synchronization is mainly needed in a multi-
process system when multiple processes are running
together
Dept. of BCA • More than one processes try to gain access to the
Click to Edit
1.Entry Section: In this section mainly the process requests for its
entry in the critical section.
section problems.
Dept. of BCA
Click to Edit
•The above shows the structure of process Pi in Peterson's
solution.
Suppose there are N processes (P1, P2, ... PN) and as
at some point of time every process requires to enter
in the Critical Section
Dept. of BCA A FLAG [] array of size N is maintained here which is
Click to Edit
return rv; }
• Now, as a solution to the critical section, how this
TestAndSet() instruction be implemented to achieve mutual
exclusion, bounded wait and progress.
• Let us do it one by one, first, we will try to achieve mutual
exclusion using TestAndSet() instruction and for that,
Dept. of BCA
Click to Edit • You have to globally declare a Boolean variable lock and
initialize it to false.
•Consider we have three processes P0, P1 and P2 are interested to enter their critical section. So the
Click to Edit
• When finished, it introduces the
lock so that different threads or
processes can take possession of it..
Types of Mutex Locks
• Mutex locks come in a variety of forms that
offer varying levels of capabilities and
behavior.
Click to Edit
Click to Edit
oBy hindering looping lock appropriation, it
makes a guarantee that an application or
procedure doesn't take over a mutex lock it
presently already holds.
Times Mutex
o For a predetermined amount of time, an algorithm or
procedure can try to acquire a lock using a timed
mutex.
o The acquiring functioning fails if the security key
Click to Edit
Click to Edit
problem
• By using two atomic operations, wait
and signal that are used for process
synchronization.
The definitions of wait and signal are as follows
−
Wait
The wait operation decrements the value of its argument S, if it is
positive. If S is negative or zero, then no operation is performed.
Click to Edit wait(S)
{
while (S<=0);
S--;
}
Signal
The signal operation increments the value of its argument S.
signal(S)
{
S++;
}
Types of Semaphores
Click to Edit
• There are two main types of semaphores i.e. Counting
semaphores and Binary semaphores. Details about these are
given as follows −
Counting Semaphores
• These are integer value semaphores and have an
unrestricted value domain.
• These semaphores are used to coordinate the resource
access, where the semaphore count is the number of
Click to Edit available resources.
• If the resources are added, semaphore count
automatically incremented and if the resources are
removed, the count is decremented.
Binary Semaphores
• The binary semaphores are like counting
semaphores but their value is restricted to 0 and 1.
• The wait operation only works when the
semaphore is 1 and the signal operation succeeds
Click to Edit
when semaphore is 0.
• It is sometimes easier to implement binary
semaphores than counting semaphores.
Classic Problems of
Synchronization
Readers and Writers Problem:
Reader’s writer problem is another example of a classic
synchronization problem
Problem statement
• There is a shared resource which should be accessed by multiple
Click to Edit
processes.
• There are two types of processes in this context.
• They are reader and writer.
• Any number of readers can read from the shared resource
simultaneously, but only one writer can write to the shared
• When a writer is writing data to the resource, no other
process can access the resource.
• A writer cannot write to the resource if there are non-zero
number of readers accessing the resource at that time.
The Solution
Click to Edit
• From the above problem statement, it is evident that readers have
higher priority than writer.
• If a writer wants to write to the resource, it must wait until there are no
readers currently accessing that resource.
• An integer variable read_count is used to maintain the
number of readers currently accessing the resource.
• The variable read_count is initialized to 0.
wait(mutex);
} while(true);
Dining Philosophers
Problem
wait (chopstick[i]);
} while(true);
Fig. The structure of Philosopher
• Although this solution guarantees that no two
neighbours are eating simultaneously, it nevertheless
must be rejected because it could create a deadlock.
• Suppose that all five-philosopher become hungry at
Click to Edit the same time and each grabs her left chopstick.
• All the elements of chopstick will now be equal to 0.
{Procedure P1(…) {
….}
Procedure P2(…) { ….
Click to Edit
}Procedure Pn(…){
…. }
Initialization code (…) {…
}}
I.A procedure defined within a monitor can access only those
variables declared locally within the monitor and its formal
parameters.
• If it will not, then the instances are not identical, and the
resource type classes have not been defined properly.
A process must request a resource before using it, and must
Click to Edit release the resource after using it.
A process may request as many resources as it requires
carrying out its designated task.
Example of Deadlock
A real-world example would be traffic, which is going only
in one direction.
Here, a bridge is considered a resource.
So, when Deadlock happens, it can be easily resolved if one
Click to Edit
car backs up (Pre-empt resources and rollback).
Several cars may have to be backed up if a deadlock
situation occurs.
So, starvation is possible.
Example of deadlock
Click to Edit Under the normal mode of operation, a process may utilize a resource in only
the following sequence:
Request: If the request cannot be granted immediately, then the requesting
process must wait until it can acquire the resource.
Click to Edit
A deadlock situation can arise if the following four conditions hold
simultaneously in a system:
Click to Edit 4. Circular wait: There must exist a set (P0 , P1 ... Pn ) of waiting
processes such that PO is waiting for a resource that is held by P1 , P1 is
waiting for a resource that is held by P2 , ..... Pn-1 is waiting for a resource
that is held by Pn. and Pn is waiting for a resource that is held by P0.
Methods of Handling Deadlock
1. Deadlock Ignorance
2. Deadlock Prevention
3. Deadlock Avoidance
• Deadlock prevention means try to make all four conditions false or at least
try to remove one of the condition or at least make one of the conditions is
Deadlock ignorance
Deadlock ignorance refers to a situation in computing or
Click to Edit
A deadlock occurs when two or more processes are unable to
holds.
Deadlock Detection and Recovery
Deadlock occurs when a set of processes are blocked because
Click to Edit
Detection involves monitoring the state of resource allocation and
• Process states:
o Process P1 is holding an instance of resource type R2
Click to Edit and is waiting for an instance of resource type R1.
o Process P2 is holding an instance of R1 and an instance
of R2 and is waiting for an instance of R3.
o Process P3 is holding an instance of R3
o E= {P1→ R1, P2→R3, R1→P2, R2→P2, R2→P1,
R3→P3}
Thank You !!!
Click to Edit