UNIT-2-Process Synchronization
UNIT-2-Process Synchronization
Process Synchronization is the coordination of execution of
multiple processes in a multi-process system to ensure that
they access shared resources in a controlled and predictable
manner.
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.
This can lead to the inconsistency of shared data. So the change
made by one process not necessarily reflected when other
processes accessed the same shared data. To avoid this type of
inconsistency of data, the processes need to be synchronized
with each other.
Thursday, June 15, 2023 2
Process Synchronization
In a multi-process system, synchronization is
necessary to ensure data consistency and integrity,
and to avoid the risk of deadlocks and other
synchronization problems.
Process synchronization is an important aspect of
modern operating systems, and it plays a crucial role
in ensuring the correct and efficient functioning of
multi-process systems.
A process can be of two types:
1. Independent process.
2. Co-operating process.
An independent process is not affected by the
execution of other processes while a co-operating
process can be affected by other executing processes.
Inter-process communication (IPC) is a mechanism
that allows processes to communicate with each
other and synchronize their actions. The
communication between these processes can be seen
as a method of co-operation between them.
Thursday, June 15, 2023 7
Inter Process Communication (IPC) Models
There are two fundamental models of interprocess
communication :
1. Shared Memory
2. Message passing
In the shared-memory model, a region of memory that is
shared by cooperating processes is established.
Processes can then exchange information by reading and
writing data to the shared region.
In the message-passing model, communication takes place
by means of messages exchanged between the
cooperating processes. The two communications models
are contrasted in Figure 3.12
Thursday, June 15, 2023 8
Inter Process Communication (IPC)
Interprocess communication using shared memory
requires communicating processes to establish a
region of shared memory.
Typically, a shared-memory region resides in the
address space of the process creating the shared-
memory segment.
Other processes that wish to communicate using this
shared-memory segment must attach it to their
address space.
not required to name the sender.
In this scheme, the send() and receive() primitives are
defined as follows:
• send(P, message)—Send a message to process P.
• receive(id, message)—Receive a message from any
process.
The variable id is set to the name of the process with which
communication has taken place
A process can communicate with another process via a
number of different mailboxes, but two processes can
communicate only if they have a shared mailbox.
Two processes can communicate only if they have a
shared mailbox.
The send() and receive() primitives are defined as
follows:
• send(A, message)—Send a message to mailbox A.
• receive(A, message)—Receive a message from
mailbox A
Basically, such queues can be implemented in three ways:
• Zero capacity: The queue has a maximum length of zero;
thus, the link cannot have any messages waiting in it. In
this case, the sender must block until the recipient receives
the message.
• Bounded capacity: The queue has finite length n; thus, at
most n messages can reside in it. If the queue is not full
when a new message is sent, the message is placed in the
queue and the sender can continue execution without
waiting. The link’s capacity is finite, however. If the link is
full, the sender must block until space is available in the
queue.
Thursday, June 15, 2023 24
Buffering
• Unbounded capacity: The queue’s length is
potentially infinite; thus, any number of messages can
wait in it. The sender never blocks.
The zero-capacity case is sometimes referred to as a
message system with no buffering.
There two famous approach to provide solution for
critical section problem:
1. Peterson’s Solution
2. Dekker’s Solution
Does not require strict alternation.
Peterson’s solution is restricted to two processes that
alternate execution between their CSs and remainder
sections. The processes are numbered P0 and P1.
Peterson’s solution requires two data items to be shared
between the two processes:
int turn;
boolean flag[i];
The variable turn indicates whose turn it is to enter its CS. That is, if
turn == i, then process Pi allowed to execute in its CS.
The flag array is used to indicate if a process is ready to enter its CS.
For example, if flag[i] is true, this value indicates that Pi is ready to
enter its CS.
Thursday, June 15, 2023 32
do
Process Pi do
Process Pj
{ {
Wait(S); Wait(S);
Critical Section Critical Section
Signal (S) Signal (S)
Remainder Section Remainder Section
} }
While (true) While (true)
Wait(S) Signal(S)
{ {
While(S<=0); S=S+1;
S=S-1; }
} June 15, 2023
Thursday, 40
Dining Philosophers Problem
The dining philosophers problem states that there
are five philosophers sitting around a dinning table
Philosopher either can eat or think
Dinning table has five chopsticks and a bowl of rice
in the middle.
When a philosopher wants to eat, he needs both their
right and left chopstick.
When a philosopher wants to think, he keeps down
both chopstick.
A hungry philosopher may only eat if there are both
chopsticks available.