1
Chapter 5
Process Synchronization and Management
Process Synchronization
2
Processes Synchronization or Synchronization is the way by which processes
that share the same memory space are managed in an operating system. It helps
maintain the consistency of data by using variables or hardware so that only
one process can make changes to the shared memory at a time.
The main objective of process synchronization is to ensure that multiple
processes access shared resources without interfering with each other and to
prevent the possibility of inconsistent data due to concurrent access. To achieve
this, various synchronization techniques such as semaphores, monitors, and
critical sections are used
Process Synchronization
3
On the basis of synchronization, processes are categorized into two types:
1. Independent Process: The execution of one process does not affect the execution of
other processes.
Ex. The process that does not share any shared variable, database, files, etc.
2. Cooperative Process: A process that can affect or be affected by other processes
executing in the system.
Ex. The process that share file, variable, database, etc.
NB: Process synchronization problem arises in the case of Cooperative processes also
because resources are shared in Cooperative processes.
Process Synchronization
4
Inter process communication (IPC) refers to the mechanisms and techniques
used by operating systems to allow different processes to communicate with
each other. There are several methods of IPC, each with its own advantages
and disadvantages. Some common methods of IPC include:
1. Pipes: A pipe is a unidirectional communication channel used for IPC
between two related processes. One process writes to the pipe, and the
other process reads from it.
2. Message Queues: Message queues are used to send and receive messages
between processes. They can be used for both one-to-one and one-to-many
communication.
Process Synchronization
5
3. Shared Memory: Shared memory is a technique where multiple processes
can access the same region of memory. This allows for high-speed
communication between processes.
4. Semaphores: Semaphores are used for controlling access to shared
resources. They are used to prevent multiple processes from accessing the
same resource simultaneously, which can lead to data corruption.
5. Sockets: Sockets are used for network communication between processes
running on different hosts. They provide a standard interface for
communication, which can be used across different platforms and
programming languages.
Process Synchronization (cont’d)
6
• Processes can execute concurrently
-- May be interrupted at any time, partially completing execution
• Concurrent access to shared data may result in data inconsistency
• Maintaining data consistency requires mechanisms to ensure the orderly
execution of cooperating processes
Illustration of the problem:
Suppose that we wanted to provide a solution to the producer – consumer
problem that fills all the buffers. We can do so by having an integer counter
that keeps track of the number of full buffers. Initially, counter is set to 0. It is
incremented by the producer after it produces a new buffer and is
decremented by the consumer after it consumes a buffer.
Process Synchronization (cont’d)
7
-- Race Conditions is a situation that may occur inside a critical section. This
happens when the result of multiple thread execution in critical section differs
according to the order in which the threads execute.
-- This condition can be avoided using the technique called Synchronization or
Process Synchronization, in which we allow only one process to enter and
manipulates the shared data in Critical Section.
8
When more than one processes are executing the same
code or accessing the same memory or any shared variable
in that condition there is a possibility that the output or the
value of the shared variable is wrong so for that all the
processes doing the race to say that my output is correct
this condition known as a race condition.
Several processes access and process the manipulations
over the same data concurrently, then the outcome depends
on the particular order in which the access takes place.
A race condition is a situation that may occur inside a critical
section.
This happens when the result of multiple thread execution in
the critical section differs according to the order in which the
threads execute.
9
Process Synchronization (cont’d)
10
Consider system of n processes (p0 , p1 , … pn-1)
Each process has critical section segment of code
Process may be changing common variables, updating table,
writing file, etc
When one process in critical section, no other may be in its
critical section
Critical section problem is to design protocol to solve this
Critical Section is the part of a program which tries to access shared resources. That
resource may be any resource in a computer like a memory location, Data structure, CPU
or any IO device.
11
12
Process Synchronization (cont’d)
13
Each process must ask
permission to enter critical
section in entry section, may
follow critical section with exit
section, then remainder section
Process Synchronization (cont’d)
14
Entry Section –
It is part of the process which decide the entry of a particular process in the Critical Section,
out of many other processes.
Exit Section –
It checks that a process that after a process has finished execution in Critical Section can be
removed through this Exit Section and allowing the other process that are waiting in the
Entry Section, to enter into the Critical Sections.
Remainder Section –
The other parts of the Code other than Entry Section, Critical Section and Exit Section are
known as Remainder Section.
All other parts of the Code, which is not in Critical, Entry, and Exit Section, are
known as the Remainder Section.
Process Synchronization (cont’d)
15
Critical Section problems must satisfy these three requirements:
1. Mutual Exclusion – It states that no other process is allowed to execute in the
critical section if a process is executing in critical section.
2. Progress – When no process is in the critical section, then any process from
outside that request for execution can enter in the critical section without any
delay. Only those process can enter that have requested and have finite time to
enter the process.
Progress means that if one process doesn't need to execute into
critical section then it should not stop other processes to get into
the critical section.
16
3. Bounded Waiting – An upper bound must exist on the number of times a process enters so 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.
We should be able to predict the waiting time for every process to get into the critical section. The
process must not be endlessly waiting for getting into the critical section.
17
Our solution must provide mutual exclusion. By Mutual
Exclusion, we mean that if one process is executing inside
critical section then the other process must not enter in the
critical section.
Process Synchronization (cont’d)
18
Classical Problems of Synchronization
Below are some of the classical problem depicting flaws of process
synchronization in systems where cooperating processes are present.
• Bounded Buffer (Producer-Consumer) Problem
• Dining Philosophers Problem
• The Readers Writers Problem
Process Synchronization (cont’d)
19
Bounded Buffer Problem
-- This problem is generalized in terms of the Producer Consumer problem,
where a finite buffer pool is used to exchange messages between producer and
consumer processes.
-- Because the buffer pool has a maximum size, this problem is often called the
Bounded buffer problem.
-- Solution to this problem is, creating two counting semaphores (variable or
abstract data type used to control access to a common resource) "full" and
"empty" to keep track of the current number of full and empty buffers
respectively.
20
21
Process Synchronization (cont’d)
22
Dining Philosophers Problem
-- The dining philosopher's problem involves the allocation of limited
resources to a group of processes in a deadlock-free and starvation-free
manner.
-- There are five philosophers sitting around a table, in which there are
five chopsticks/forks kept beside them and a bowl of rice in the centre,
-- When a philosopher wants to eat, he uses two chopsticks - one from
their left and one from their right. When a philosopher wants to think, he
keeps down both chopsticks at their original place.
23
The dining philosopher's problem is the classical problem of synchronization which says that Five
philosophers are sitting around a circular table and their job is to think and eat alternatively. A bowl of
noodles is placed at the center of the table along with five chopsticks for each of the philosophers. To eat a
philosopher needs both their right and a left chopstick.
A philosopher can only eat if both immediate left and right chopsticks of the philosopher is available. In case
if both immediate left and right chopsticks of the philosopher are not available then the philosopher puts
down their (either left or right) chopstick and starts thinking again.
The dining philosopher demonstrates a large class of concurrency control problems hence it's a classic
synchronization problem.
24
25
We use a semaphore to represent a chopstick and this truly acts as a
solution of the Dining Philosophers Problem. Wait and Signal operations
will be used for the solution of the Dining Philosophers Problem, for
picking a chopstick wait operation can be executed while for releasing a
chopstick signal semaphore can be executed.
Process Synchronization (cont’d)
26
The Readers Writers Problem
-- In this problem there are some processes(called readers) that only
read the shared data, and never change it, and there are other
processes(called writers) who may change the data in addition to
reading, or instead of reading it.
-- There are various type of readers-writers problem, most centred on
relative priorities of readers and writers.
27
Consider a situation where we have a file shared between many people.
If one of the people tries editing the file, no other person should be reading or
writing at the same time, otherwise changes will not be visible to him/her.
•However if some person is reading the file, then others may read it at the same
time.
Precisely in OS we call this situation as the readers-writers problem
Problem parameters:
One set of data is shared among a number of processes
Once a writer is ready, it performs its write. Only one writer may write at a time
If a process is writing, no other process can read it
If at least one reader is reading, no other process can write
Readers may not write and only read
28
Process Synchronization (cont’d)
29
Reading Assignment
Hardware and Software Approaches to process synchronization
Tools that are used to solve process synchronization problems