In this article, we have explained Inter-process communication (IPC) in Operating System, why is IPC needed and various ways to achieve IPC like using shared memory, message passing, buffering, pipes and more.
Table of contents:
- Processes and communication
- Why need communication among processes ?
- What is Inter-process communication ?
- IPC in Shared-Memory Systems
- IPC in Message-Passing Systems
Let us get started with Inter-process communication & it's types in Operating System.
Processes and communication
The processes that occur concurrently in the Operating systems can be of 2 types :
- Independent: These do not share any data with any other process.
- Cooperating: These processes share data with other processes. Clearly they can affect and get affected themselves too by other processes.
Why need communication among processes ?
There are several reasons for the same. Some of them are :
Information sharing: There may be a possibility that processes may need the same piece of information at the same time for their execution. For example, Copying and Pasting.
Computation speed: It is very important to have high speed in computation. If a task is subdivided into tasks which are executed parallely, it enhances the speed and throughput of system.
Modularity: Modularity means dividing the functions of operating system into separate processes called Threads, to achieve improved throughput.
What is Inter-process communication ?
Inter-process communication (IPC) helps to achieve the communication among the processes or threads in a system.
It is useful mainly in the environment where the processes reside on different computer systems connected via any type of network. A very simple and self explanatory example is the chat system used in World Wide Web.
There are 2 basic models for IPC:
- Shared Memory
- Message Passing
|Shared Memory||Message Passing|
|A region of memory is shared among the processes.||Messages are exchanged among the processes.|
|Faster than message-passsing systems.||Useful for exchanging smaller amounts of data.|
|Only require to establish shared-memory regions.||Require more time consuming task of kernel intervention.|
1. IPC in Shared-Memory Systems
A process creates the shared-memory region in it's own address space. Other processes communicate by attaching the address space to their own address space.
Processes communicate by Reading and Writing data in the shared area. Operating system does not have any control over data or location. It is solely determined by the processes.
A very famous problem called Producer Consumer Problem is used to illustrate the inner working of Shared-Memory systems. Briefly explaining :
- A producer process produces information that is to be consumed by the consumer.
- By shared memory, both producer and consumer share a memory space called Buffer.
- A producer produces an item at a time and consumer consumes another item at that time.
- Both producer and consumer are synchronized so that consumer does not consume an item that has not yet been produced.
- A simple example to understand the problem is Client-server system. Considering Server as a Producer and Client as a consumer. For example, A Web server produces web content such as HTML files and images which are consumed by the client web browser.
- 2 types of buffers can be used : Bounded buffer(Fixed buffer size) and Unbounded buffer(No limit on buffer size).
The POSIX API uses Shared-memory IPC for communication.
2. IPC in Message-Passing Systems
Message passing provides a mechanism to allow processes to communicate and to synchronize their actions without sharing the same address space.
It is very useful in case where the tasks or processes reside on different computers and are connected by a network.
Messages can be of fixed or variable size. Methods for message passing operations:
1. Direct and Indirect communication
In Direct communication,
Each task explicitly pass the message along with passing name of process to which it is passing.
send(task name, message)
receive(task name, message)
In Indirect communication, Message passing is through mailboxes. The tasks communicating should have a shared mailbox.
send(mailbox name, message)
receive(mailbox name, message)
2. Synchronous and Asynchronous communication
Tasks make calls to each other for communication. Synchronous means blocking and Asynchronous means non-blocking.
There are 4 cases:
->Blocking send: The sending process is blocked until message is received by receiver task.
->Non-Blocking send: The sending process sends the message according to it's requirement without considering whether message is received or not at receiver end.
->Blocking receive: The receiving process is blocked until message is available.
->Non-Blocking receive: The receiving goes on accepting either the message or null information continuously.
When there are both receiving and sending blocked, that case is called Rendezvous.
The messages to be exchanged reside in temporary queue. It can be implemented in 3 ways:
- Zero capacity: Maximum length of queue is zero, so messages can't wait in it. Means sender will be blocked until the receiver receives the message.
- Bounded capacity: Queue will be having finite length, and atmost that number of messages will only reside in it. Sender can send messages until queue is not filled.
- Unbounded capacity: Queue's length is infinite, so any number of messages can wait in it. Sender will never block.
Pipes act as a channel between 2 processes to communicate. These were one of the first IPC mechanisms in early UNIX. Pipes allow a mechanism in which output of one process is input of another process. Pipes function as FIFO.
There are 2 types of pipes:
1. Ordinary pipes: It allows communication in producer-consumer fashion. The producer writes to one end, and consumer reads from the other. It allows only undirectional flow. For 2-way, we will have to use 2 ordinary pipes. On Windows systems, ordinary pipes are termed as Anonymous pipes and the communicating processes have a parent-child relationship. These can be used only for communication between processes on same machine. Once processes finish, ordinary pipes don't exist.
2. Named pipes: These can be bidirectional, require no parent-child relationship and is used for communication for several processes. These pipes continue to exist after communicating processes have finished. Named pipes are referred to as FIFOs in UNIX.
Just as pipes are of two tastes (i.e. named and ordinary), Sockets also do. Similarly these enable channel-based communication for processes on same device. But Network sockets allow IPC for processes on different hosts via networking. A socket consists of an IP address and a port number.
Generally sockets use client-server architecture. The server here waits for client request by listening to a specified port. After receiving the request, server accepts the connection and the connection is completed. The client process has a port assigned to it by host computer.
For example, client on host A with IP address 18.104.22.168 wishes to establish a connection with web server which is listening at port 70 at address 22.214.171.124 .
Semaphores are used to lock/unlock the critical region which is shared among processes. If a single region is shared by multiple processes, there is a possibility of deadlock. Thus semaphore comes into play to manage message passing.
Semaphores are of 2 types: Binary semaphores(having 2 states 0 or 1) and Counting semaphores(different resource counts).
A very basic example for implementation of semaphores is: Consider the case of printing. We are having 5 printers(assuming 1 printer will print 1 at a time). We request to print 3 jobs. These jobs will be given to 3 printers. Again 4 jobs came into request while previous was executing. So now the 2 printers free will schedule the 2 requests and other 2 requests will be executed when one of the printers will be free. This is how counting semaphores are implemented. Semaphores manage the entry and exit of processes in critical section (or the execution).
With this article at OpenGenus, you must have a strong idea of Inter-process communication & it's types in Operating System.