Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Operating System - Inter-Process Communication



Inter-process Communication (IPC) is a mechanism that allows the exchange of data between processes. It enables resource and data sharing between the processes without interference. This communication involves one process informing another that an event has occurred or that some data has been transferred between processes.

Inter-Process Communication

Inter-process Communication (IPC) helps in faster communication between processes. It promotes parallel processing and multitasking. IPC allows processes to share memory data and system resources without creating copies, which saves memory space. It also provides good synchronization between processes.

Processes executing concurrently in the operating system may either be independent processes or cooperating processes

  • Independent Processes − If the processes are not sharing anythng then these processes are called independent. These process cannot affect or be affected by the other processes executing in the system.
  • Cooperating Processes − These process can affect or be afected by the other processes executing in the system. So, if any process that shares data with other processes, then we can say that, that process can affect the other process with which it is sharing the data.

Interprocess communication is required in cooperating processes because they may need to communicate with each other.

Synchronization in Inter-Process Communication

Synchronization is a necessary part of inter-process communication. It is either provided by the inter-process control mechanism or handled by the communicating processes. Some of the methods to provide synchronization are as follows −

  • Semaphore − A semaphore is a variable th at controls the access to a common resource by multiple processes. The two types of semaphores are binary semaphores and counting semaphores.
  • Mutual Exclusion − Mutual exclusion requires that only one process thread can enter the critical section at a time. This is useful for synchronization and also prevents race conditions.
  • Barrier − A barrier does not allow individual processes to proceed until all the processes reach it. Many parallel languages and collective routines impose barriers.
  • Spinlock − A spinlock is a type of lock. The processes trying to acquire this lock wait in a loop while checking if the lock is available or not. This is known as busy waiting because the process is not doing any useful operation even though it is active.

Why is Inter-Process Communication Necessary?

Inter-process communication is necessary in process management for a number of reasons, which are highlighted in the following list −

  • Information sharing − If several users are interested in the same piece of information, we must provide an environment to allow concurrent access to such information. Inter-process communication is used for sharing data and coordinating actions between processes. With IPC, different processes can exchange information, send commands, and work together to complete a task.
  • Achieving Modularity − When a large task is broken into smaller modules, each of these modules is executed by one or more processes. As these processes are a part of a bigger task, they need to coordinate among themselves, share data and control signals to achieve the task. IPC helps in coordination among the processes and thus helps to achieve modularity.
  • Faster Processes − Properly synchronized inter-process communication helps processes to complete tasks faster. If we want a particular task to run faster, we must break it into subtasks, then each subtask will be executed parallely. IPC will help these parallel processes in sharing information or coordinate their progress with each other so they can work together and complete the task faster. So, IPC overall speed up the system.
  • Convenience − Individual users may work on many tasks at the same time and users may edit, listen to music and do all types of tasks at a time because the operating system is very convenient due to inter-process communication.

How to Implement Inter-Process Communication?

We can implement Inter Process Communication (IPC) in the following two ways −

  • Shared Memory
  • Message Passing

Let's discuss both these methods one by one.

Shared Memory

A shared memory is the memory that can be simultaneously accessed by multiple processes. This is done so that the processes can communicate with each other. Processes can exchnage information by reading and writing data to the shared region or area. All POSIX(Portable Operating System Interface) systems like Linux as well as Windows operating systems use shared memory.

Shared memory requires a shared region (common area) that multiple processes can use. This shared memory region is stored in the address space of the process that creates it. Other processes that want to communicate using this shared memory, they must attach this shared memory segment to their own address space.

The following image depicts how we can use a shared memory space to facilitate inter-process communication −

Shared Memory
  • Suppose Process A wants to communicate with Process B.
  • First, Process A creates a shared memory region, and this shared memory is stored in Process A's address space. This means Process A initiates the communication by creating a common memory area.
  • If Process B wants to communicate and exchange data using this shared memory, then it must attach this shared memory segment to its own address space. Then only both processes can access the same memory location.
  • Now, if Process A writes data into the shared memory, then Process B can also read it directly from its own address space.
  • If Process B updates the shared memory, then Process A will also see the updated value.

In this way, by using a shared memory space, interprocess communication becomes easy and fast.

Message Passing

Communication takes place by means of messages exchanged between the cooperating processes. Message passing is useful for exchanging small amounts of data because no conflicts need be avoided. In this method, one process sends a message, and the other process receives it, allowing them to share information.

It is easier to implement when compared to shared memory because it allows processes to communicate and to synchronize their work/task without sharing the same address space and this method is useful in distributed environment, where the communicating processes may run on different computers connected by a network.

The following image shows the Message Passing method of inter-process communication −

Message Passing

Ways to Implement Message Passing

Message Passing can be achieved through different methods like Sockets, Message Queues, and Pipes.

  • A pipe is a data channel that is unidirectional. Two pipes can be used to create a two-way data channel between two processes. This uses standard input and output methods. Pipes are used in all POSIX systems as well as Windows operating systems.
  • A socket is the endpoint for sending or receiving data in a network. This is true for data sent between processes on the same computer or data sent between different computers on the same network. Most of the operating systems use sockets for interprocess communication.
  • A file is a data record that may be stored on a disk or acquired on demand by a file server. Multiple processes can access a file as required. All operating systems use files for data storage.
  • Signals are useful in interprocess communication in a limited way. They are system messages that are sent from one process to another. Normally, signals are not used to transfer data but are used for remote commands between processes.
  • Message Queue multiple processes can read and write data to the message queue without being connected to each other. Messages are stored in the queue until their recipient retrieves them. Message queues are quite useful for inter-process communication and are used by most operating systems.

Conclusion

To conclude, inter-process communication allows multiple processes to share information, coordinate tasks. It also improves the system performance and multitasking but also supports distributed applications where processes may run on different machines.

Advertisements