Inter Process Communication (IPC)
Inter Process Communication (IPC) Overview
Inter Process Communication (IPC) is a mechanism that enables processes to communicate and synchronize their actions when executing concurrently.
Two processes might need to share data or send information to each other for tasks such as printing a file.
The operating system supports several IPC mechanisms, including:
File
Shared Memory
Message Passing
Pipe
Signal
Remote Procedure Call (RPC)
IPC Mechanisms
Files
Definition: Processes can share data through common files by reading from and writing to them.
Issue: Synchronization may be necessary to prevent race conditions—when two processes attempt to update the same file simultaneously.
Shared Memory
Definition: A portion of the main memory is accessible for multiple processes.
Characteristic: Changes made to a memory location in shared memory by one process are instantly visible to all processes having access to that memory.
Implementation: Achieved by mapping parts of the address spaces of multiple processes to the same segment of main memory.
Example Illustration
Execution of Process P and Process Q
A variable
x
might be modified in memory, which would be immediately visible to other processes.
Message Passing
Definition: Allows a process to send messages to another process asynchronously, like postal mail.
Mechanism: Messages are stored in a message queue until the recipient requests to receive them.
System Calls:
Sending:
send
,sendmsg
, orsendto
system calls.Behavior: Returns after copying message contents to a system buffer.
Receiving:
recv
,recvmsg
, orrecvfrom
system calls.Behavior: Blocks until a message is available if the queue is empty.
Message Passing Types
Synchronous: Both sender and receiver must be ready for communication (also called rendezvous).
Benefits: Easier debugging and reasoning about correctness, though may lower concurrency due to blocking.
Fully Asynchronous: Neither sender nor receiver blocks. The sender continues even if there is no message available, increasing concurrency but putting more onus on the programmer.
Example Illustration
Execution of Process P sends “Hello!” to Process Q.
System states may involve copying to a system buffer and signals indicating readiness at both ends.
Pipes
Definition: Implements a producer-consumer model where one process produces data and another consumes it.
Characteristics: FIFO (First In, First Out) manner for data consumption.
Types of Pipes:
Unnamed Pipes:
Characteristics: Can only be used by related processes, lacks an explicit handle, typically supports unidirectional communication.
Named Pipes:
Characteristics: Offers an explicit handle, can be used by unrelated processes, typically supports bidirectional communication.
Pipe Examples
To illustrate unnamed and named pipes, assume processes are executing commands that share data through each pipe, starting from writing to buffer to reading it by another process.
Remote Procedure Calls (RPC)
Definition: Allows procedures to be called as if they are local, while they may be executed on another machine.
Benefit: Abstracts communication details, making it simple for programmers; utilizes message passing for implementation.
Illustration: The client invokes a remote procedure call, sends a request, and might be blocked until the server responds.
Signals
Definition: A mechanism for processes to asynchronously notify each other of events, such as termination or completion of tasks.
Signal Handling: Each signal has a default action which can be overridden by installing a signal handler.
Example Usage: In UNIX and Linux, signals can be sent from a parent process to a child process or vice-versa.
Advantages and Disadvantages of Signals
Advantages:
Low overhead and easy to implement without needing the target process to handle signals explicitly.
Disadvantages:
Ensuring correctness with signal handlers can be complex due to potential data races and deadlocks.
IPC Pros and Cons Summary
Files:
Pros: Persistent data exists beyond process lifespan.
Cons: Involves high overhead due to I/O operations.
Shared Memory:
Pros: Low overhead after setup.
Cons: Vulnerable to race conditions requiring synchronization.
Message Passing:
Pros: Easier debugging compared to shared memory.
Cons: High overhead due to multiple system calls for each message.
Pipes:
Pros: Natural communication model for producer-consumer scenarios.
Cons: Similar overhead concerns as other IPC methods.
RPC:
Pros: Simplifies communication between different machines.
Cons: Ensuring exactly-once execution semantics is challenging due to failures.
Signals:
Pros: Simple and low overhead.
Cons: Complexity in handler implementation.