Search for Courses, Topics

Inter Process Communication (IPS) in OS (Operating System)

Learn about inter process communication in OS.

13 Feb 2022-5 mins read
quiz
Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Overview

Inter-process communication in OS is the way by which multiple processes can communicate with each other. Shared memory in OS, message queues, FIFO, etc. are some of the ways to achieve IPC in os.

Scope of the Article

  • This article introduces interprocess communication in OS and discusses the need for the same.
  • This article also covers different ways to implement ipc in OS using C language.

What is Inter Process Communication?

A system can have two types of processes i.e. independent or cooperating. Cooperating processes affect each other and may share data and information among themselves.

what is inter process communication

Interprocess Communication or IPC provides a mechanism to exchange data and information across multiple processes, which might be on single or multiple computers connected by a network.

Why IPC is required?

IPC helps achieve these things:

  • Computational Speedup
  • Modularity
  • Information and data sharing
  • Privilege separation
  • Processes can communicate with each other and synchronize their action.

Approaches for Inter-Process Communication

approaches for inter process communication.

Different Ways to Implement IPC

Pipes

  • It is a half-duplex method (or one-way communication) used for IPC between two related processes.
  • It is like a scenario like filling the water with a tap into a bucket. The filling process is writing into the pipe and the reading process is retrieved from the pipe.

pipes different ways to implement ipc

//Sample pseudo-code program to implement IPC using Pipe (C language)//

Start:
Store any message in one character array ( char *msg=”Hello world”)
Declare another character array
Create a pipe by using pipe() system call
Create another process by executing fork() system call
In parent process use system call write() to write message from one process to
another process.
In child process display the message.

Shared Memory

Multiple processes can access a common shared memory. Multiple processes communicate by shared memory, where one process makes changes at a time and then others view the change. Shared memory does not use kernel.

shared memory different ways to implement ipc

Message Passing

  • In IPC, this is used by a process for communication and synchronization.
  • Processes can communicate without any shared variables, therefore it can be used in a distributed environment on a network.
  • It is slower than the shared memory technique.
  • It has two actions sending (fixed size message) and receiving messages.

Message Queues

We have a linked list to store messages in a kernel of OS and a message queue is identified using "message queue identifier".

message queues different ways to implement ipc

//Psedo-code to implement message queue (C language)//

Start:
Create a message queue or connect to an already existing message queue 
Write into message queue 
Read from the message queue
Perform control operations on the message queue 
End

Direct Communication

In this, process that wanna communicate must name the sender or receiver.

  • A pair of communicating processes must have one link between them.
  • A link (generally bi-directional) establishes between every pair of communicating processes.

direct communication different ways to implement ipc

Indirect Communication

  • Pairs of communicating processes have shared mailboxes.
  • Link (uni-directional or bi-directional) is established between pairs of processes.
  • Sender process puts the message in the port or mailbox of a receiver process and receiver process takes out (or deletes) the data from the mailbox.

FIFO

  • Used to communicate between two processes that are not related.
  • Full-duplex method - Process P1 is able to communicate with Process P2, and vice versa.

Terms Used in IPC

Since multiple processes can read and write on shared variables and data, it is very important to maintain consistency of data, else we can get error-prone results. To prevent this we synchronize the processes. Some common ways to achieve this are given below.

  • Semaphore: A variable that manages several processes' access to a shared resource. Binary and counting semaphores are the two types of semaphores.
  • Mutual Exclusion or mutex: is a term used to describe a situation where only one process or thread at a time can enter the crucial part due to mutual exclusion. This avoids race conditions.

What is Like FIFOS and Unlike FIFOS?

Like FIFOSUnlike FIFOS
FIFO or first in first out method is followedThere are methods to remove urgent messages before these messages arrive at the front.
Both receiving and sending processes are not required for FIFO to occur.It is always ready, therefore opening or closing is not required.
Data transfer is allowed to occur among processes that are unrelated.There are no synchronization problems between open and close.

Conclusion

Congrats! You have completed this tutorial. Let's go through what we have studied.

  • IPC is the way by which multiple processes or threads communicate among each other.
  • IPC in OS obtains modularity, computational speedup and data sharing.
  • Different ways of IPC are pipe, message passing, message queue, shared memory, direct communication, indirect communication and FIFO.
  • It is important to obtain synchronization among processes in IPC to maintain data consistency. Semaphore and mutex are two ways to do so.
Challenge Time!
quiz Time to test your skills and win rewards! Note: Rewards will be credited after the next product update.