Inter Process Communication
Inter Process Communication
Inter Process Communication
Communication
By
1
Inter-process Communication (IPC)
Processes frequently need to communicate with other:
• For example, in a shell pipeline, the output of the
first process must be passed to the second
process, and so on down the line.
There are three issues here
1. How one process can pass information to another
2. Making sure two or more processes do not get in
each other's way
3. Proper sequencing when dependencies are
present:
• If process A produces data and process B prints
them, B has to wait until A has produced some data
before starting to print.
Interprocess Communication
(IPC)
• provides a mechanism to allow processes to communicate and
to synchronize their actions without sharing the same address
space.
• Message passing technique is used.
Purpose is to allow processes to communicate without the
need to resort to shared data.
Communication link must exist between processes to send
& receive messages through.
3
Interprocess Communication
(IPC)
• Communication may be
Direct or indirect
Symmetric or asymmetric
• Direct communication
Each process must explicitly name the recipient or sender.
e.g., Send(P, msg) [send a message msg to process P]
Receive(Q, msg) [receive a message msg from process Q]
The processes need to know each other’s identity to
communicate.
This is symmetric addressing scheme.
4
Interprocess Communication
(IPC)
In asymmetric scheme, recipient is not required to name the
sender.
e.g., Send(P, msg) [send message msg to process P]
Receive(id, msg) [receive message msg from any process]
• Indirect communication
Messages can be sent and received from mailboxes.
Two processes can communicate only if they share a mailbox.
e.g., Send(A, msg) [send msg to mailbox A]
Receive(A, msg) [receive msg from mailbox A]
5
Synchronization
• Concurrent processes need access to shared data and shared
resources.
• Without any proper control mechanism, possibility is there to
obtain inconsistent state of this data.
• Maintaining data consistency requires mechanism to ensure the
orderly execution of operating processes. So, process
synchronization methods are used.
• Synchronization is a facility that enforces mutual exclusion and
event ordering. A common synchronization mechanism used in
multiprocessor operating systems is locks
• Communication b/w processes takes place by calls to send and
receive.
6
Synchronization
Race Condition:
• when multiple processes or threads read and write data items so
that the final result depends on the order of execution of
instructions in the multiple processes (i.e., who runs precisely).
7
Synchronization/Race Condition
• Example# 2. two process, P3 and P4, that share global variables b and c, with
initial values b = 1 and c = 2. At some point in its execution, P3 executes the
assignment b = b + c, and at some point in its execution, P4 executes the
assignment c = b + c. Note that the two processes update different variables.
However, the final values of the two variables depend on the order in which
the two processes execute these two assignments. If P3 executes its assignment
statement first, then the final values are b = 3 and c = 5. If P4 executes its
assignment statement first, then the final values are b = 4 and c = 3.
8
Synchronization
• Message passing may be synchronous or asynchronous (also
called as blocking or non-blocking respectively)
Blocking send
The sending process is blocked until the receiving
process (in case of direct communication) or the mailbox
(in case of indirect communication) receives the msg.
Non-blocking send
The sending process sends the msg and resumes
operation.
Blocking receive
The receiver blocks until a msg is available.
Non-blocking receive
The receiver receives either a valid msg or null.
9
Critical Region/Section (1)
Critical Region/Section:
• The region/section of code in executing process in which a
process may updates shared data (file, variable, database, etc).
• The critical region is a piece of code in executing process.
• That part of the program where the shared memory is accessed
Critical Region/Section Problem:
Ref: www.vu.edu.pk
10
How to avoid race condition?
• The execution of critical regions must be mutually exclusive (i.e., at any given time,
only one process is allowed to execute in critical region).
• Mutual exclusion is a mechanism to ensure that only one process (or person) is
doing certain things at one time, thus avoid data inconsistency. All others should be
prevented from modifying shared data until the current process finishes.
(OR) mutual exclusion means that if process P1 is executing in its critical section,
then no other process can be executing in their critical section.
13
Mutual Exclusion with Busy Waiting (2)
One process is busy updating shared memory in its
critical region, no other process will enter its critical
region and cause trouble
15
The TSL (Test & Set Lock) Instruction
• A process call enter_region , if lock is 0, process set it to
1 using the TSL instruction and then read or write the
shared memory.
• When it is done, the process call leave_region sets lock
back to 0 using an ordinary move instruction.