Inter Process Communication

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Inter Process

Communication

By

Dr. M. Bilal Qureshi

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).

Example# 1. two processes, P1 and P2, share the global variable a.


At some point in its execution, P1 updates a to the value 1, and at
some point in its execution, P2 updates a to the value 2.Thus, the
two tasks are in a race to write variable a. In this example the
“loser” of the race (the process that updates last) determines the final
value of a.

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:

• Serializing executions of critical regions is more difficult than


one executing processes.
• When a process executes a code that manipulates shared data (or
resource), the process is said to be in critical region (for that
shared data).

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.

• Each process must first request permission to enter critical region.

• Conditions to provide mutual exclusion/or to avoid race condition are,


1. No two processes simultaneously in critical region
2. No process running outside its critical region may block another process
3. No process must wait forever to enter its critical region
11
Critical Region/Section (2)

Mutual exclusion using critical region/section


12
Mutual Exclusion with Busy Waiting (1)
Busy waiting, or spin waiting, refers to a technique in
which a process can do nothing until it gets permission
to enter its critical section but continues to execute an
instruction or set of instructions that tests the appropriate
variable to gain entrance.

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

Proposals for achieving mutual exclusion:


• Disabling interrupts
• Lock variables
• Strict alternation
• The TSL instruction
Strict Alteration

Proposed strict alteration solution to critical region problem


(a) Process 0. (b) Process 1.

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.

Entering and leaving a critical region using the TSL instruction.


Sleep and Wakeup
• In Busy Waiting, when a process wants to enter its critical
region, it checks to see if the entry is allowed If it is not, the
process just sits in a tight loop waiting until it allowed to
enter in critical region
This approach waste CPU time
Another way to block IPC primitive use pair of sleep and
wakeup
Sleep is a system call that causes the caller to block
until another process wakes it up
The wakeup call has one parameter, the process to
be awakened.
Producer-Consumer Problem
• Producer process produces information that is consumed by a consumer
process
• Producer, puts information into the buffer, consumer, takes it out.
• Problem arise when:
• Producer wants to put a new item in the buffer, but it is already full
• Consumer wants to remove an item from the buffer and sees that the
buffer is empty
• Solution:
• Producer go to sleep, to be awakened when the consumer has
removed one or more items
• Consumer goes to sleep until the producer puts something in the
buffer and wakes it up.

You might also like