Inter Process Communication (IPC)

Learn via video courses
Topics Covered

Inter process communication in OS is 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.

  • 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 in OS?

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 exachange data and information across multiple processes, which might be on single or multiple computers connected by a network. This is essential for many tasks, such as:

  • Sharing data
  • Coordinating activities
  • Managing resources
  • Achieving modularity

Synchronization in Inter Process Communication

Synchronization in Inter Process Communication (IPC) is the process of ensuring that multiple processes are coordinated and do not interfere with each other. This is important because processes can share data and resources, and if they are not synchronized, they can overwrite each other's data or cause other problems.

There are a number of different mechanisms that can be used to synchronize processes, including:

  • Mutual exclusion: This is a mechanism that ensures that only one process can access a shared resource at a time. This is typically implemented using a lock, which is a data structure that indicates whether a resource is available.
  • Condition variables: These are variables that can be used to wait for a certain condition to be met. For example, a process could wait for a shared resource to become available before using it.
  • Barriers: These are synchronization points that all processes must reach before they can proceed. This can be used to ensure that all processes have completed a certain task before moving on to the next task.
  • Semaphores: These are variables that can be used to count the number of times a shared resource is being used. This can be used to prevent a resource from being used more than a certain number of times at the same time.

Here are some examples of how synchronization is used in IPC:

  • In a database, multiple processes may need to access the same data. Synchronization is used to ensure that only one process can write to the data at a time, and that other processes do not read the data while it is being written.
  • In a web server, multiple processes may need to handle requests from clients. Synchronization is used to ensure that only one process handles a request at a time, and that other processes do not interfere with the request.
  • In a distributed system, multiple processes may need to communicate with each other. Synchronization is used to ensure that messages are sent and received in the correct order, and that processes do not take actions based on outdated information.

Approaches for Inter-Process Communication

approaches for inter process communication.

Pipes

  • Pipes are a simple form of shared memory that allows two processes to communicate with each other.
  • It is a half duplex method (or one way communication) used for IPC between two related processes .
  • One process writes data to the pipe, and the other process reads data from the pipe.
  • It is like a scenario like filling water with a tap into a bucket. The filling process is writing into the pipe and the reading process is retrieving from the pipe.
  • Pipes can be either named or anonymous, depending on whether they have a unique name or not.
  • Named pipes are a type of pipe that has a unique name, and can be accessed by multiple processes. Named pipes can be used for communication between processes running on the same host or between processes running on different hosts over a network.
  • Anonymous pipes, on the other hand, are pipes that are created for communication between a parent process and its child process. Anonymous pipes are typically used for one-way communication between processes, as they do not have a unique name and can only be accessed by the processes that created them. pipes different ways to implement ipc

Shared Memory

  • Shared memory is a region of memory that is accessible to multiple processes. This allows processes to communicate with each other by reading and writing data from the shared memory region.
  • Shared memory is a fast and efficient way for processes to communicate, but it can be difficult to use if the processes are not carefully synchronized.
  • There are two main types of shared memory:
    • Anonymous shared memory: Anonymous shared memory is not associated with any file or other system object. It is created by the operating system and is only accessible to the processes that created it.
    • Mapped shared memory: Mapped shared memory is associated with a file or other system object. It is created by mapping a file into the address space of one or more processes.

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

  • Message passing is a method of Inter Process Communication in OS. It involves the exchange of messages between processes, where each process sends and receives messages to coordinate its activities and exchange data with other processes.
  • Processes can communicate without any shared variables, therefore it can be used in a distributed environment on a network.
  • In message passing, each process has a unique identifier, known as a process ID, and messages are sent from one process to another using this identifier. When a process sends a message, it specifies the recipient process ID and the contents of the message, and the operating system is responsible for delivering the message to the recipient process. The recipient process can then retrieve the contents of the message and respond, if necessary.
  • Message passing in shared memory has a number of advantages over other IPC mechanisms. First, it is very fast, as messages are simply copied from one process's address space to another. Second, it is very flexible, as any type of data can be shared between processes. Third, it is relatively easy to implement, as it does not require any special support from the operating system.
  • However, message passing in shared memory also has some disadvantages. First, it can be difficult to ensure that messages are delivered in the correct order. Second, it can be difficult to manage the size of the message queue. Third, it can be difficult to port to other platforms, as the implementation of shared memory can vary from one operating system to another.

Message Queues

  • Message queues are a more advanced form of pipes.
  • They allow processes to send messages to each other, and they can be used to communicate between processes that are not running on the same machine.
  • Message queues are a good choice for communication between processes that need to be decoupled from each other.
  • In Message Queue IPC, each message has a priority associated with it, and messages are retrieved from the queue in order of their priority. This allows processes to prioritize the delivery of important messages and ensures that critical messages are not blocked by less important messages in the queue.
  • Message Queue IPC provides a flexible and scalable method of communication between processes, as messages can be sent and received asynchronously, allowing processes to continue executing while they wait for messages to arrive.
  • The main disadvantage of Message Queue IPC is that it can introduce additional overhead, as messages must be copied between address spaces, and the queue must be managed by the operating system to ensure that it remains synchronized and consistent across all processes.

We have 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

Direct Communication

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

  • A pair of communicating processes must have one link between them.
  • A link (generally bi-directional) establishes between every pair of communicating processes.
  • In direct communication, the sender process must know the identifier of the receiver process in order to send a message to it. This identifier can be a process ID, a port number, or some other unique identifier. Once the sender process has the identifier of the receiver process, it can send a message to it directly.
  • The main advantage of direct communication is that it provides a simple and direct way for processes to communicate, as processes can access each other’s data directly without the need for intermediate communication mechanisms.
  • However, direct communication also has some limitations, as it can lead to tight coupling between processes, and it can make it more difficult to change the communication mechanism in the future, as direct communication is hardcoded into the processes themselves.

direct communication different ways to implement ipc

Indirect Communication

  • Indirect communication in IPC is a method of communication in which processes do not explicitly name the sender or receiver of the communication. Instead, processes communicate through a shared medium such as a message queue or mailbox.
  • Pairs of communicating processes have shared mailbox.
  • Link (uni-directional or bi-directional) is established between pairs of processes.
  • Sender process puts message in the port or mailbox of reciever process and reciever process takes out (or deletes) the data from mailbox.
  • The sender and receiver processes do not need to know each other's identifiers in order to communicate with each other.
  • The main advantage of indirect communication is that it provides a more flexible and scalable way for processes to communicate, as processes do not need to have direct access to each other’s data.
  • However, indirect communication can also introduce additional overhead, as data must be copied between address spaces, and the communication mechanism must be managed by the operating system to ensure that it remains synchronized and consistent across all processes.

FIFO

  • FIFO (First In First Out) is a type of message queue that guarantees that messages are delivered in the order they were sent.
  • It involves the use of a FIFO buffer, which acts as a queue for exchanging data between processes.
  • Used to communicate between two processes that are not related.
  • In the FIFO method, one process writes data to the FIFO buffer, and another process reads the data from the buffer in the order in which it was written.
  • Full-duplex method - Process P1 is able to communicate with Process P2, and vice versa.
  • The main advantage of the FIFO method is that it provides a simple way for processes to communicate, as data is exchanged sequentially, and there is no need for processes to coordinate their access to the FIFO buffer.
  • However, the FIFO method can also introduce limitations, as it may result in slow performance if the buffer becomes full and data must be written to the disk, or if the buffer becomes empty and data must be read from the disk.

Why Inter Process Communication (IPC) is Required?

Inter-process communication (IPC) is required for a number of reasons:

  • Sharing data: IPC allows processes to share data with each other. This is essential for many tasks, such as sharing files, databases, and other resources.
  • Coordinating activities: IPC allows processes to coordinate their activities. This is essential for tasks such as distributed computing, where multiple processes are working together to solve a problem.
  • Managing resources: IPC allows processes to manage resources such as memory, devices, and files. This is essential for ensuring that resources are used efficiently.
  • Achieving modularity: IPC allows processes to be developed and maintained independently of each other. This makes it easier to develop and maintain large and complex software systems.
  • Flexibility: IPC allows processes to run on different hosts or nodes in a network, providing greater flexibility and scalability in large and complex systems.

FAQ

Q. What is Inter-Process Communication (IPC)?

A. IPC is a mechanism that allows processes in an operating system to communicate, share data, and synchronize their activities.

Q. Why is IPC necessary in operating systems?

A. IPC is necessary to enable processes to cooperate, coordinate, and exchange information, which is vital for modern computing systems and multitasking environments.

Q. What is the difference between IPC and threads?

A. IPC involves communication between separate processes, while threads are lightweight sub-processes within the same process that share the same memory space. IPC is used to communicate between separate processes, and threads are used to parallelize within a process.

Q. What are some common scenarios where IPC is used?

A. IPC is used in scenarios such as inter-thread communication, client-server applications, distributed systems, parallel processing, and multi-core processors.

Q. What are the advantages of using synchronization methods in IPC?

A. Synchronization methods in IPC ensure data consistency, prevent race conditions, and improve performance.

Q. What are the common IPC mechanisms in operating systems?

A. The common IPC mechanisms in operating systems are pipes, message queues, shared memory, direct and indirect communication.

Q. What are some common synchronization methods used in IPC?

A. Common synchronization methods include mutexes, semaphores, condition variables, monitors, and message passing, each offering specific benefits for coordinating processes.

Q. What are the challenges in IPC, and how can they be addressed?

A. Challenges in IPC include synchronization, race conditions, and deadlock. These challenges can be addressed through proper design, synchronization mechanisms, and error handling.

Conclusion

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

  • IPC is 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 synchronisation among processes in IPC to maintain data consistency. Semaphore and mutex are two ways to do so.