The term interprocess communication (IPC) describes any method of coordinating the actions of multiple processes, or sending data from one process to another. IPC is commonly used to allow processes to coordinate the use of shared data objects; for instance, to let two programs update the same data in memory without interfering with each other, or to make data acquired by one process available to others. IPC is possible between the processes on same computer as well as on the processes running on different computer i.e. in networked/distributed system.
Types of Inter-process Communications (IPCs)
- Pipes
This allows flow of data in one direction only. Analogous to simplex systems (Keyboard). Data from the output is usually buffered until input process receives it which must have a common origin. - Signals
A means of receiving notice of a software or hardware event, asynchronously. - Semaphores
This is used in solving problems associated with synchronization and to avoid race condition. These are integer values which are greater than or equal to 0. - Message Queue
This allows messages to be passed between processes using either a single queue or several message queue. This is managed by system kernel, messages are co-ordinated using an API. - Shared Memory
This allows interchange of data through a defined area of a memory. Semaphore values has to be obtained before data can get access to a shared memory. A way to create a segment of memory that is mapped into the address space of two or more processes, each of which can access and alter the memory contents.
Shared Memory
Distinct processes (created by system calls) have distinct address spaces, with no writable contents in common. The processes or threads running in different address spaces can share data simply by referring to the contents of the shared segment in memory i.e. segment of memory is part of the address space of more than one process.
Example: Producer-Consumer problem
There are two processes: Producer and Consumer. Producer produces some item and Consumer consumes that item. The two processes shares a common space or memory location known as buffer where the item produced by Producer is stored and from where the Consumer consumes the item if needed. Assume that buffers is bounded buffer i.e. Producer can keep on producing items untill buffer is not full. 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 first check for the availability of the item and if no item is available, Consumer will wait for producer to produce it. If there are items available, consumer will consume it.
Message Passing
If two processes p1 and p2 want to communicate with each other, they proceed as follow:
- Establish a communication link
- Start exchanging messages using basic primitives.
We need at least two primitives:- send(message, destinaion)
- receive(message, host)
Communication can be either
- Direct communication link
Direct Communication links are implemented when the processes use specific process identifier for the communication but it is hard to identify the sender ahead of time. For example: the print server.The process which want to communicate must explicitly name the recipient or sender of communication.
e.g. send(p1, message) means send the message to p1.
similarly, receive(p2, message) means receive the message from p2.In this method of communication, the communication link get established automatically, which can be either unidirectional or bidirectional, but one link can be used between one pair of the sender and receiver and one pair of sender and receiver should not possess more than one pair of link. Symmetry and asymmetry between the sending and receiving can also be implemented i.e. either both process will name each other for sending and receiving the messages or only sender will name receiver for sending the message and there is no need for receiver for naming the sender for receiving the message.The problem with this method of communication is that if the name of one process changes, this method will not work.
- In-directed communication link
In-directed Communication is done via a shred mailbox (port), which consists of queue of messages. Sender keeps the message in mailbox and receiver picks them up.Processes uses mailboxes (also referred to as ports) for sending and receiving messages. Each mailbox has a unique id and processes can communicate only if they share a mailbox. Link established only if processes share a common mailbox and a single link can be associated with many processes. Each pair of processes can share several communication links and these link may be unidirectional or bi-directional. Suppose two process want to communicate though Indirect message passing, the required operations are: create a mail box, use this mail box for sending and receiving messages, destroy the mail box.