Inter-Process Communication | Operating System - Computer Science Engineering (CSE) PDF Download

Inter Process Communication

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

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 | Operating System - Computer Science Engineering (CSE)

i) Shared Memory Method

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

#define buff_max 25
 #define mod %
  
     struct item{
  
         // diffrent member of the produced data 
         // or consumed data    
         ---------
     }
      
     // An array is needed for holding the items. 
     // This is the shared place which will be  
     // access by both process   
     // item shared_buff [ buff_max ];
       
     // Two variables which will keep track of 
     // the indexes of the items produced by producer 
     // and consumer The free index points to 
     // the next free index. The full index points to 
     // the first full index. 
     int free_index = 0;
     int full_index = 0;

Producer Process Code

item nextProduced;
     
    while(1){
         
        // check if there is no space 
        // for production.
        // if so keep waiting.
        while((free_index+1) mod buff_max == full_index);
         
        shared_buff[free_index] = nextProduced;
        free_index = (free_index + 1) mod buff_max;
    }

Consumer Process Code

item nextConsumed;
      
     while(1){
          
         // check if there is an available 
         // item  for consumption. 
         // if not keep on waiting for 
         // get them produced.
         while((free_index == full_index);
          
         nextConsumed = shared_buff[full_index];
         full_index = (full_index + 1) mod buff_max;
     }

In the above code, The producer will start producing again when the (free_index+1) mod buff max will be free because if it it 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 points to the same index, this implies that there are no item to consume.

ii) Messaging Passing Method

Now, We will start our discussion for the communication between processes via message passing. In this method, processes communicate with each other without using any kind of of shared memory. If two processes p1 and p2 want to communicate with each other, they proceed as follow:

  • Establish a communication link (if a link already exists, no need to establish it again.)
  • Start exchanging messages using basic primitives.
    We need at least two primitives:
    – send(message, destinaion) or send(message)
    – receive(message, host) or receive(message)

Inter-Process Communication | Operating System - Computer Science Engineering (CSE)

The message size can be of fixed size or of variable size. if it is of fixed size, it is easy for OS designer but complicated for programmer and if it is of variable size then it is easy for 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 link. While implementing the link, there are some questions which need to be kept in mind like :

  1. How are links established?
  2. Can a link be associated with more than two processes?
  3. How many links can there be between every pair of communicating processes?
  4. What is the capacity of a link? Is the size of a message that the link can accommodate fixed or variable?
  5. Is a link unidirectional or bi-directional?

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 either of zero capacity or of bounded capacity or of unbounded capacity. In zero capacity, sender wait until receiver inform 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 to 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 specific process identifier for the communication but it is hard to identify the sender ahead of time.

For example: the print server.
In-directed Communication is done via a shred mailbox (port), which consists of queue of messages. Sender keeps the message in mailbox and 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 But the sender expect acknowledgement from 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 but at the same time, if the message send keep on failing, receiver will have to wait for indefinitely. That is why we also consider the other possibility of message passing. There are basically three most preferred combinations:

  • Blocking send and blocking receive
  • Non-blocking send and Non-blocking receive
  • Non-blocking send and Blocking receive (Mostly used)

In Direct message passing, The process which want to communicate must explicitly name the recipient or sender of communication.
e.g. send(p1, message) means send the message to p1.
similarly, receive(p2, message) means receive the message from p2.
In this method of communication, the communication link get 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 link. Symmetry and asymmetry between the sending and receiving can also be implemented i.e. either both process will name each other for sending and receiving the messages or only sender will name receiver for sending the message and there is no need for 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 uses 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 link may be unidirectional or bi-directional. Suppose two process want to communicate though Indirect message passing, the required operations are: create a mail box, use this mail box for sending and receiving messages, destroy the mail box. 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 in 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 forcing 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 which can have multiple sender and single receiver. It is used in client/server application (Here 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 process or 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 create which is shared by n process. 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, lets discuss the Producer-Consumer problem using message passing concept. The producer place items (inside messages) in the mailbox and the consumer can consume item when at least one message present in the mailbox. The code are given below:

Producer Code

void Producer(void){
          
         int item;
         Message m;
          
         while(1){
              
             receive(Consumer, &m);
             item = produce();
             build_message(&m , item ) ;
             send(Consumer, &m);
         }
     }

Consumer Code

void Consumer(void){
          
         int item;
         Message m;
          
         while(1){
              
             receive(Producer, &m);
             item = extracted_item();
             send(Producer, &m);
             consume_item(item);
         }
     }

 

Examples of IPC systems

  1. Posix : uses shared memory method.
  2. Mach : uses message passing
  3. Windows XP : uses message passing using local procedural calls

Communication in client/server Architecture:

There are various mechanism:

  • Pipe
  • Socket
  • Remote Procedural calls (RPCs)

The above three methods will be discussed later article as all of them are quite conceptual and deserve their own separate articles.

fork() in C

fork()  system call is used to create a new process. Newly created process becomes child of the caller process. It take no parameters and return integer value. Below are different values returned by fork().

  • Negative Value: creation of a child process was unsuccessful.
  • Zero: Returned to the newly created child process.
  • Positive value: Returned to parent or caller. The value contains process ID of newly created child process.

Examples:
1) Output of below program.

#include  <stdio.h>
#include  <sys/types.h>
int  main()
{
    pid_t pid = fork();
    if (pid == 0)
        printf("Child process created ");
    else
        printf("Parent process created ");
    return 0;
}

Output:
Parent process created
Child process created

In the above code, a child process is created, fork() returns 0 in the child process and positive integer to the parent process.

 2) Calculate number of times hello is printed.

#include  <stdio.h>
#include  <sys/types.h>
int main()
{
    fork();
    fork();
    fork();
    printf("hello ");
    return 0;
}

Output:
hello
hello
hello
hello
hello
hello
hello
hello

Number of times hello printed is equal to number of process created. Total Number of Processes = 2n where n is number of fork system calls. So here n=3, 2^3 = 8

Let us put some label names for the three lines:

Inter-Process Communication | Operating System - Computer Science Engineering (CSE)

So there are total eight processes (new child processes and one original process).

Please note that the above programs don’t compile in Windows environment.

 fork() vs exec()

The fork system call creates a new process. The new process created by fork() is copy of the current process except the returned value. The exex system call replaces the current process with a new program.

The document Inter-Process Communication | Operating System - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Operating System.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
10 videos|99 docs|33 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Inter-Process Communication - Operating System - Computer Science Engineering (CSE)

1. What is inter-process communication in computer science engineering?
Ans. Inter-process communication (IPC) in computer science engineering refers to the mechanisms and techniques used by different processes running on a computer system to communicate and share data with each other. It allows processes to exchange information, synchronize their actions, and coordinate their activities.
2. Why is inter-process communication important in computer science engineering?
Ans. Inter-process communication is crucial in computer science engineering as it enables different processes to collaborate and work together effectively. It allows processes to share data, resources, and information, leading to improved efficiency, faster execution, and better coordination in complex systems.
3. What are the different methods of inter-process communication?
Ans. There are several methods of inter-process communication, including shared memory, message passing, pipes and sockets, remote procedure calls (RPC), and signals. Each method has its own advantages, and the choice of method depends on the specific requirements and constraints of the system.
4. How does shared memory inter-process communication work?
Ans. Shared memory inter-process communication involves multiple processes accessing a common area of memory to exchange data. The processes can read from and write to this shared memory region, allowing them to communicate directly without the need for intermediate data copying. Proper synchronization mechanisms, like semaphores or mutexes, are used to ensure data consistency and avoid race conditions.
5. What is the difference between message passing and shared memory in inter-process communication?
Ans. In message passing, processes communicate by sending and receiving messages through a communication channel, such as pipes or sockets. The data is copied from the sending process to the receiving process. On the other hand, shared memory allows processes to directly access a common memory region. While message passing is more flexible and suitable for communication between remote processes, shared memory offers faster communication but requires careful synchronization to avoid data corruption.
10 videos|99 docs|33 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

pdf

,

past year papers

,

Free

,

Semester Notes

,

Exam

,

Sample Paper

,

study material

,

mock tests for examination

,

Summary

,

Viva Questions

,

Previous Year Questions with Solutions

,

practice quizzes

,

Extra Questions

,

Inter-Process Communication | Operating System - Computer Science Engineering (CSE)

,

Objective type Questions

,

Inter-Process Communication | Operating System - Computer Science Engineering (CSE)

,

Important questions

,

Inter-Process Communication | Operating System - Computer Science Engineering (CSE)

,

video lectures

,

shortcuts and tricks

,

MCQs

,

ppt

;