Process Communication (IPC)
What is Process Communication?
Process Communication, commonly called Inter-Process Communication (IPC), refers to the mechanisms provided by an operating system that allow two or more processes to exchange data and coordinate their activities.
IPC = Communication + Synchronization between processes
Normally, processes have separate memory spaces:
Process A Process B ┌───────────────┐ ┌───────────────┐ │ Code │ │ Code │ │ Data │ │ Data │ │ Heap │ │ Heap │ │ Stack │ │ Stack │ └───────────────┘ └───────────────┘ │ │ └──────────── IPC ───────────────┘
One process generally cannot directly access another process's private memory. The OS provides controlled mechanisms for communication.
Why Do Processes Need Communication?
Processes often need to work together.
For example, consider a web browser:
Browser | +── Process A → User Interface | +── Process B → Network | +── Process C → Rendering | +── Process D → Extensions
These processes may need to exchange information.
For example:
Network Process ↓ Downloaded data ↓ Rendering Process ↓ Display webpage
IPC makes this possible.
Main Goals of IPC
IPC is used for:
1. Data Sharing
Processes can exchange information.
Process A ↓ Data ↓ Process B
2. Synchronization
Processes can coordinate their execution.
For example:
Process A → Complete task ↓ Process B ↓ Start next task
3. Resource Sharing
Processes can coordinate access to shared resources such as:
- Files
- Devices
- Memory
- Databases
4. Modularity
A large application can be divided into separate processes.
Application | +── Process A +── Process B +── Process C
Each process performs a specific job.
Two Main IPC Models
There are two fundamental IPC approaches:
- Shared Memory
- Message Passing
IPC | +-------+-------+ ↓ ↓ Shared Memory Message Passing
1. Shared Memory
In the shared-memory model, the OS creates a memory region that multiple processes can access.
Process A Process B │ │ │ │ └───────┐ ┌───────┘ ↓ ↓ ┌─────────────┐ │ Shared │ │ Memory │ └─────────────┘
Both processes can read and write data in the shared region.
Example
Suppose:
Process A → Producer Process B → Consumer
Process A produces data:
Process A ↓ Shared Memory ↓ Process B ↓ Uses data
For example:
Producer → [10, 20, 30, 40] → Consumer
Problem with Shared Memory
The biggest problem is synchronization.
Suppose both processes access the same variable:
Shared variable = 100
Both processes modify it at the same time.
Process A ──┐ ├──→ Shared Data Process B ──┘
This can create a race condition.
Therefore, shared memory usually needs synchronization mechanisms such as:
- Mutex
- Semaphore
- Condition variables
- Other synchronization primitives
Advantages of Shared Memory
- Very fast after the shared region is established
- Efficient for large amounts of data
- Processes can directly access shared data
- Avoids repeatedly copying large data through messages
Disadvantages
- Synchronization is required
- Race conditions can occur
- Programming is more complex
- Incorrect synchronization can cause deadlocks
2. Message Passing
In message passing, processes communicate by sending and receiving messages through an OS-supported mechanism.
Process A | | Send(Message) ↓ ┌──────────────┐ │ IPC Channel │ └──────────────┘ | | Receive(Message) ↓ Process B
For example:
Process A | | "Hello" ↓ Process B
The processes don't necessarily need to share the same memory.
Basic Message-Passing Operations
Usually there are two fundamental operations:
Send
send(message)
Receive
receive(message)
Conceptually:
Process A Process B | | |──── send(message) ────────>| | | |<─── receive(message) ─────|
Direct Communication
In direct communication, processes explicitly identify each other.
For example:
Process A → Process B
Conceptually:
send(ProcessB, message)
and:
receive(ProcessA, message)
The sender knows the receiver.
Indirect Communication
In indirect communication, processes communicate through an intermediate object such as a mailbox or message queue.
Process A | ↓ ┌──────────────┐ │ Mailbox / │ │ MessageQueue │ └──────────────┘ | ↓ Process B
Process A doesn't necessarily communicate directly with Process B.
Blocking and Non-Blocking Communication
Message passing can also be classified according to whether operations wait.
Blocking Send
The sender may wait until the system accepts or processes the message.
Send ↓ WAIT ↓ Continue
Non-Blocking Send
The sender continues execution without waiting for the receiver to process the message.
Send ↓ Continue immediately
Blocking Receive
The receiver waits until a message is available.
Receive ↓ Message available? ↓ No WAIT ↓ Yes Process message
Non-Blocking Receive
The receiver checks for a message and continues if none is available.
Receive ↓ Message? / \ Yes No ↓ ↓ Use Continue
IPC Mechanisms
Operating systems provide several IPC mechanisms.
The important ones are:
- Pipes
- Named Pipes
- Message Queues
- Shared Memory
- Semaphores
- Signals
- Sockets
- Memory-Mapped Files
Let's understand each.
1. Pipes
A pipe provides a communication channel between processes.
Process A | | Write ↓ ┌───────────┐ │ Pipe │ └───────────┘ | | Read ↓ Process B
A common use is:
Process A → Pipe → Process B
Linux Example
ls | grep ".txt"
Here:
ls ↓ Pipe ↓ grep
The output of ls becomes the input of grep.
2. Named Pipes
A normal anonymous pipe is commonly associated with related processes.
A named pipe has a name in the filesystem namespace and can be used by processes that don't necessarily have a parent-child relationship.
Linux example:
mkfifo mypipe
Then one process can write:
echo "Hello" > mypipe
Another process can read:
cat mypipe
Conceptually:
Process A ↓ mypipe ↓ Process B
3. Message Queues
A message queue stores messages until a receiving process retrieves them.
Process A | ↓ ┌───────────────────┐ │ Message Queue │ │ │ │ Message 1 │ │ Message 2 │ │ Message 3 │ └───────────────────┘ | ↓ Process B
This is useful when sender and receiver don't have to operate at exactly the same time.
Example:
Producer ↓ Queue ↓ Consumer
This is a common pattern in operating systems and distributed applications.
4. Shared Memory
As discussed earlier:
Process A ──┐ ↓ Shared Memory ↑ Process B ──┘
It is very efficient for large data transfers.
But synchronization is essential.
5. Semaphores
A semaphore is primarily a synchronization mechanism rather than a general-purpose data-transfer mechanism.
It helps control access to shared resources.
Suppose there is one resource:
Resource ↑ Semaphore ↑ Process A / Process B
A semaphore can ensure that only the appropriate number of processes enter a critical section.
For a binary semaphore:
0 → unavailable 1 → available
Common conceptual operations:
wait() signal()
6. Signals
A signal is a notification sent to a process to inform it that an event has occurred.
For example, on Linux:
kill -SIGTERM PID
This does not necessarily mean "kill immediately" despite the command name. It sends the SIGTERM signal, which requests termination.
Another common signal:
SIGINT
Often generated when you press:
Ctrl + C
Conceptually:
Process A | | Signal ↓ Process B
Signals are generally used for notification/control, not for transferring large amounts of data.
7. Sockets
A socket provides communication between processes.
They are especially important for communication over networks.
Computer A Computer B Process A Process B | | Socket A Socket B | | +──────── Network ────────────+
Sockets can also be used between processes on the same computer.
Examples:
- Web servers
- Chat applications
- Database servers
- APIs
- Distributed applications
For example:
Browser ↓ Socket ↓ Web Server
8. Memory-Mapped Files
A file can be mapped into a process's memory address space.
Multiple processes can potentially map the same file and use it as a communication mechanism.
Process A ──┐ ↓ Mapped File ↑ Process B ──┘
This can be useful for efficient sharing of persistent or file-backed data, with synchronization still required when concurrent writes are involved.
Producer-Consumer Problem
One of the most important examples of IPC is the Producer-Consumer problem.
There are two processes:
Producer ↓ Produces data ↓ Buffer ↓ Consumer ↓ Uses data
Example:
Producer → [A][B][C][D] → Consumer
The buffer may be shared memory or a queue.
Why Synchronization Is Needed?
Suppose the buffer is full:
[A][B][C][D]
The producer should wait.
If the buffer is empty:
[ ][ ][ ][ ]
The consumer should wait.
Therefore, we need synchronization.
This problem is commonly associated with:
- Shared memory
- Semaphores
- Mutexes
- Condition variables
IPC and Synchronization
Communication and synchronization are related but not exactly the same.
Communication
Means:
Exchanging information
Example:
A → "Hello" → B
Synchronization
Means:
Coordinating the order/timing of processes
Example:
A finishes ↓ B starts
IPC mechanisms can support both.
IPC Example: Print Spooling
Consider multiple applications sending documents to a printer.
Application 1 ──┐ Application 2 ──┤ Application 3 ──┼──→ Print Queue ──→ Printer Application 4 ──┘
The applications don't directly control the printer simultaneously.
Instead, they communicate through a queue managed by the system.
IPC Example: Browser and Server
Suppose a browser communicates with a web server.
Browser Process | ↓ Socket | ↓ Network | ↓ Socket | ↓ Web Server Process
The socket provides the communication interface.
IPC in Linux
Linux provides many IPC mechanisms, including:
Pipes Named Pipes (FIFO) Message Queues Shared Memory Semaphores Signals Unix Domain Sockets Network Sockets
Useful commands/tools include:
ipcs
This can display information about several System V IPC resources.
For example:
ipcs -q
can show message queues.
ipcs -m
can show shared-memory segments.
IPC Comparison
| IPC Mechanism | Main Purpose | Data Transfer | Synchronization | Typical Use |
|---|---|---|---|---|
| Pipe | Stream communication | Yes | Some | Parent/child communication |
| Named Pipe | Process communication | Yes | Some | Unrelated local processes |
| Message Queue | Message-based communication | Yes | Built around messages | Producer/consumer |
| Shared Memory | Fast data sharing | Yes | Requires separate synchronization | Large data |
| Semaphore | Synchronization | No/limited | Yes | Resource control |
| Signal | Notification | Very limited | Event notification | Process control |
| Socket | Process/network communication | Yes | Protocol-dependent | Client/server |
| Memory-Mapped File | Shared/file-backed data | Yes | Requires synchronization | Large/file-backed data |
Shared Memory vs Message Passing
This is an important exam question.
| Shared Memory | Message Passing |
|---|---|
| Processes share a memory region | Processes exchange messages |
| Very fast for large data | Can have communication overhead |
| Synchronization is required | Synchronization may be integrated into communication semantics |
| More complex to manage safely | Often simpler conceptually |
| Suitable for large amounts of data | Suitable for structured messages |
| Processes need access to shared region | Processes don't need to share memory |
Simple memory trick:
Shared Memory → Share the data
Message Passing → Send the data
IPC vs Threads
Don't confuse IPC with thread communication.
Threads
Threads within the same process naturally share:
Code Data Heap Resources
Process ├── Thread A └── Thread B ↓ Shared memory
Processes
Separate processes normally have separate address spaces:
Process A Process B │ │ Private Memory Private Memory │ │ └────── IPC ───────────┘
Therefore, IPC is particularly important for communication between processes.
Advantages of IPC
- Allows processes to exchange information
- Enables resource sharing
- Supports synchronization
- Helps create modular applications
- Enables client-server architectures
- Allows independent processes to cooperate
- Useful for distributed systems and networking
Disadvantages / Challenges
- Synchronization complexity
- Race conditions
- Deadlocks
- Data consistency problems
- Security and access-control concerns
- Communication overhead
- Debugging can be difficult
Complete IPC Picture
IPC | +---------------+---------------+ | | Shared Memory Message Passing | | +----+----+ +-------+-------+ | | | | | Memory Sync Pipe Queue Socket | Semaphore
Other mechanisms:
Signals Named Pipes Memory-Mapped Files
Exam Definition
Inter-Process Communication (IPC) is a set of operating system mechanisms that enables processes to exchange data and synchronize their activities. Common IPC mechanisms include pipes, message queues, shared memory, semaphores, signals, and sockets.
Quick Revision
IPC ↓ Processes communicate ↓ Exchange data + synchronize ↓ Two major models ├── Shared Memory └── Message Passing
Remember these 8 IPC mechanisms:
P M S S S S S M
- P — Pipes
- M — Message Queues
- S — Shared Memory
- S — Semaphores
- S — Signals
- S — Sockets
- S — Named Pipes
- M — Memory-Mapped Files