Table of contents
1.
Introduction
2.
Inter-process communication through the shared memory method
3.
Inter-process communication through message passing method
4.
Direct and indirect communication 
5.
Synchronous and asynchronous communication
6.
Buffering
7.
Frequently Asked Questions
7.1.
What is inter-process communication?
7.2.
Name the two popular mechanisms of IPC.
7.3.
What are the two basic primitives used in the message passing method?
7.4.
Where is the message passing method mostly used?
7.5.
What is the difference between a bounded and unbounded buffer?
7.6.
Give an example of direct communication.
8.
Conclusion
Last Updated: Mar 27, 2024

Inter-Process Communication

Author ANKIT KUMAR
0 upvote
Operating Systems

Introduction

Inter-process communication is a mechanism by which processes communicate with each other. There are several reasons for the processes to communicate with each other. Consider a situation where there are multiple processes involved in a task where one process needs to share information with another. In that case, inter-process communication is inevitable.

There are basically two types of processes:

  • Independent process: Such a process neither affects the working of any other process nor is affected by any other process.
  • Cooperating process: Such a process can affect as well as get affected by any other process. They are involved in inter-process communication.


Need for inter-process communication:

  • Sharing of information: Inter-process communication helps in sharing information when several processes try to access a particular file concurrently. It is, therefore, necessary for the processes to cooperate with each other so that the information provided is correct.
  • Speed: Dividing a big task into smaller tasks and then running them concurrently can speed up the entire functioning of the system. We need inter-process communication in such a case. The multiprocessor environment is a very good example of this.
  • Modularity: When a system is divided into modules, such modules may require information sharing. 
  • Convenience: Allowing multiple processes to run simultaneously provides a better experience to any user. 


There are two ways of inter-process communication(IPC) are :

  1. Shared memory
  2. Message Passing

Also see, Introduction to Process Synchronisation, Multiprogramming vs Multitasking

Inter-process communication through the shared memory method

In the shared memory method, two processes communicate with each other using common shared memory. We shall discuss this method using the below diagram.

Illustration Image IPC

The above diagram shows the pictorial representation of the physical memory. We know that the kernel resides in the memory. There are two processes- P1 and P2 which are present in the memory. Apart from that, we also have a shared memory region that can be accessed by both process P1 and process P2. The shared memory resides in the address space of the process involved in the communication. 

Working: Whenever process P1 wants to share some information with process P2, it will simply write that information in the shared memory region. Process P2 will then read the information from the same shared memory region. Likewise, when P2 wants to share some information with process P1, it can also write the information in the shared memory region, which can be read by process P1.

A very popular example of the shared memory concept is the producer-consumer problem. A producer produces goods that are consumed by a consumer. Let us consider that there are two types of processes- producer and consumer. The producer process produces or creates information that is supposed to be consumed by the consumer process. But how can both producer and consumer process do their work concurrently? The solution to this problem is that we must have a "buffer" of items such that it is filled by the producer and emptied or consumed by the consumer. The buffer here is nothing but the shared memory that we discussed earlier.

It is very important that both the consumer and producer are synchronized. If it is not so, the consumer may try to consume something from the buffer while the buffer is empty because the producer has not produced anything.

There is two kinds of buffers:

  • Unbounded buffer: In this type of buffer, there is no limit to the size of the buffer. The producer can continue producing new items or goods without worrying about the consumer.
  • Bounded buffer: In this type of buffer, the size of the buffer is limited. Since the size is limited, the consumer must wait before it tries to consume anything if the buffer is empty. Similarly, the producer must also wait for the consumer to consume something from the buffer in case the buffer is full before producing any new goods or items.

Inter-process communication through message passing method

The message passing method provides a mechanism to allow processes to communicate and to synchronize their actions without sharing the same memory space. This type of method is useful in distributed systems, where the communicating processes reside on different computers connected by a network.

The message passing method makes use of two basic operations:

  1. send(message): This operation is used by the sending process that wants to send a message to some other process.
  2. Receive (message): This operation is used by the receiving process that wants to receive a message from another process.


The message that is shared between the processes may be of fixed or variable size. In the fixed-size message, every process will send or receive the message of the fixed size, which is decided by the programmer or designer beforehand, while in variable-size messages, there is no limit to the size of the message.

When two processes P1 and P2 wish to communicate with each other, there must be some facility such that both can send messages to each other and receive the message from each other. This facility is logically implemented using a communication link. A communication link can be simply thought of as a medium via which the processes share information.

There are three ways of logically implementing the communication link:

  1. Direct or indirect communication.
  2. Synchronous or asynchronous communication.
  3. Automatic or explicit buffering.

Direct and indirect communication 

In the direct communication method, each process involved in the communication should explicitly name the recipient or sender of the communication.

The two operations in direct communication are:

  1. Send (P1, message): This operation is used by a process to send the message to process P1. Here the process that wants to send any message to some other process should explicitly name the processor should provide any other way to identify the process.
  2. Receive (P2, message): This operation is used by a process to receive the message from process P2. The process that wants to receive any message from P2 has to explicitly name it.


The communication link in the direct communication methods has the following features:

  • The processes involved in the communication process should explicitly name each other for identification.
  • The link is associated with exactly two processes.
  • For every pair of processes, there exists exactly one communication link.


In the indirect communication method, there is a “mediator” that helps the processes to communicate with each other. This mediator is known as a mailbox or port. Instead of directly sending the messages to each other, their communication is done via the mailbox.

For two processes P1 and P2, to communicate, they must have a shared mailbox that can be owned by any of the processes. Whenever process P1 wants to send any message to process P2, it will first send the message to the mailbox. The process P2 will then read that message from the mailbox. A mailbox can be thought of as an object into which messages can be placed by processes and from which messages can be removed.

Suppose the mailbox is identified by A. The send and receive operations work as follow:

  • Send (A, message): used to send the message to the mailbox A.
  • Receive (A, message): used to receive the message from the mailbox A.


There can be multiple mailboxes for various processes involved in the communication. Each mailbox has its own identification and is unique.

The communication link in the indirect communication methods has the following features:

  • Link is established between a pair of processes only if they have a shared mailbox through which they can share the messages.
  • Unlike direct communication, here, there may be multiple processes associated with a link. For example, process P1 writes any message in the mailbox. The message then can be read by various other processes like P2, P3, P4.
  • Each link, however, corresponds to one mailbox.

Synchronous and asynchronous communication

The message sharing between two processes can be synchronous or asynchronous. Synchronous is also known as blocking, and asynchronous is also known as non-blocking. When a process sends a message, the send primitive can be blocking or non-blocking. Similarly, when a process receives a message, the receive primitive can be blocking or non-blocking. We shall now discuss each of these.

  • Blocking send: The sending primitive is blocking in nature. The process that is sending the message is blocked until the message is received by the receiving process.
  • Non-blocking send: The sending process is not blocked after sending the message. The sending process can continue its other operations, which may be sending some other messages.
  • Blocking receive: The receiving process is blocked until it receives the message from the sending process. A receiving process implements receive primitives. In case of blocking receive, it will be blocked until it gets the message that it has sought through receive primitive.
  • Non-blocking receive: The receiving process will not be blocked. It is either going to receive the message from the sending processor will receive a null in case of any failure.

Buffering

Whenever the processes communicate, either directly or indirectly, the messages that are to be exchanged are stored in a temporary queue known as buffers. Various types of such queues or buffers are:

  • Zero capacity: The maximum length or size of the queue is zero. In this kind of buffer, the messages cannot be stored. It works as a gateway for the sending process to send the message to the receiving process. Here the send primitive is blocking in nature because the sender process must be blocked unless and until the receiving process receives the message. If the sending process is not blocked, it may continue to send messages to the process which is yet to receive the first message. But the problem is that the buffer size is zero, so the second message that has been sent cannot be stored. Only one message can be sent at a time.
  • Bounded capacity: Unlike the zero capacity buffer, the bounded capacity buffer has a finite size. The sender process can keep sending the messages into the buffer until it gets full. So, the sender process here need not be blocked if the buffer is not full. Since the bounded capacity buffer has a finite size, at some point of time it may get full. In such a case, the sender must be blocked until the messages are retrieved from the buffer to free some space.
  • Unbounded buffer: In such types of buffers, the size is infinite. Here the sender can send any amount of messages without getting blocked because there is no memory constraint.

Frequently Asked Questions

What is inter-process communication?

Inter-process communication is a mechanism by which processes communicate with each other.

Name the two popular mechanisms of IPC.

Shared memory, message passing.

What are the two basic primitives used in the message passing method?

send() and receive()

Where is the message passing method mostly used?

Distributed environment. 

What is the difference between a bounded and unbounded buffer?

The size of the bounded buffer is finite; hence there is a restriction on the number of messages it can store at any given time, however in an unbounded buffer the size is infinite, and there is no restriction on the number of messages it can store.

Give an example of direct communication.

Print server.

Conclusion

  • Inter-process communication is a mechanism by which processes communicate with each other.
  • Two ways of inter-process communication are the shared memory method and the message passing method.
  • In the shared memory method, two processes communicate with each other using common shared memory. E.g.: Producer-consumer problem.
  • The message passing method provides a mechanism to allow processes to communicate and to synchronize their actions without sharing the same memory space. This type of method is useful in distributed systems.
  • Two basic operations in the message passing method send () and receive().
  •  A communication link can be simply thought of as a medium via which the processes share information.
  • Synchronous is also known as blocking, and asynchronous is also known as non-blocking.
  • Whenever the processes communicate, either directly or indirectly, the messages that are to be exchanged are stored in a temporary queue known as buffers.


Recommended Readings: 


Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Happy learning!

Live masterclass