Because the Linux operating system strictly isolates each process inside its own protected virtual memory address space (to prevent one faulty program from crashing others), processes cannot directly read or write into each other's memory. Inter-Process Communication (IPC) refers to the standardized set of mechanisms provided by the Linux kernel to enable processes to share data and coordinate activities.
Figure 6: Overview of the five major IPC mechanisms available on Linux.
pipe()An anonymous pipe is a unidirectional, half-duplex communication channel. The pipe() system call creates two file descriptors:
fd[0] : Read end of the pipe (data is read from here).
fd[1] : Write end of the pipe (data is written here).
/*
* pipe_demo.c - Parent writes message, child reads and prints
* Compile: gcc pipe_demo.c -o pipe_demo
* Run : ./pipe_demo
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
int main() {
int pipe_fd[2];
pid_t pid;
char write_msg[] = "Hello Child from Veewom Linux IPC!";
char read_buf[100];
// Step 1: Create pipe BEFORE fork()
if (pipe(pipe_fd) == -1) {
perror("Pipe creation failed");
return 1;
}
// Step 2: Fork child process
pid = fork();
if (pid < 0) {
perror("Fork failed");
return 2;
}
if (pid == 0) {
// --- CHILD PROCESS (Reader) ---
// Close unused write end
close(pipe_fd[1]);
// Read message from pipe
ssize_t bytes = read(pipe_fd[0], read_buf, sizeof(read_buf));
read_buf[bytes] = '\0'; // Null-terminate string
printf("[CHILD] Received message via pipe: '%s'\n", read_buf);
// Close read end
close(pipe_fd[0]);
exit(0);
} else {
// --- PARENT PROCESS (Writer) ---
// Close unused read end
close(pipe_fd[0]);
printf("[PARENT] Sending message to child...\n");
write(pipe_fd[1], write_msg, strlen(write_msg) + 1);
// Close write end (sends EOF to reader)
close(pipe_fd[1]);
// Wait for child to finish
wait(NULL);
printf("[PARENT] Child finished reading.\n");
}
return 0;
}
Unlike anonymous pipes which only work between related parent/child processes, Named Pipes (FIFOs) exist as special files on the filesystem disk directory (created via mkfifo() or terminal $ mkfifo /tmp/myfifo). Any two unrelated processes can open this file and communicate in FIFO (First-In, First-Out) order.
Shared memory maps the exact same block of physical RAM into the address spaces of multiple processes. Because processes read and write directly to RAM without passing through kernel buffers, it achieves the highest throughput possible.
1. shmget() / shm_open() : Allocates or opens shared memory segment.
2. shmat() / mmap() : Attaches shared segment to process virtual address space.
3. shmdt() / munmap() : Detaches segment when finished.
4. shmctl(IPC_RMID) : Destroys the shared segment.
If the pipe is empty and the write end is still open, the read() system call blocks (sleeps) until data is written. However, if all write descriptors have been closed, read() immediately returns 0, signaling End-Of-File (EOF).
The kernel sends a SIGPIPE signal to the writing process. If unhandled, this signal terminates the process with the error "Broken pipe". If SIGPIPE is caught or ignored, the write() call returns -1 with errno set to EPIPE.
Intercept asynchronous software interrupts and timers in Class 28: Linux Signals & Handling.
Continue to Class 28: Linux Signals & Handling → Previous: ← File I/O | Full Syllabus