---
title: "pipe(); semaphore in distributed systems"
subject: "Operating System"
chapter: 3
key: "semester_6/operating_system:pipe-semaphore-distributed"
source: https://www.bctengineer.com/q/semester_6/operating_system/pipe-semaphore-distributed
site: BCT Engineering
---

# pipe(); semaphore in distributed systems

Operating System · Chapter 3: Process Communication and Synchronization

- Asked in: 2071 Magh, 3+4+3 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_6/operating_system/pipe-semaphore-distributed
- Whole chapter: https://www.bctengineer.com/md/semester_6/operating_system/chapter-3

## Question

Why do we need pipe() function? Define Semaphore and explain the major operations in semaphore. Can semaphores be used in distributed system? Explain why or why not.

## Short answer

pipe() creates a unidirectional inter-process communication channel, letting output of one process become input of another. A semaphore is an integer with atomic down and up operations for mutual exclusion and synchronization. Semaphores are not directly usable in distributed systems because there is no shared memory or common clock; they can be emulated with a central server or distributed mutual-exclusion algorithms.

## Answer

#### Why do we need pipe()?

`pipe()` is a UNIX system call that creates a unidirectional communication channel (a kernel buffer) between processes. It is needed for **inter-process communication**: it lets the output of one process become the input of another (as in `ls | wc`), usually between a parent and its child created by `fork`. It also synchronizes the processes, since a reader blocks on an empty pipe and a writer blocks on a full one.

```c
int fd[2];
pipe(fd);                 /* fd[0] = read end, fd[1] = write end */
if (fork() == 0) { close(fd[0]); write(fd[1], "hello", 6); }
else             { close(fd[1]); read(fd[0], buf, 6); }
```

#### Semaphore and its operations

A **semaphore** is an integer variable (introduced by Dijkstra) that can be accessed only through two atomic operations, `down` (P / wait) and `up` (V / signal). It is used for mutual exclusion and for synchronization of processes.

```c
down(S):  if (S > 0) S = S - 1;
          else block the calling process (put it on S's queue);
up(S):    if (some process is blocked on S) wake up one of them;
          else S = S + 1;
```
Both operations are indivisible: once started, no other process can access S until the operation finishes or blocks.

Initialised to 1 it acts as a mutex; initialised to n it counts n identical resources. Typical uses are mutual exclusion (`down(mutex)` ... `up(mutex)`) and synchronization (`down(full)`, `up(empty)` in producer-consumer).

#### Can semaphores be used in a distributed system?

**Not directly, but they can be emulated.** A normal semaphore is a shared variable whose `down` and `up` are made atomic by the OS of a single machine (by disabling interrupts, TSL or kernel locking). In a distributed system the computers have no shared memory and no common clock, and communicate only by messages with delays and failures, so there is no single place where atomic access can be guaranteed.

They can be implemented in two ways:
- **Central semaphore server**: one process owns the semaphore, and other machines send `down`/`up` request messages and wait for the reply. It is simple, but the server is a single point of failure and a bottleneck.
- **Distributed algorithms** (message-based mutual exclusion, e.g. Ricart-Agrawala, token ring or Lamport's algorithm based on logical clocks) that give the same effect.

So other mechanisms such as message passing, RPC and distributed mutual exclusion algorithms are used in practice.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What does pipe() provide?
   A. A shared memory segment
   B. A network socket
   C. A unidirectional kernel buffer channel for inter-process communication
   D. A new thread
   Answer: C. pipe() creates a one-way channel so one process's output becomes another's input, typically parent and child. (Key point: Purpose of pipe())

2. After pipe(fd), which descriptor is the write end?
   A. fd[0]
   B. fd[1]
   C. Both
   D. Neither
   Answer: B. fd[0] is the read end and fd[1] is the write end. (Key point: Pipe file descriptors)

3. Why are down and up made indivisible?
   A. To make them faster
   B. To avoid using memory
   C. So no other process can change S until the operation finishes or blocks
   D. To allow interleaving
   Answer: C. Otherwise two processes could change S simultaneously, creating a race on the semaphore itself. (Key point: Semaphore atomicity)

4. Why can ordinary semaphores not be used directly in a distributed system?
   A. Semaphores are too slow
   B. Computers have no shared memory or common clock, so atomic access cannot be guaranteed at one place
   C. Networks forbid integers
   D. Distributed systems have one CPU
   Answer: B. Machines communicate only by delayed, failure-prone messages, so there is no single place for atomic down/up. (Key point: Semaphores in distributed systems)

5. Which is a way to emulate semaphores in a distributed system?
   A. A central semaphore server reached by messages, with the drawback of a single point of failure
   B. Disabling interrupts on all machines
   C. Sharing a variable in RAM across machines
   D. Using TSL across the network
   Answer: A. One process owns the semaphore and others send down/up messages; it is simple but a bottleneck and single point of failure. (Key point: Emulating semaphores)

## Related in Operating System

- [TSL; semaphore operations as class](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Atsl-semaphore-class) — Operating System, chapter 3
- [Semaphore operations with pseudocode](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Asemaphore-operations-pseudocode) — Operating System, chapter 3
- [Semaphore for mutual exclusion](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Asemaphore-enforce-mutual-exclusion) — Operating System, chapter 3
- [Producer-consumer using semaphore](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Apc-semaphore-mutual-exclusion) — Operating System, chapter 3, asked 2×
- [Producer-consumer: semaphore and messages](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Apc-semaphore-message-passing) — Operating System, chapter 3
- [Monitor; dining philosophers with semaphore](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Amonitor-dining-philosopher) — Operating System, chapter 3

## Related in other subjects

- [Need for difference equation; LCCDE and system function](https://www.bctengineer.com/question.md?key=semester_7%2Fdigital_signal_analysis_and_processing%3Awhy-difference-equation-lccde) — Digital Signal Analysis and Processing, chapter 1, asked 2×
- [Why do we need DFT](https://www.bctengineer.com/question.md?key=semester_7%2Fdigital_signal_analysis_and_processing%3Awhy-need-dft) — Digital Signal Analysis and Processing, chapter 7, asked 6×
- [Why do we need FFT](https://www.bctengineer.com/question.md?key=semester_7%2Fdigital_signal_analysis_and_processing%3Awhy-need-fft) — Digital Signal Analysis and Processing, chapter 7, asked 2×
- [Major discontinuity systems of Nepal Himalaya](https://www.bctengineer.com/question.md?key=semester_4%2Fengineering_geology_ii%3Anepal-discontinuity-systems) — Engineering Geology II, chapter 2, asked 12×
