
Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: June 12, 2024
Most modern computer systems use the notion of a process that lets us execute multiple tasks at any time. And, as multiple processes execute at the same time, often they need to communicate with each other for various reasons.
In this tutorial, we’ll discuss the operating system process, its types, inter-process communication (IPC), and various modes.
Before discussing the IPC, let us understand the concept of an operating system process and its related details.
We write computer programs that perform a designated task. A program when in execution is a process. However, a program is more than the program source code. A process is an active entity that contains various additional components other than the program source code. A process contains a process stack to store the temporary data such as function parameters, local variables, etc, a data section to store global variables, a heap memory allocated dynamically at runtime.
A Process Control Block represents a process in the operating system. A process control block contains various information related to a process such as a process state, program counter, CPU register details, memory management information, etc. It acts as a repository that contains all details that belong to the process.
Once a process is in execution, it changes its state based on the stage of execution:
The above diagram demonstrates the state transition diagram of a process.
Processes executing concurrently in a computer system are of two types – independent processes or cooperating processes.
A process is independent if it can not affect or be affected by any other process executing in the computer system. Besides, any process that does not share data with any other process is also an independent process.
A process is cooperating if it can affect or be affected by other processes executing in the computer system. Thus, any process that shares data with other processes is a cooperating process.
The cooperating processes need to communicate with each other to exchange data and information. Inter-process communication is the mechanism of communicating between processes.
Let us now discuss several reasons for which a process needs to communicate or share data with other processes. Following are few reasons:
There are two modes through which processes can communicate with each other – shared memory and message passing. As the name suggests, the shared memory region shares a shared memory between the processes. On the other hand, the message passing lets processes exchange information through messages. Let’s explore these in detail in the subsequent sections.
Interprocess communication through the shared memory model requires communicating processes to establish a shared memory region. In general, the process that wants to communicate creates the shared memory region in its own address space. Other processes that wish to communicate to this process need to attach their address space to this shared memory segment:
The above figure demonstrates the shared memory model of IPC. Process A and process B establishes a shared memory segment and exchange information through the shared memory region.
By default, the operating system prevents processes from accessing other process memory. The shared memory model requires processes to agree to remove this restriction. Besides, as shared memory is established based on the agreement between processes, the processes are also responsible to ensure synchronization so that both processes are not writing to the same location at the same time.
Although the shared memory model is useful for process communication, it is not always suitable and achievable. For instance, in a distributed computing environment, the processes exchanging data might reside in different computer systems. Thus, it is not straightforward to establish a shared memory region for communication.
The message passing mechanism provides an alternative means processes for communication. In this mode, processes interact with each other through messages with assistance from the underlying operating system:
In the above diagram two processes A, and B are communicating with each other through message passing. Process A sends a message M to the operating system (kernel). This message is then read by process B.
In order to successfully exchange messages, there needs to be a communication link between the processes. There are several techniques through which these communication links are established. Following are a bride overview of these mechanisms:
One classical example of IPC is the producer-consumer problem. A producer is an application/process that produces data. A consumer is an application/process that consumes the produced data. The producer and consumer processes can decide any of the mechanisms discussed before to communicate with each other.
In this article, we’ve discussed computer system processes and various mechanisms of communications between them.
We introduced the notion of process, PCB, and process states. We then discussed two major techniques of process communications – shared memory and message passing.
Finally, we provided the example of producer-consumer that uses the concept of IPC.