Unit 3 (3.3) Inter Process Communication (IPC)
Unit 3 (3.3) Inter Process Communication (IPC)
Independent process is the process that can not affect or be affected by the other
processes. Independent processes does not share any data like temporary or persistent
with any other process.
Co-operating process is affect or be affected by the other processes executing in the
system. Cooperating process shares data with other processes.
Though one can think that those processes, which are running independently, will
execute very efficiently, in reality, there are many situations when co-operative nature
can be utilised for increasing computational speed, convenience and modularity.
Inter process communication (IPC) is a mechanism which 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.
Processes can communicate with each other through both Shared Memory and Message
passing.
There are two fundamental models of interprocess communication :
1. Shared Memory
2. Message passing
An operating system can implement both method of communication. First, we will discuss
the shared memory methods of communication and then message passing method.
Communication between processes using shared memory requires processes to share some
variable and it completely depends on how programmer will implement it.
One way of communication using shared memory can be imagined like this: Suppose
process1 and process2 are executing simultaneously and they share some resources or use
some information from another process.
Process1 generate information about certain computations or resources being used and
keeps it as a record in shared memory.
When process2 needs to use the shared information, it will check in the record stored in
shared memory and take note of the information generated by process1 and act accordingly.
Processes can use shared memory for extracting information as a record from another
process as well as for delivering any specific information to other processes.
Let’s discuss an example of communication between processes using shared memory and
message passing.
The Figure 1 below shows a basic structure of communication between processes via
the shared memory system and via the message passing system.
Message Queue
i) Shared Memory System
There are two processes: Producer and Consumer. Producer produces some item and
Consumer consumes that item.
The two processes share a common space or memory location known as a buffer where
the item produced by Producer is stored and from which the Consumer consumes the
item, if needed.
There are two versions of this problem: the first one is known as unbounded buffer
problem in which Producer can keep on producing items and there is no limit on
the size of the buffer, the second one is known as the bounded buffer problem in
which Producer can produce up to a certain number of items before it starts waiting
for Consumer to consume it.
We will discuss the bounded buffer problem. First, the Producer and the Consumer
will share some common memory, then producer will start producing items.
If the total produced item is equal to the size of buffer, producer will wait to get it
consumed by the Consumer. Similarly, the consumer will first check for the
availability of the item.
If no item is available, Consumer will wait for Producer to produce it. If there are
items available, Consumer will consume it.
This example uses shared memory and a circular queue. Note in the code below
that only the producer changes "in", and only the consumer changes "out", and that
they can never be accessing the same array location at the same time.
• First the following data is set up in the shared memory area:
#define BUFFER_SIZE 10
typedef struct
{
. . .
} item;
item buffer [ BUFFER_SIZE ];
int in = 0;
int out = 0;
Producer process:
• Then the producer process. Note that the buffer is full when "in" is one less than
"out" in a circular sense:
item nextProduced;
while ( true )
{
/* Produce an item and store it in nextProduced */
nextProduced = makeNewItem ( . . . );
• Then the consumer process. Note that the buffer is empty when "in" is equal
to "out“ :
item nextConsumed;
while ( true )
{
/* wait for an item to become available */
while (in == out);
/* Do nothing */
Now, We will start our discussion of the communication between processes via message
passing. In this system, processes communicate with each other without using any kind
of shared memory. If two processes p1 and p2 want to communicate with each other,
they proceed as follows:
• Establish a communication link (if a link already exists, no need to establish it again.)
• Start exchanging messages using basic primitives.
following are two primitives used in message passing:
⁻ send (destination, messages)
⁻ receive (source, messages)
Direct Communication:
With direct communication the sender must know the name of the receiver to which it
wishes to send a message. There is a one-to-one link between every sender-receiver
pair.
• For symmetric communication, the receiver must also know the specific name of
the sender from which it wishes to receive messages.
• For asymmetric communications, this is not necessary.
• Direct Communication links are implemented when the processes uses a specific
process identifier for the communication, but it is hard to identify the sender
ahead of time. (e.g. : a print server).
Messages are directed and received from mailboxes (also referred to as ports).
• Each mailbox has a unique id.
• Processes can communicate only if they share a mailbox.
Properties of communication link:
• Link established only if processes share a common mailbox.
• A link may be associated with many processes.
• Each pair of processes may share several communication links.
• Link may be unidirectional or bi-directional.
Operations:
• create a new mailbox
• send and receive messages through mailbox
• destroy a mailbox
Either the sending or receiving of messages (or neither or both) may be either blocking
or non-blocking.
For the sender: it is more natural not to be blocked after issuing send:
• can send several messages to multiple destinations.
• but sender usually expect acknowledgment of message receipt (in case receiver
fails).
Thus, both the sender and receiver can be blocking or non-blocking. There are
basically three preferred combinations:
1. Blocking send, Blocking receive
2. Non-blocking send, Non-blocking receive
3. Non-blocking send, Blocking receive
Message passing system should give guarantee that message will be correctly
received by receiver. Receiver sends acknowledgement to sender after receiving the
message.
If acknowledgement not received in defined time then sender resend the message. It
also offers authentication service.
Buffering
A link has some capacity that determines the number of messages that can reside in it
temporarily for which every link has a queue associated with it which can be of zero
capacity, bounded capacity, or unbounded capacity.
In zero capacity, the sender waits until the receiver informs the sender that it has
received the message.
In non-zero capacity cases, a process does not know whether a message has been
received or not after the send operation. For this, the sender must communicate with
the receiver explicitly.
Implementation of the link depends on the situation, it can be either a direct
communication link or an in-directed communication link.
Messages are passed via queues, which may have one of three capacity configurations:
Zero capacity - Messages cannot be stored in the queue, so senders must block until
receivers accept the messages.
Bounded capacity- There is a certain pre-determined finite capacity in the queue.
Senders must block if the queue is full, until space becomes available in the queue,
but may be either blocking or non-blocking otherwise.
Unbounded capacity - The queue has a theoretical infinite capacity, so senders are
never forced to block.
Examples of IPC systems:
References:
https://www.geeksforgeeks.org
Operating System Concepts by Galvin et al.
Lecture notes/ppt of Ariel J. Frank, Bar-Ilan
University