MODULE II
PROCESS SYNCHRONIZATION
PROCESS SYNCHRONIZATION
Cooperating Processes
A cooperating process is one that can affect or
be affected by other processes executing in the
system.
Independent process cannot affect or be
affected by the execution of another process.
Cooperating processes can either directly share
a logical address space or be allowed to share
data only through files or messages.
Advantages of process cooperation -
Information sharing , Computation speed-up
Synchronization
Concurrent access to shared data may result in
data inconsistency.
Maintaining data consistency requires mechanisms
to ensure the orderly execution of cooperating
processes.
Operating System Concepts
Race Condition
Race condition: The situation, where several
processes access and manipulate shared data
concurrently and,
The final value of the shared data depends on the
particular order in which the access takes place.
To guard against the race condition, we need to
ensure that only one process at a time can be
manipulating the shared data.
To prevent race conditions, concurrent processes
must be synchronized.
The Critical-Section Problems
Consider a system consisting of N processes {P0,
P1, ..., Pn−1}.
All competing to use shared data.
Each process has a segment of code, called a
critical section, in which the process may be
changing common variables, updating a table,
writing a file, and so on.
In critical section, the shared data is accessed.
The Critical-Section Problems
When one process is executing in its critical
section, no other process is allowed to execute
in its critical section.
No two processes can execute in their critical
sections at the same time.
Each process must request permission to enter its
critical section.
The Critical-Section Problems
The section of code implementing this request is
the entry section.
The critical section may be followed by an exit
section.
The remaining code is the remainder section.
Solution to Critical Section Problem -
Requirements
Mutual Exclusion
If process Pi is executing in its critical section, then no other
processes can be executing in their critical sections.
Progress
If no process is executing in its critical section and there
exists some processes that wish to enter their critical
section, then the selection of the processes that will enter
the critical section next cannot be postponed indefinitely.
Bounded Waiting
A bound must exist on the number of times that other
processes are allowed to enter their critical sections after a
process has made a request to enter its critical section and
before that request is granted.
Deadlock
In a multiprogramming environment, several
processes may compete for a finite number of
resources.
A process requests resources; if the resources are
not available at that time, the process enters a
waiting state.
Sometimes, a waiting process is never again able to
change state, because the resources it has
requested are held by other waiting processes.
This situation is called a deadlock
Deadlock
A set of blocked processes each holding a
resource and waiting to acquire a resource held
by another process in the set.
Example
System has 2 tape drives. P1 and P2 each hold one tape
drive and each needs the other one.
Deadlock
A process is deadlocked if it is waiting for an
event that will never occur.
A process is indefinitely postponed if it is
delayed repeatedly over a long period of time
i.e. the process is ready to proceed but never gets the
CPU.
Necessary Conditions for Deadlock
A deadlock situation can arise if the following four
conditions hold simultaneously in a system,
Mutual exclusion - At least one resource must be
held in a nonsharable mode; that is, only one
process at a time can use the resource.
If another process requests that resource, the
requesting process must be delayed until the
resource has been released.
Necessary Conditions for Deadlock
Hold and wait - A process must be holding at least
one resource and waiting to acquire additional
resources that are currently being held by other
processes.
No preemption - Resources cannot be preempted;
that is, a resource can be released only voluntarily
by the process holding it, after that process has
completed its task.
Necessary Conditions for Deadlock
Circular wait - A set {P0, P1, ..., Pn} of waiting
processes must exist such that P0 is waiting for a
resource held by P1, P1 is waiting for a resource
held by P2, ..., Pn−1 is waiting for a resource held by
Pn, and Pn is waiting for a resource held by P0.
Resource Allocation Graph
Deadlock can be described by a directed graph
It consists- set of vertices V and a set of edges E
V is partitioned into 2 types
P = {P1, P2,…,Pn} - the set of processes in the system
R = {R1, R2,…,Rn} - the set of resource types in the
system
Two kinds of edges
Request edge - Directed edge Pi ---> Rj
Assignment edge - Directed edge Rj ----> Pi
Resource Allocation Graph
Process
Resource type with 4 instances
Pi requests instance of Rj
Pi is holding an instance of Rj
Graph with no cycles
R1 R2
P1 P2 P3
R3 R4
Graph with cycles
R1 P2
P1 P3
R2 P4
Graph with cycles and deadlock
R1 R2
P1 P2 P3
R3 R4
Basic facts
If graph contains no cycles
No deadlock
If graph contains a cycle
if only one instance per resource type, then
deadlock
if several instances per resource type, possibility of
deadlock.
Deadlock Management
Prevention
Design the system in such a way that deadlocks can
never occur
Avoidance
Impose less strict conditions than for prevention,
allowing the possibility of deadlock but sidestepping it
as it occurs.
Detection
Allow possibility of deadlock, determine if deadlock
has occurred and which processes and resources are
involved.
Recovery
After detection, clear the problem, allow processes to
compete and resources to be reused. May involve
destroying and restarting processes.
Deadlock Prevention
If any one of the conditions for deadlock is
denied, deadlock is impossible.
Mutual Exclusion
non-issue for sharable resources
cannot deny this for non-sharable resources
Hold and Wait - guarantee that when a process
requests a resource, it does not hold other resources.
Force each process to acquire all the required resources at
once. Process cannot proceed until all resources have
been acquired.
Low resource utilization, starvation possible
Deadlock Prevention (cont.)
No Preemption
If a process that is holding some resources requests
another resource that cannot be immediately allocated
to it, the process releases the resources currently being
held..
Process will be restarted only when it can regain its old
resources as well as the new ones that it is requesting.
Circular Wait
Impose a total ordering of all resource types.
processes request resources in increasing order of
enumeration
Deadlock Avoidance
Requires that the system has some additional
information available.
Simplest model requires that each process declare the
maximum number of resources of each type that it
may need.
The deadlock-avoidance algorithm dynamically
examines the resource-allocation state to ensure that
there can never be a circular-wait condition.
Resource allocation state is defined by the number of
available and allocated resources, and the maximum
demands of the processes.
Deadlock Detection
Allow system to enter deadlock state
Detection Algorithm
Recovery Scheme
Recovery from Deadlock: Process Termination
Abort all deadlocked processes.
Abort one process at a time until the deadlock cycle is
eliminated.
In which order should we choose to abort?
Priority of the process
How long the process has computed, and how much
longer to completion.
Resources the process has used.
Resources process needs to complete.
How many processes will need to be terminated.
Is process interactive or batch?
Recovery from Deadlock: Resource Preemption
Selecting a victim - minimize cost.
Rollback
return to some safe state, restart process from that
state.
Starvation
same process may always be picked as victim; include
number of rollback in cost factor.