0% found this document useful (0 votes)
536 views

Unit 3 (3.3) Inter Process Communication (IPC)

Inter-process communication (IPC) allows processes to communicate and synchronize their actions. There are two fundamental models of IPC: shared memory and message passing. Shared memory communication involves processes sharing access to the same memory addresses to read and write data. Message passing involves processes exchanging discrete messages without sharing memory. Message passing can be direct, using specific process identifiers, or indirect using shared mailboxes. Processes communicate by sending messages to these mailboxes.

Uploaded by

Tushar Hedau
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
536 views

Unit 3 (3.3) Inter Process Communication (IPC)

Inter-process communication (IPC) allows processes to communicate and synchronize their actions. There are two fundamental models of IPC: shared memory and message passing. Shared memory communication involves processes sharing access to the same memory addresses to read and write data. Message passing involves processes exchanging discrete messages without sharing memory. Message passing can be direct, using specific process identifiers, or indirect using shared mailboxes. Processes communicate by sending messages to these mailboxes.

Uploaded by

Tushar Hedau
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Operating System

Topic : Inter-process Communication (IPC)

Presented by : Tushar Hedau


Inter Process Communication (IPC)
A process can be of two types:
1. Independent process.
2. Co-operating process.

 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

 In general the memory to be shared in a shared-memory system is initially within the


address space of a particular process, which needs to make system calls in order to make
that memory publicly available to one or more other processes.
 Other processes which wish to use the shared memory must then make their own system
calls to attach the shared memory area onto their address space.
 Generally a few messages must be passed back and forth between the cooperating
processes first in order to set up and coordinate the shared memory access.

Producer-Consumer problem example using shared memory :

 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 ( . . . );

/* Wait for space to become available */


while ((( in + 1 ) % BUFFER_SIZE ) == out );
/* Do nothing */

/* And then store the item and repeat the loop. */


buffer [ in ] = nextProduced;
in = ( in + 1 ) % BUFFER_SIZE;
}
Consumer process:

• 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 */

/* Get the next available item */


nextConsumed = buffer[ out ];
out = ( out + 1 ) % BUFFER_SIZE;

/* Consume the item in nextConsumed


( Do something with it )*/
}
ii) Message Passing System

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

Fig 2 : Basic Message-passing Primitives


 Message size is fixed or variable.
 Message passing systems must support at a minimum system calls for "send message"
and "receive message".
 There are three key issues to be resolved in message passing systems as further
explored in the next three subsections:
• Direct or indirect communication
• Synchronous or Asynchronous communication
• Automatic or explicit buffering.
Message Passing through Direct/Indirect Communication

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

 Properties of communication link:


• Links are established automatically.
• A link is associated with exactly one pair of communicating processes.
• Between each pair there exists exactly one link.
• The link may be unidirectional, but is usually bi-directional.
Indirect communication:

 Indirect communication uses shared mailboxes, or ports.


• Multiple processes can share the same mailbox or boxes.
• Only one process can read any given message in a mailbox. Initially the process
that creates the mailbox is the owner, and is the only one allowed to read mail in
the mailbox, although this privilege may be transferred.
 ( Of course the process that reads the message can immediately turn around
and place an identical message back in the box for someone else to read, but
that may put it at the back end of a queue of messages. )
• The OS must provide system calls to create and delete mailboxes, and to send
and receive messages to/from mailboxes.
• messages are sent to a shared mailbox which consists of a queue of messages.
• senders place messages in the mailbox, receivers pick them up.

 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

 Primitives are defined as:


• send (A, message) – send a message to mailbox A.
• receive (A, message) – receive a message from mailbox A.
 Mailbox sharing:
• P1, P2, and P3 share mailbox A.
• P1, sends; P2 and P3 receive.
 Possible solutions:
• Allow a link to be associated with at most two processes.
• Allow only one process at a time to execute a receive operation.
• Allow the system to select arbitrarily the receiver. Sender is notified who the
receiver was.
Message passing through exchanging the messages

Synchronous and Asynchronous Message Passing:

 Either the sending or receiving of messages (or neither or both) may be either blocking
or non-blocking.

 Blocking is considered synchronous


• Blocking send has the sender block until the message is received.
• Blocking receive has the receiver block until a message is available.

 Non-blocking is considered asynchronous


• Non-blocking send has the sender send the message and continue.
• Non-blocking receive has the receiver receive a valid message or null.

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

 For the receiver: it is more natural to be blocked after issuing receive:


• the receiver usually needs the information before proceeding.
• but could be blocked indefinitely if sender process fails before send.
 Example: blocking send, blocking receive:
• both are blocked until the message is received.
• occurs when the communication link is un-buffered (no message queue).
• provides tight synchronization.

 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:

 Posix : uses shared memory method.


 Mach : uses message passing.
 Windows XP : uses message passing using local procedural
calls.

References:
https://www.geeksforgeeks.org
Operating System Concepts by Galvin et al.
Lecture notes/ppt of Ariel J. Frank, Bar-Ilan
University

You might also like