Inter-Process Communication

Inter-Process Communication

ยท

6 min read

Bridging the Gap in Data Communication

Have you ever wondered how data seamlessly travels between different software applications, enabling the digital world to function as smoothly as it does? Behind the scenes, this magic is made possible by a fundamental concept in computer science known as Inter-Process Communication (IPC).

In this blog post, We'll explore what IPC is and why it's essential for modern computing

What is IPC?

It is a technique used by operating systems and software applications to allow processes to communicate and share data. In a multitasking operating system or distributed network, multiple processes run independently, and IPC enables them to exchange information, synchronize their activities, and work together to achieve specific tasks.

IPC allows various processes or components, whether residing on the same device or distributed across a network, to exchange information. Think of it as the digital language that applications speak to understand each other's needs.

Why IPC?

Processes operate as independent programs, each isolated from others to maintain security and stability. However, there are situations where these distinct processes need to collaborate and exchange information to achieve a common goal. Inter-process communication (IPC) becomes crucial in such scenarios. Unlike threads within the same process, processes do not share memory by default. Therefore, IPC mechanisms are essential for enabling communication and coordination between processes, allowing them to perform actions collectively and share data without direct memory access.

IPC in Different ways:

  1. Message Passing: Processes can send and receive messages containing data or signals. These messages can be sent via message queues, pipes, sockets, or other communication channels. Message passing ensures secure data exchange between processes.

  2. Shared Memory: Processes can share a portion of their memory space, allowing them to read and modify shared data. This method offers fast communication but requires careful synchronization to prevent conflicts when multiple processes access shared memory simultaneously.

  3. Synchronization Primitives: IPC can involve synchronization primitives like semaphores, mutexes, and condition variables, which help processes coordinate their actions and avoid conflicts when accessing shared resources.

  4. Remote Procedure Calls (RPC) and Remote Method Invocation (RMI): RPC and RMI allow a process to invoke a procedure or method in another process, even if they are on different systems. This method is commonly used in distributed computing environments.

Let's understand how Inter-Process Communication (IPC) is involved in software applications with an example

  1. Message Passing

    ๐Ÿ“Œ Distributed across the network

    We all have experience working with APIs and integrating them into web clients, as well as integrating our web servers with third-party APIs such as Razorpay and PayPal etc.,

    Do you know that all this type of communication is said to be Inter-Process communication? where two processes run independently and try to share data through the network using some of the messaging protocols like HTTP for communication.

    ๐Ÿ“Œ In Same Device

    In Android, message passing within the same device is a fundamental concept exemplified by a feature we use daily:

    Sharing images from the gallery via WhatsApp

    Imagine a user who wants to share an image. The Gallery app, responsible for displaying and editing images, runs as a separate process. Similarly, WhatsApp operates independently, handling messaging and file sharing.

    WhatsApp allows users to share images from the gallery, but there's a challenge. The Gallery app and WhatsApp are distinct processes, isolated from each other. Direct access from one to the other is impossible.

    Here's where Inter-Process Communication (IPC) and message passing step in. Android utilizes a package called Intent, a messaging protocol allowing different processes to share data seamlessly. Through Intents, these separate processes can communicate and exchange data.

    By employing this messaging protocol, the Gallery app can pass the selected image data to WhatsApp. Despite their isolation, these processes collaborate effectively, all within the same device.

    An image that consists of two images: one on the left showing a mobile screen with gallery open, selecting an image and sharing it via mail, WhatsApp and two other options; and one on the right showing a chat page that has the selected image as sent from this side. The background is white and the images look realistic.

  2. Shared Memory

    Problem

    Imagine a large e-commerce platform with multiple microservices handling various tasks such as user management, product catalogue, payment processing, and order fulfilment. One challenge faced by the system is user authentication. Each microservice needs to ensure that requests are made by authenticated users with the appropriate permissions. However, maintaining consistent user sessions and permissions across different services can be complex.

    Solution

    To solve this we created a centralised user authentication service which creates a session for the user and creates a unique session ID and Utilizing Redis as a shared memory cache for the User Authentication Service.

    Now whenever the other services want to know the user state they can get the user state by querying the Redis using the sessionID from the incoming request and same like any other service that needs the user state can retrieve it from the centralised Redis store.

    In this scenario, Redis acts as shared memory, serving as a central data store accessible by various microservices or processes. It effectively shares data among them, ensuring seamless communication, consistency, and efficient coordination between different parts of the system. This shared memory concept facilitates the exchange of information, allowing microservices to work together cohesively and enhancing the overall functionality of the system.\

  3. Remote procedure calls:

    RPC is one of the fastest and most familiar Inter-Process Communication (IPC) protocols that make communication between servers more reliable.

    RPC allows a program to execute code in another address space (commonly on another computer) as if it were a local procedure call, making it easier to develop distributed applications where different parts of the application run on different computers.

    Consider two services: 'placeMyRide' and a service responsible for calculating the shortest path between source and destination points.

    The 'placeMyRide' service needs the shortest path information for journey planning. It achieves this by invoking an RPC-enabled service responsible for path calculations. Through this procedure call, 'placeMyRide' provides the necessary parameters. The calculation service processes the request, computes the shortest path, and returns the data.

    This seamless interaction facilitated by RPC ensures effective communication between services. It allows 'placeMyRide' to efficiently access the required data, enhancing the overall functionality and responsiveness of the system.

Conclusion:
In conclusion, understanding and implementing these IPC techniques are fundamental for developers aiming to build robust, scalable, and responsive systems. Whether it's within a single device or across distributed networks, mastering these communication paradigms ensures that software components work harmoniously, providing users with reliable and seamless experiences. As we continue to innovate, the art of effective inter-process communication remains at the heart of creating powerful and connected software ecosystems.

ย