A process can be of two type:

  • Independent process.
  • Co-operating process.

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 but in practical, there are many situations when co-operative nature can be utilised for increasing computational speed, convenience and modularity. Inter process communication (IPC) is a mechanism which allows processes to communicate 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 using these two ways:

  1. Shared Memory
  2. Message passing
[ad type=”banner”]

The Figure 1 below shows a basic structure of communication between processes via shared memory method and via message passing.

An operating system can implement both method of communication. First, we will discuss the shared memory method of communication and then message passing. Communication between processes using shared memory requires processes to share some variable and it completely depends on how 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 other process, process1 generate information about certain computations or resources being used and keeps it as a record in shared memory. When process2 need 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 other process as well as for delivering any specific information to other process.
Let’s discuss an example of communication between processes using shared memory method.

Inter Process Communication

i) Shared Memory Method

[ad type=”banner”]

Ex: Producer-Consumer problem
There are two processes: Producer and Consumer. Producer produces some item and Consumer consumes that item. The two processes shares a common space or memory location known as buffer where the item produced by Producer is stored and from where the Consumer consumes the item if needed. There are two version of this problem: first one is known as unbounded buffer problem in which Producer can keep on producing items and there is no limit on size of buffer, the second one is known as bounded buffer problem in which producer can produce up to a certain amount of item and after that 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 producer will start producing items. If the total produced item is equal to the size of buffer, producer will wait to get it consumed by the Consumer. Sim-
ilarly, the consumer first check for the availability of the item and if no item is available, Consumer will wait for producer to produce it. If there are items available, consumer will consume it. The pseudo code are given below:

Shared Data between the two Processes,

[pastacode lang=”c” manual=”%23define%20buff_max%2025%0A%23define%20mod%20%25%0A%20%0A%20%20%20%20struct%20item%7B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20diffrent%20member%20of%20the%20produced%20data%20%0A%20%20%20%20%20%20%20%20%2F%2F%20or%20consumed%20data%20%20%20%20%0A%20%20%20%20%20%20%20%20———%0A%20%20%20%20%7D%0A%20%20%20%20%20%0A%20%20%20%20%2F%2F%20An%20array%20is%20needed%20for%20holding%20the%20items.%20%0A%20%20%20%20%2F%2F%20This%20is%20the%20shared%20place%20which%20will%20be%20%20%0A%20%20%20%20%2F%2F%20access%20by%20both%20process%20%20%20%0A%20%20%20%20%2F%2F%20item%20shared_buff%20%5B%20buff_max%20%5D%3B%0A%20%20%20%20%20%20%0A%20%20%20%20%2F%2F%20Two%20variables%20which%20will%20keep%20track%20of%20%0A%20%20%20%20%2F%2F%20the%20indexes%20of%20the%20items%20produced%20by%20producer%20%0A%20%20%20%20%2F%2F%20and%20consumer%20The%20free%20index%20points%20to%20%0A%20%20%20%20%2F%2F%20the%20next%20free%20index.%20The%20full%20index%20points%20to%20%0A%20%20%20%20%2F%2F%20the%20first%20full%20index.%20%0A%20%20%20%20int%20free_index%20%3D%200%3B%0A%20%20%20%20int%20full_index%20%3D%200%3B%0A%20%20″ message=”c” highlight=”” provider=”manual”/]