Inter-process communication (pipes, shared memory) in Operating Systems - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When processes talk to each other using pipes or shared memory, the time it takes depends on how much data they exchange.
We want to understand how the time grows as the amount of data increases.
Analyze the time complexity of the following code snippet.
int fd[2];
pipe(fd); // create pipe
char buffer[SIZE];
for (int i = 0; i < SIZE; i++) {
buffer[i] = 'a';
}
write(fd[1], buffer, SIZE); // write data to pipe
read(fd[0], buffer, SIZE); // read data from pipe
This code creates a pipe, fills a buffer, then writes and reads data through the pipe.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop filling the buffer and the write/read calls that transfer data.
- How many times: The loop runs SIZE times; write and read handle SIZE bytes.
As the buffer size grows, the time to fill, write, and read grows proportionally.
| Input Size (SIZE) | Approx. Operations |
|---|---|
| 10 | About 10 steps to fill + 10 to write + 10 to read = 30 steps |
| 100 | About 100 + 100 + 100 = 300 steps |
| 1000 | About 1000 + 1000 + 1000 = 3000 steps |
Pattern observation: The total work grows directly with the size of data; doubling data roughly doubles the time.
Time Complexity: O(n)
This means the time to communicate grows in a straight line with the amount of data sent.
[X] Wrong: "Sending data through a pipe or shared memory always takes constant time regardless of size."
[OK] Correct: Actually, the time depends on how much data is sent; bigger data means more time to copy and transfer.
Understanding how communication time grows helps you design efficient programs and explain system behavior clearly.
"What if we used multiple smaller writes instead of one big write? How would the time complexity change?"
Practice
Solution
Step 1: Understand what a pipe does
A pipe is used to send data in a continuous stream from one process to another, allowing communication.Step 2: Compare with other options
Shared memory allows direct access to the same data, encryption is unrelated, and process creation is a different concept.Final Answer:
A channel that sends data in a stream from one process to another -> Option DQuick Check:
Pipe = Stream data channel [OK]
- Confusing pipes with shared memory
- Thinking pipes create processes
- Assuming pipes encrypt data
Solution
Step 1: Recall the pipe function signature
The pipe function requires an integer array of size 2 passed by reference to store file descriptors.Step 2: Match the correct syntax
The correct syntax is pipe(fd); where fd is an integer array of size 2 declared before the call.Final Answer:
pipe(fd); -> Option BQuick Check:
pipe needs int array of size 2 [OK]
- Omitting the type in the argument
- Passing pointer instead of array
- Passing array without size
1. Create shared memory segment 2. Process A writes value 10 to shared memory 3. Process B reads value from shared memory 4. Process B writes value 20 to shared memory 5. Process A reads value from shared memoryWhat value will Process A read in step 5?
Solution
Step 1: Track writes and reads in shared memory
Process A writes 10, then Process B reads 10, then Process B writes 20.Step 2: Determine what Process A reads after Process B's write
Since shared memory is common, Process A will read the updated value 20.Final Answer:
20 -> Option AQuick Check:
Shared memory shows last written value [OK]
- Assuming Process A reads its own old value
- Thinking reads cause errors
- Confusing shared memory with pipes
Solution
Step 1: Understand pipe blocking behavior
A reading process blocks if no data is available to read from the pipe.Step 2: Identify the cause of blocking
If the writing process has not sent data, the reader waits indefinitely for input.Final Answer:
The writing process has not sent any data yet -> Option CQuick Check:
Reader blocks if no data sent [OK]
- Blaming syntax errors for blocking
- Confusing pipe with shared memory
- Assuming buffer size causes blocking
Solution
Step 1: Analyze requirements for sharing large data structure
Efficient sharing with read/write access means processes need direct access to the same memory.Step 2: Compare IPC methods
Pipes stream data but are unidirectional and less efficient for large shared data. Message queues and sockets add overhead and are for message passing, not direct shared access.Final Answer:
Use shared memory because it allows direct access to the same data -> Option AQuick Check:
Shared memory = direct, efficient data sharing [OK]
- Choosing pipes for large data sharing
- Confusing message queues with shared memory
- Thinking sockets are best for local IPC
