Home  Java   Interproces ...

Interprocess communication example in Java

Inter-process communication (IPC) is a mechanism that allows processes to communicate with each other and synchronize their actions. IPC is essential for any operating system to provide a way for multiple processes to cooperate. There are several IPC methods, including:

  1. Pipes: Allow one process to send data to another process.
  2. Message Queues: Allow processes to send and receive messages.
  3. Shared Memory: Allows multiple processes to access a common memory space.
  4. Sockets: Allow processes to communicate over a network.

Let's focus on a simple example using pipes, one of the most common IPC mechanisms.

Example: IPC Using Pipes

In this example, we'll create a parent process that sends a message to a child process using a pipe.

Step 1: Creating the Pipe and Forking the Process

We'll use Java for this example, leveraging the ProcessBuilder to create and manage processes.

import java.io.*;

public class IPCExample {
    public static void main(String[] args) {
        try {
            // Create a pipe
            PipedOutputStream out = new PipedOutputStream();
            PipedInputStream in = new PipedInputStream(out);

            // Create a new process to read from the pipe
            ProcessBuilder processBuilder = new ProcessBuilder("java", "ChildProcess");
            Process process = processBuilder.start();

            // Write to the pipe
            try (PrintWriter writer = new PrintWriter(out)) {
                writer.println("Hello from parent process!");
            }

            // Read from the pipe in the parent process (simulating child process reading)
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
                String message = reader.readLine();
                System.out.println("Parent process received: " + message);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Step 2: Creating the Child Process

Save the following content in a separate file named ChildProcess.java.

import java.io.*;

public class ChildProcess {
    public static void main(String[] args) {
        try {
            // Read from standard input (pipe)
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            String message = reader.readLine();
            System.out.println("Child process received: " + message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Step 3: Compiling and Running the Example

  1. Compile both Java files:
javac IPCExample.java ChildProcess.java
  1. Run the IPCExample:
java IPCExample

Explanation

  1. Pipes:

    • We create a PipedOutputStream and a corresponding PipedInputStream. The PipedOutputStream is connected to the PipedInputStream, allowing us to write data to the pipe and read it from the other end.
  2. ProcessBuilder:

    • We use ProcessBuilder to start a new Java process (ChildProcess). This process will simulate a child process reading from the pipe.
  3. Writing to the Pipe:

    • In the parent process, we write a message to the PipedOutputStream.
  4. Reading from the Pipe:

    • In the child process (simulated by reading from System.in), we read the message from the pipe and print it out.
Published on: Jun 26, 2024, 11:01 PM  
 

Comments

Add your comment