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:
- Pipes: Allow one process to send data to another process.
- Message Queues: Allow processes to send and receive messages.
- Shared Memory: Allows multiple processes to access a common memory space.
- 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
- Compile both Java files:
javac IPCExample.java ChildProcess.java
- Run the
IPCExample
:
java IPCExample
Explanation
-
Pipes:
- We create a
PipedOutputStream
and a correspondingPipedInputStream
. ThePipedOutputStream
is connected to thePipedInputStream
, allowing us to write data to the pipe and read it from the other end.
- We create a
-
ProcessBuilder:
- We use
ProcessBuilder
to start a new Java process (ChildProcess
). This process will simulate a child process reading from the pipe.
- We use
-
Writing to the Pipe:
- In the parent process, we write a message to the
PipedOutputStream
.
- In the parent process, we write a message to the
-
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.
- In the child process (simulated by reading from
Published on: Jun 26, 2024, 11:01 PM