Interprocess communication is a capability of Operating Systems that enables a process to communicate with any other process. IPC enables one application to control another application and share the matching data without interfering with one another.
Lets read further to learn more about interprocess communication, types of processes, different models of IPC, etc.
Interprocess communication (IPC) is an ability to hold by an Operating System that allows one process to communicate with another process. The processes can be running on the same computer or on different computers connected through a network.
Interprocess communication enables one application to control another application, and for numerous applications to share the matching data without interfering with one another. IPC is required in all multiprogramming systems.
Interprocess communication is a mechanism that is used by the processes to exchange data and information. Many processes may be executing at the same time in the Operating System. These processes can be divided into two types known as independent processes and cooperating processes.
Types of Processes
Following are the important types of processes
- Independent Processes
- Cooperating Processes
A process that cannot affect or be affected by any other process executing in the system is called an independent process. An independent process does not share data with any other process.
A process that can affect or be affected by any other process executing in the system is called a cooperating process. A cooperating process shares data with other processes.
Advantages of Process Cooperation
Different advantages of process cooperation are as follows:
Process cooperation is necessary because many users may need to access the same information such as a shared file etc. an environment must be provided where such information can be accessed by multiple users concurrently.
One task can be divided into multiple tasks to execute it faster. All divided subtasks can be executed concurrently to speed up the computation. In this case, these subtasks may be required to cooperate with each other as they are part of the same task.
Process cooperation is very important if the system is to be constructed in the modular style. The modular system divides the functions into separate processes or threads.
Process cooperation provides convenience if a single user is working on multiple tasks at the same time. It ensures that any conflicts are avoided in the system if the user is using the same data in multiple tasks.
Models of Interprocess Communication
There are two fundamental models of interprocess communication:
- Shared Memory Model
- Message Passing Model
Shared Memory Model
The shared memory model establishes a region of memory that is shared by cooperating processes. The processes exchange information by reading and writing data to the shared region. This model is faster than a message-passing model and provides maximum speed and convenience.
Typically, the shared memory region resides in the address space of the process that creates this region. The other processes requiring to communicate using that shared memory process to access the memory of another process.
All processes wishing to communicate using this model must agree to remove this restriction. The processes must also ensure that they do not write to the same location simultaneously.
Below is a very simple example of two cooperating processes. The problem is called the producer-consumer problem and it uses two processes called producer and consumer.
Producer Process: it generates information that will be consumed by the consumer.
Consumer Process: it consumes information created by the producer.
Both processes run parallel. If the consumer has nothing to use, it waits. There are two sides of the producer. In version one, the producer can produce an unlimited amount of items. This is called the Unbounded Buffer Producer-Consumer Problem.
In the other version, there is a predetermined limit to the buffer size. When the buffer is full, the producer must wait until there is some space in the buffer before it can make a new item.
Bounded Buffer-Shared Memory Solution
Here is a shared memory resolution for a bounded buffer producer-consumer problem. Both processes have some collective data that can be accessed and updated. The shared data is as follows:
// shared data:
int n=5, item, in, out;
// number of items in a buffer is at most 5
Int buffer [n];
// both the consumer and producer are currently looking at buffer element 0.
// Producer Process
While (in+1 % n==out)
// Do nothing
//produce a random item
Buffer[in] = nextProduction;
In = in+1 % n;
// Consumer process
While (in == out)
// Do nothing
NextConsumed = buffer [out];
Out = out + 1 % n;
The producer essentially just checks to see if there is any space in which to put a recently produced item (outer while loop). If there is no space, then it waits until there is a little space. The consumer waits while the buffer is empty.
If there is something, it captures the next item and consumes it. One drawback with this solution is that there is one element of the buffer that is wasted.
Message Passing Model
In the message-passing model, the cooperating processes communicate by exchanging messages with one another. This model is useful for exchanging a smaller amount of data. It can also be implemented more easily than a shared memory model.
It is typically implemented using system calls and is more time-consuming. The message-passing model is typically useful in a distributed environment. The processes may reside on different computers connected by a network.
The chat program used on the internet is an example of the message passing system. Users can communicate by exchanging messages with one another. A message-passing model provides at least two operations:
Two processes can communicate with each other by sending and receiving messages. This communication can take place by establishing a communication link between these processes.
Mechanisms For Interprocess Communication
Following are the important mechanisms for interprocess communication:
The processes that want to communicate must refer to each other using any method. It can be done using direct or indirect communication.
In Direct communication, each process that wants to communicate must name the sender or recipient of the communication. There are two schemes of direct communication known as symmetric communication and asymmetric communication.
The symmetric communication requires that both the sender and receiver process must name each other to communicate. In asymmetric communication, only the sender process must name the recipient. The recipient process does not need to name the sender process.
Send () and Receive () functions in symmetric communication are defined as follows:
Send (P, msg) it is used to send the message msg to the process P.
Receive (Q, msg) it is used to receive the message msg by the process Q.
Send () and Receive () functions in asymmetric communication are defined as follows:
Send (P, msg) it is used to send the message msg to the process P.
Receive (id, msg) it is used to receive message msg from any process. The variable id refers to the name of the process with which the communication has taken place.
The properties of the communication link in direct communication are as follows:
- A link is automatically established between the pair of processes that want to communicate. The processes only need to know the identity of each other.
- A link is established between exactly two processes.
- Only one link is established between one pair of processes.
In-indirect communication, the message is sent and received from mailboxes or ports. A mailbox is an object used by the processes to store and remove messages. Each mailbox has a unique identification. Two processes can communicate using this method only if they have a shared mailbox.
A mailbox can be owned by a processor or the Operating System. If the mailbox is owned by a process then it disappears when that process is terminated.
The Operating System must provide a mechanism for the following:
- Create a new mailbox
- Send and receive messages via mailbox
- Delete a mailbox
Send () and Receive () functions in asymmetric communication are as follows:
Send (M, msg) it is used to send the message msg to the mailbox M.
Receive (M, msg) it is used to receive the message msg from the mailbox M.
The properties of a communication link in indirect communication are as follows:
- A link is established between a pair of processes only if both have a shared mailbox.
- A link may be associated with more than two processes.
- Each pair of communicating processes may have a number of links with each link referring to one mailbox.
Problems in Mailbox System
There are some problems with the mailbox system that occur when more than two processes share a mailbox. Suppose that process 1 sends a message to a mailbox that is shared with process 2 and process 3. Process 2 and process 3 both try to receive the message. Who will get it?
Some solutions to the above problem are :
- To allow a link to be established with at most two processes
- Allow only one process at a time to execute a receive operation
- To allow the system to arbitrarily select the receiver. The sender must be notified who received the message.
The communication between different processes takes place through calls to send() and receive () functions. Different design options to implement each function include message passing by blocking or unblocking. These are also known as synchronous or asynchronous.
Blocking Send: the sending process is blocked until the message is received by the receiving process or by the mailbox.
Non-blocking Send: the sending process sends the message and resumes options.
Blocking Receive: the receiver process blocks until a message is available.
Non-Blocking Receiver: the receiver process retrieves either a valid message or a null.
The message exchanged via any type of communication resides in a temporary queue. These queues can be implemented in the following ways:
The queue has a maximum length of zero. The link cannot have any messages waiting in it. The sender must wait until the recipient receives the message. The two processes must be synchronized for the transfer of a message. The zero-capacity link is referred to as a message-passing system without buffering.
The queue has a finite length of n. it can store n number of messages in it. A message is stored in the queue if it is not full and the sender continues the execution without any wait. The sender must block until there is a space available in the queue.
The queue length is infinite and any number of messages can wait in it. The sender is never blocked. That is why the sender is never delayed.