Why do we need ipc?
Although there occur some issues while establishing interprocess communication. We will discuss those issues along with their solution. We will also discuss ways to achieve interprocess communication.
Interprocess communication is communication between two processes that may be on the same system or on a different system. If we talk about the process in the operating system, they are of two types:
An Independent process is one that doesn’t get affected by the other processes running in the system. It doesn’t even affect the processing of any other process in the system. This is because the independent process doesn’t share any data with the other process.
The cooperating process is one that shares data with the other processes in the system. Thus, it can get affected by the other processes of the system. And even it can affect the working of other processes.
So, among these two processes, the cooperating process requires interprocess communication.
The cooperating process shares its data and information with the other processes. The reason behind this interprocess communication is:
Thus, we need IPC to share data and information between the cooperating processes.
The issues related to interprocess communication are as below:
These are the three generic issues that processes face during interprocess communication.
The same sort of problems or issues arises in the case of threads. As the same problems exist for processes and threads. The same solutions are applicable to them.
Well, these problems are resolved by the models discussed in the section below.
There are two methods or two models that operating systems can implement to achieve IPC.
Some operating systems implement both of these models. Let us study these models in more detail.
The operating system implementing the shared memory model is a shared memory system. Here a cooperating process creates a shared memory segment in its address space. Now, if there occurs another process that wants to communicate with this process. Then it must attach itself to the created shared memory segment.
The communicating processes share information by:
Note: Operating system does not allow a process to access the memory of other processes.
But in a shared memory system, the process can access the memory of other processes. If two or more processes are ready to remove this restriction.
The format of the data to be shared and its location are totally under the control of the processes. The communicating processes must not write to the same shared location at the same time.
To understand this, let us discuss the producer-consumer problem. This is the most common paradigm for reflecting the issue of cooperating processes.
The producer-consumer problem state that:
There is a producer process, a consumer process and a fixed-size buffer.
The buffer should be in a shared memory region of producer and consumer processes to resolve the issue. This allows the producer and consumer processes to access the buffer concurrently.
Thus, the producer process can produce the data onto the buffer. And at the same time, the consumer process can consume the data from the buffer. Along with this, both the producer and consumer processes must be synchronized. So that the consumer should not try to consume the data that the producer process has not yet produced.
The message-passing system lets the cooperative processes communicate through a message-passing facility.
The message-passing system is more useful in the distributed environment. As in the distributed environment, the communicating processes may be running on different systems. And these systems are connected via a network.
The message-passing system facilitates communication via at least two operations. That sends messages and receives messages. Although the size of the message can be variable, or it can be of a fixed size.
With the fixed-size messages, the system-level implementation becomes simple. But the programming of such a system becomes difficult.
With the variable size messages, the system-level implementation is difficult. But the programming of such a system becomes simpler.
The message passing can either be:
In synchronous message passing, the sender is blocked until the receiver receives the message. And the receiver is blocked until the message is available.
In asynchronous message passing, the sender sends a message and resumes its operation. Whereas the receiver may receive a valid message or a null.
Thus, the synchronous message passing achieves synchronization between the communicating processes. Which is an essential part of any interprocess communication.
A process can be of two types:
An independent process is not affected by the execution of other processes while a co-operating process can be affected by other executing 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 utilized for increasing computational speed, convenience, and modularity. Inter-process communication (IPC) is a mechanism that 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:
Figure 1 below shows a basic structure of communication between processes via the shared memory method and via the message passing method.
An operating system can implement both methods of communication. First, we will discuss the shared memory methods of communication and then message passing. Communication between processes using shared memory requires processes to share some variable, and it completely depends on how the 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 generates 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 the shared memory method.
i) Shared Memory Method
Ex: Producer-Consumer problem There are two processes: Producer and Consumer. The producer produces some items and the Consumer consumes that item. The two processes share a common space or memory location known as a buffer where the item produced by the 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 the unbounded buffer problem in which the 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 the 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 the producer will start producing items. If the total produced item is equal to the size of the buffer, the 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, the Consumer will wait for the Producer to produce it. If there are items available, Consumer will consume them. The pseudo-code to demonstrate is provided below:Shared Data between the two Processes
Producer Process Code
Consumer Process Code
In the above code, the Producer will start producing again when the (free_index+1) mod buff max will be free because if it is not free, this implies that there are still items that can be consumed by the Consumer so there is no need to produce more. Similarly, if free index and full index point to the same index, this implies that there are no items to consume.
ii) Messaging Passing Method
Now, We will start our discussion of the communication between processes via message passing. In this method, 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:
The message size can be of fixed size or of variable size. If it is of fixed size, it is easy for an OS designer but complicated for a programmer and if it is of variable size then it is easy for a programmer but complicated for the OS designer. A standard message can have two parts: header and body. The header part is used for storing message type, destination id, source id, message length, and control information. The control information contains information like what to do if runs out of buffer space, sequence number, priority. Generally, message is sent using FIFO style.
Message Passing through Communication Link.Direct and Indirect Communication link Now, We will start our discussion about the methods of implementing communication links. While implementing the link, there are some questions that need to be kept in mind like :
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. Direct Communication links are implemented when the processes use a specific process identifier for the communication, but it is hard to identify the sender ahead of time. For example the print server.In-direct Communication is done via a shared mailbox (port), which consists of a queue of messages. The sender keeps the message in mailbox and the receiver picks them up.
Message Passing through Exchanging the Messages.
Synchronous and Asynchronous Message Passing: A process that is blocked is one that is waiting for some event, such as a resource becoming available or the completion of an I/O operation. 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. In both cases, the process may or may not be blocked while sending a message or attempting to receive a message so message passing may be blocking or non-blocking. Blocking is considered synchronous and blocking send means the sender will be blocked until the message is received by receiver. Similarly, blocking receive has the receiver block until a message is available. Non-blocking is considered asynchronous and Non-blocking send has the sender sends the message and continue. Similarly, Non-blocking receive has the receiver receive a valid message or null. After a careful analysis, we can come to a conclusion that for a sender it is more natural to be non-blocking after message passing as there may be a need to send the message to different processes. However, the sender expects acknowledgment from the receiver in case the send fails. Similarly, it is more natural for a receiver to be blocking after issuing the receive as the information from the received message may be used for further execution. At the same time, if the message send keep on failing, the receiver will have to wait indefinitely. That is why we also consider the other possibility of message passing. There are basically three preferred combinations:
In Direct message passing, The process which wants to communicate must explicitly name the recipient or sender of the communication. e.g. send(p1, message) means send the message to p1. Similarly, receive(p2, message) means to receive the message from p2. In this method of communication, the communication link gets 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 links. Symmetry and asymmetry between sending and receiving can also be implemented i.e. either both processes will name each other for sending and receiving the messages or only the sender will name the receiver for sending the message and there is no need for the 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 Indirect message passing, processes use 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 links may be unidirectional or bi-directional. Suppose two processes want to communicate through Indirect message passing, the required operations are: create a mailbox, use this mailbox for sending and receiving messages, then destroy the mailbox. The standard primitives used are: send(A, message) which means send the message to mailbox A. The primitive for the receiving the message also works in the same way e.g. received (A, message). There is a problem with this mailbox implementation. Suppose there are more than two processes sharing the same mailbox and suppose the process p1 sends a message to the mailbox, which process will be the receiver? This can be solved by either enforcing that only two processes can share a single mailbox or enforcing that only one process is allowed to execute the receive at a given time or select any process randomly and notify the sender about the receiver. A mailbox can be made private to a single sender/receiver pair and can also be shared between multiple sender/receiver pairs. Port is an implementation of such mailbox that can have multiple senders and a single receiver. It is used in client/server applications (in this case the server is the receiver). The port is owned by the receiving process and created by OS on the request of the receiver process and can be destroyed either on request of the same receiver processor when the receiver terminates itself. Enforcing that only one process is allowed to execute the receive can be done using the concept of mutual exclusion. Mutex mailbox is created which is shared by n process. The sender is non-blocking and sends the message. The first process which executes the receive will enter in the critical section and all other processes will be blocking and will wait.Now, let’s discuss the Producer-Consumer problem using the message passing concept. The producer places items (inside messages) in the mailbox and the consumer can consume an item when at least one message present in the mailbox. The code is given below:Producer Code
Consumer Code
Examples of IPC systems
Communication in client/server Architecture:There are various mechanism:
The above three methods will be discussed in later articles as all of them are quite conceptual and deserve their own separate articles.References:
Inter-process communication (IPC) is the mechanism through which processes or threads can communicate and exchange data with each other on a computer or across a network. IPC is an important aspect of modern operating systems, as it enables different processes to work together and share resources, leading to increased efficiency and flexibility.
More Reference: http://nptel.ac.in/courses/106108101/pdf/Lecture_Notes/Mod%207_LN.pdf https://www.youtube.com/watch?v=lcRqHwIn5DkThis article is contributed by Durgesh Pandey. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Related Questions
More Questions
- What is www.ides.illinois.gov?
- What is the best gluten free rio rancho?
- Amazon hmd survey?
- what year is hzd set in?
- which chkdsk command is best?
- What is aoda treatment?
- What is a beauty room?
- Is it necessary for Indians to have a passport to travel in India?
- What is aws rds multi az?
- What is the best obgyn el paso?