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.
2. Process Overview
Before discussing the IPC, let us understand the concept of an operating system process and its related details.
2.1. What is a Process?
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.
2.2. Process Control Block
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.
2.3. Process State
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.
- New: Initially, when the process is created it is in a new state
- Ready: Process moves to the ready state once it is ready to execute in a processor
- Running: Once the process is in execution in a processor, it is in a running state. Only one process can be in running status in a processor at any time. A process moves to the ready state if there is an interrupt
- Waiting: While executing if the process needs to perform some additional event such as input/output access, it is moved to waiting for the state. Once the event completes, the process moves to the ready state
- Terminated: Once a process completes execution, it’s terminated
3. Types of Processes
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.
4. Inter-Process Communication (IPC)
4.1. What is IPC?
The cooperating processes need to communicate with each other to exchange data and information. Inter-process communication is the mechanism of communicating between processes.
4.2. Need for IPC
Let us now discuss several reasons for which a process needs to communicate or share data with other processes. Following are few reasons:
- Information Sharing: Several users may need to access the same piece of information (e.g., a shared file). Thus, there needs to be an environment for concurrent access to the shared information
- Computation Speedup: Often a task is split into several sub-tasks to speed up its execution. This also requires related processes to exchange information related to the task
- Modularity: Most of the time applications are built in a modular fashion and divided into separate processes. For instance, the Google Chrome web browser spawns a separate process for each new tab
5. Modes of Inter-Process Communication
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.
5.1. Shared Memory
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.
5.2. Message Passing
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:
- Direct Communication: In the mode, each process explicitly specifies the recipient or the sender. For instance, if process A needs to send a message to process B, it can use the primitive
- Indirect Communication: In this mode, processes can exchange messages through a mailbox. A mailbox is a container that holds the messages. For instance, if X is a mailbox, then process A can send a message to mailbox using the primitive
- Synchronization: This is an extension to direct and indirect communication with an additional option of synchronization. Based on the need. a process can choose to block while sending or receiving messages. Besides, it can also asynchronously communicate without any blocking
- Buffering: The exchanged messages reside in a temporary queue. These queues can be of zero, bounded, and unbounded capacity
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.