OS Notes
Complete guide to IPC mechanisms including shared memory, message passing, pipes, named pipes (FIFOs), sockets, and signals with practical code examples for each method.
Introduction
Processes on your computer are like people living in separate apartments. Each has their own private space (address space) and cannot see or access their neighbor's belongings. But sometimes they need to communicate — maybe one process generates data that another needs to display, or two processes need to coordinate their actions.
Interprocess Communication (IPC) refers to the mechanisms an OS provides for processes to exchange data and coordinate. Unlike threads (which share memory naturally), processes have isolated address spaces, so communication requires explicit OS support.
Why Do Processes Need to Communicate?
- Data sharing: A web server process shares session data with a database process
- Computation speedup: Divide a task among multiple processes running in parallel
- Modularity: A system designed as cooperating processes (microservices architecture)
- Convenience: A user running multiple tasks that need to share information
Two Fundamental Approaches
1. Shared Memory
Processes agree to share a region of memory. They read/write this shared region directly. Fast (no kernel involvement after setup) but requires synchronization to prevent race conditions.
2. Message Passing
Processes communicate through the kernel by sending and receiving messages. Safer (no shared state) but slower (each message crosses the kernel boundary).
IPC Mechanism 1: Pipes
A pipe is a unidirectional communication channel between related processes (typically parent and child). Data written to one end can be read from the other, like a water pipe.
Limitations: Unidirectional, only between related processes (parent-child), no random access (FIFO only).
IPC Mechanism 2: Named Pipes (FIFOs)
Named pipes overcome the "related processes only" limitation. They exist as special files in the file system, so any process that knows the path can communicate.
IPC Mechanism 3: Shared Memory
The fastest IPC mechanism. Two processes map the same physical memory region into their address spaces. After setup, no kernel involvement is needed for communication.
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
// Process A: Create and write shared memory
int fd = shm_open("/my_shm", O_CREAT | O_RDWR, 0666);
ftruncate(fd, 4096); // Set size
char *ptr = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
sprintf(ptr, "Hello from Process A!");
// Process B: Open and read shared memory
int fd = shm_open("/my_shm", O_RDONLY, 0666);
char *ptr = mmap(NULL, 4096, PROT_READ, MAP_SHARED, fd, 0);
printf("Read: %s\n", ptr); // "Hello from Process A!"Critical: Shared memory requires synchronization (semaphores/mutexes) to prevent race conditions when both processes access it simultaneously.
IPC Mechanism 4: Message Queues
Structured messages stored in a kernel-managed queue. Processes send typed messages that receivers can selectively retrieve.
IPC Mechanism 5: Sockets
Sockets enable communication between processes on the same machine or across a network. They are the foundation of all network communication.
// Server (simplified)
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
bind(server_fd, (struct sockaddr*)&address, sizeof(address));
listen(server_fd, 3);
int client_fd = accept(server_fd, NULL, NULL);
read(client_fd, buffer, 1024);
// Client (simplified)
int sock = socket(AF_INET, SOCK_STREAM, 0);
connect(sock, (struct sockaddr*)&server_addr, sizeof(server_addr));
send(sock, "Hello Server!", 13, 0);IPC Mechanism 6: Signals
Signals are asynchronous notifications sent to a process to notify it of an event. They are like software interrupts.
#include <signal.h>
void handler(int signum) {
printf("Caught signal %d\n", signum);
}
int main() {
signal(SIGINT, handler); // Register handler for Ctrl+C
signal(SIGUSR1, handler); // Register for user signal
while(1) pause(); // Wait for signals
return 0;
}
// From another process or terminal:
// kill -SIGUSR1 <pid>Comparison of IPC Mechanisms
| Mechanism | Speed | Complexity | Scope | Use Case |
|---|---|---|---|---|
| Pipes | Medium | Low | Parent-child | Shell pipelines |
| Named Pipes | Medium | Low | Same machine | Simple IPC |
| Shared Memory | Fastest | High (sync needed) | Same machine | High-throughput data |
| Message Queues | Medium | Medium | Same machine | Structured messages |
| Sockets | Slower | Medium | Network-wide | Client-server apps |
| Signals | Fast | Low | Same machine | Event notification |
Real-World Analogy
IPC mechanisms are like different ways people in separate offices communicate:
- Pipes: A pneumatic tube between two specific offices (fast, one-way, only between connected offices)
- Shared Memory: A shared whiteboard in the hallway (fastest, but people might write over each other)
- Message Passing: Internal mail system with envelopes (organized, but mail room adds delay)
- Sockets: Phone calls (works across buildings/cities, but setup and per-message overhead)
- Signals: Fire alarm or intercom announcement (notification only, no data payload)
Key Takeaways
- IPC allows isolated processes to exchange data and coordinate actions
- Shared memory is fastest but requires careful synchronization
- Message passing is safer but involves kernel overhead for each communication
- Pipes are simple but limited to related processes; named pipes work between any processes
- Sockets enable network communication between processes on different machines
- Signals provide asynchronous event notification between processes
- The choice of IPC mechanism depends on speed requirements, data volume, and whether processes are on the same machine
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Interprocess Communication (IPC).
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Operating Systems topic.
Search Terms
operating-systems, operating systems, operating, systems, process, management, interprocess, communication
Related Operating Systems Topics