14. Process Synchronization and Communication
14. Process Synchronization and Communication
Synchronization
Message passing may be either blocking or non-blocking
i. Blocking is considered synchronous
o Blocking send has the sender block until the message is received
o Blocking receive has the receiver block until a message is available
ii. Non-blocking is considered asynchronous
o Non-blocking send has the sender send the message and continue
o Non-blocking receive has the receiver receive a valid message or null
Process synchronization refers to the idea that multiple processes are to join up or handshake or
cooperation at a certain point, in order to reach an agreement or commit to a certain sequence of
action. Coordination of simultaneous processes to complete a task is known as process
synchronization.
Buffering
Queue of messages attached to the link; implemented in one of three ways 1. 2. Zero capacity –
0 messages Sender must wait for receiver (rendezvous) Bounded capacity – finite length of n
messages Sender must wait if link full 3. Unbounded capacity – infinite length Sender never waits
Inter process Communication
•A cooperating process is one that can affect or be affected by the other processes executing in
the system.
•Cooperating processes may either directly share a logical address space (that is, both code and
data), or be allowed to share data only through files. The former case is achieved through the use
of lightweight processes or threads. Concurrent access to shared data may result in data
inconsistency.
• In this topic, we discuss various mechanisms to ensure the orderly execution of cooperating
processes that share a logical address space, so that data consistency is maintained.
Cooperating Processes
•The concurrent processes executing in the operating system may be either independent processes
or cooperating processes.
1 Sarah M. W. Maloba
•A process is independent if it cannot affect or be affected by the other processes executing in the
system.
•On the other hand, a process is cooperating if it can affect or be affected by the other processes
executing in the system.
• There are several reasons for providing an environment that allows process cooperation:
2 Sarah M. W. Maloba
Race condition
•When several processes access and manipulate the same data concurrently and the outcome of
the execution depends on the particular order in which the access takes place, is called a race
condition.
A solution to the critical section problem must satisfy the following 3 requirements:
1.mutual exclusion: Only one process can execute their critical section at any time.
2. Progress: When no process is executing a critical section for a data, one of the processes
wishing to enter a critical section for data will be granted entry.
3. Bounded wait: No process should wait for a resource for infinite amount of time.
Critical section: The portion in any program that accesses a shared resource is called as critical
section (or) critical region.
Synchronization hardware
In a uniprocessor multiprogrammed system, mutual exclusion can be obtained by disabling the
interrupts before the process enters its critical section and enabling them after it has exited the
critical section.
Disable
interrupts
Critical section
Enable interrupts
Once a process is in critical section it cannot be interrupted. This solution cannot be used in
multiprocessor environment. since processes run independently on different processors.
In multiprocessor systems, Test and set instruction is provided, it completes execution without
interruption. Each process when entering their critical section must set lock, to prevent other
processes from entering their critical sections simultaneously and must release the lock when
exiting their critical sections.
A process wants to enter critical section and value of lock is false then testandset returns false and
the value of lock becomes true, thus for other processes wanting to enter their critical sections
4 Sarah M. W. Maloba
testandset returns true and the processes do busy waiting until the process exits critical section
and sets the value of lock to false.
Semaphores
A semaphore is an integer variable. Semaphore accesses only through two operations.
1) wait: wait operation decrements the count by1. If the result value is negative, the process
executing the wait operation is blocked.
2) signal operation: Signal operation increments by 1, if the value is not positive then one of the
processes blocked in wait operation unblocked.
5 Sarah M. W. Maloba
that the buffer is empty, it goes to sleep until the producer puts something in the buffer and wakes
it up.
2) The readers-writers problem
A database is to be shared among several concurrent processes. Some processes may want only
to read the database, some may want to update the database. If two readers access the shared data
simultaneously no problem. if a writer, some other process accesses the database simultaneously
problem arises. Writers have exclusive access to the shared database while writing to the database.
This problem is known as readers- writes problem.
First readers-writers problem
No reader should be kept waiting unless a writer has already obtained permission to use the shared
resource.
Second readers-writers problem:
Once a writer is ready, that writer performs its write as soon as possible. A process wishing to
modify the shared data must request the lock in write mode. multiple processes are permitted to
concurrently acquire a reader-writer lock in read mode. A reader writer lock in read mode. but
only one process may acquire the lock for writing as exclusive access is required for writers.
6 Sarah M. W. Maloba
solution guarantees that no two neighbours are eating simultaneously. Suppose all 5 philosophers
become hungry simultaneously and each grabs his left fork, he will be delayed forever.
Several remedies:
1) Allow at most 4 philosophers to be sitting simultaneously at the table.
2) Allow a philosopher to pick up his fork only if both forks are available.
3) An odd philosopher picks up first his left fork and then right fork. an even philosopher picks
up his right fork and then his left fork.
MONITORS
The disadvantage of semaphore is that it is unstructured construct. Wait and signal operations can
be scattered in a program and hence debugging becomes difficult. A monitor is an object that
contains both the data and procedures needed to perform allocation of a shared resource. To
accomplish resource allocation using monitors, a process must call a monitor entry routine. Many
processes may want to enter the monitor at the same time. but only one process at a time is allowed
to enter. Data inside a monitor may be either global to all routines within the monitor (or) local
to a specific routine. Monitor data is accessible only within the monitor. There is no way for
processes outside the monitor to access monitor data. This is a form of information hiding. If a
process calls a monitor entry routine while no other processes are executing inside the monitor,
the process acquires a lock on the monitor and enters it. while a process is in the monitor, other
processes may not enter the monitor to acquire the resource. If a process calls a monitor entry
routine while the other monitor is locked the monitor makes the calling process wait outside the
monitor until the lock on the monitor is released. The process that has the resource will call a
monitor entry routine to release the resource. This routine could free the resource and wait for
another requesting process to arrive monitor entry routine calls signal to allow one of the waiting
processes to enter the monitor and acquire the resource. Monitor gives high priority to waiting
processes than to newly arriving ones.
DEADLOCKS
System model:
A system consists of a finite number of resources to be distributed among a number of competing
processes. The resources are partitioned into several types, each consisting of some number of
7 Sarah M. W. Maloba
identical instances. Memory space, CPU cycles, files, I/O devices are examples of resource types.
If a system has 2 CPUs, then the resource type CPU has 2 instances. A process must request a
resource before using it and must release the resource after using it. A process may request as
many resources as it requires to carry out its task. The number of resources as it requires to carry
out its task. The number of resources requested may not exceed the total number of resources
available in the system. A process cannot request 3 printers if the system has only two.
(I) REQUEST: The process requests the resource. If the request cannot be granted immediately
(if the resource is being used by another process), then the requesting process must wait until it
can acquire the resource.
(II) USE: The process can operate on the resource. if the resource is a printer, the process can
print on the printer.
(III) RELEASE: The process releases the resource.
• A process requests resources; if the resources are not available at that time, the process enters a
wait state. It may happen that waiting processes will never again change state, because the
resources they have requested are held by other waiting processes. This situation is called a
deadlock.
Deadlock occurs when multiple processes are blocked each waiting for a resource that can only
be freed by one of the other blocked processes.
8 Sarah M. W. Maloba
DEADLOCK CHARACTERIZATION:
In a deadlock, processes never finish executing, and system resources are tied up, preventing other
jobs from starting.
NECESSARY CONDITIONS:
A deadlock situation can arise if the following 4 conditions hold simultaneously in a system:
1. MUTUAL EXCLUSION: 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.
2. 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.
3. NO PREEMPTION: Resources cannot be preempted. A resource can be released only
voluntarily by the process holding it, after that process has completed its task.
4. CIRCULAR WAIT: A set {P0,P1,…..Pn} of waiting processes must exist such that P0 is
waiting for 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 byP0.
Methods for Handling Deadlocks
Principally, there are three different methods for dealing with the deadlock problem:
• We can use a protocol to ensure that the system will never enter a deadlock state.
• We can allow the system to enter a deadlock state and then recover.
• Ignore the problem and pretend that deadlocks never occur in the system; used by most
operating systems, Including UNIX.
DEADLOCK PREVENTION
For a deadlock to occur, each of the 4 necessary conditions must held. By ensuring that at least
one of these conditions cannot hold, we can prevent the occurrence of a deadlock.
Mutual Exclusion – not required for sharable resources; must hold for non-sharable resources
Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold
any other resources
o Require process to request and be allocated all its resources before it begins execution, or allow
process to request resources only when the process has none
o Low resource utilization; starvation possible
9 Sarah M. W. Maloba
No Preemption – o If a process that is holding some resources requests another resource that
cannot be immediately allocated to it, then all resources currently being held are released
o Preempted resources are added to the list of resources for which the process is waiting
o 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, and require that each process
requests resources in an increasing order of enumeration
Deadlock Avoidance
Requires that the system has some additional a priori information available
• Simplest and most useful 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
If a system does not employ either a deadlock-prevention or a deadlock avoidance algorithm, then
a deadlock situation may occur. In this environment, the system must provide:
• An algorithm that examines the state of the system to determine whether a deadlock has
Occurred.
• An algorithm to recover from the deadlock
Recovery from Deadlock
When a detection algorithm determines that a deadlock exists, several alternatives exist. One
possibility is to inform the operator that a deadlock has occurred, and to let the operator deal with
the deadlock manually. The other possibility is to let the system recover from the deadlock
automatically. There are two options for breaking a deadlock. One solution is simply to abort one
or more processes to break the circular wait. The second option is to preempt some resources
from one or more of the deadlocked processes.
1. Process Termination: To eliminate deadlocks by aborting a process, we use one of two
methods. In both methods, the system reclaims all resources allocated to the terminated processes.
10 Sarah M. W. Maloba
• Abort all deadlocked processes: This method clearly will break the deadlock cycle, but at a
great expense; these processes may have computed for a long time, and the results of these partial
computations must be discarded and probably recomputed later.
• Abort one process at a time until the deadlock cycle is eliminated: This method incurs
considerable overhead, since after each process is aborted, a deadlock detection algorithm must
be invoked to determine whether any processes are still deadlocked.
2. Resource Preemption: To eliminate deadlocks using resource preemption, we successively
preempt some resources from processes and give these resources to other processes until the
deadlock cycle is broken. If preemption is required to deal with deadlocks, then three issues need
to be addressed.
• Selecting a victim: Which resources and which processes are to be preempted? As in process
termination, we must determine the order of preemption to minimize cost. Cost factors may
include such parameters as the numbers of resources a deadlock process is holding, and the
amount of time a deadlocked process has thus far consumed during its execution.
• Rollback: If we preempt a resource from a process, what should be done with that process?
Clearly, it cannot continue with its normal execution; it is missing some needed resource. We
must rollback the process to some safe state, and restart it from that state.
• Starvation: In a system where victim selection is based primarily on cost factors, it may happen
that the same process is always picked as a victim. As a result, this process never completes its
designated task, a starvation situation that needs to be dealt with in any practical system. Clearly,
we must ensure that a process can be picked as a victim only a small finite number of times. The
most common solution is to include the number of rollbacks in the cost factor.
11 Sarah M. W. Maloba