Linux IPC and Networking: Pipes, Sockets and a Worked Request-Reply Example

Choose IPC by scope, data shape and synchronization, then trace a 16-byte request-reply exchange through two pipes and TCP.

KnowledgeGate Team

Exam prep & CS education

Updated 4 Dec 20265 min read
Linux IPC and Networking: Pipes, Sockets and a Worked Request-Reply Example

Choosing between pipes, shared memory and sockets requires more than naming mechanisms. Scope, data shape, synchronization and failure handling decide the fit. The same newline-framed request-reply exchange below moves through two anonymous pipes and a TCP stream, exposing descriptor ownership, partial reads and EOF.

1. IPC and networking are one communication problem at two scopes

IPC lets isolated processes exchange data or coordinate. Local IPC stays on one host; networking may cross hosts. Unix-domain sockets are local, while Internet-domain sockets can be remote.

Choose by process relationship, stream versus message data, synchronization ownership, direction and reach. A parent can send seven bytes to its child through a pipe; unrelated local services can use a FIFO or Unix-domain socket; TCP can carry the same application protocol across hosts. CS Fundamentals for Exams and Placements places these mechanisms in the wider syllabus.

Linux OS: Kernel, Processes, Memory and File Systems uses a one-way shell pipeline inside a whole-system tour. The bidirectional case needs two pipes and explicit closure of four unused endpoints before the same byte protocol moves to a socket.

For RPC retries, deduplication and delivery semantics, use Communication Protocols in Operating Systems and Distributed Systems. At the local layer, descriptor ownership, framing, partial I/O and EOF determine whether the same byte exchange works through pipes and TCP.

2. The Linux IPC toolkit: match the mechanism to the contract

mechanism

scope

data model

typical strength

main trap

Anonymous pipe

Related processes

Byte stream

Parent-child flow

No message boundaries

FIFO

Unrelated local

Pathname byte stream

Named channel

Open order can block

Message queue

Local named kernel object

Discrete messages

Message units

Capacity, cleanup

Shared memory

Local

Mapped bytes

Avoids pipe-style copying after setup

Needs synchronization

Signal

Local

Notification

Small event channel

Not data transport

Semaphore

Local

Synchronization state

Coordinates access

No payload

Unix-domain socket

Local

Stream or datagrams

Full duplex

Linux-specific abstract naming

TCP socket

Local/remote

Byte stream

Reliable full duplex

Needs framing

UDP socket

Local/remote

Datagrams

Message boundaries

No delivery/order guarantee

The kernel buffers pipes, FIFOs, message queues and sockets. Shared memory is accessed directly. It avoids repeated pipe-style copying but adds synchronization and lifecycle work, so it is not always fastest.

If a producer writes value = 1369 before ready = 1, an unsynchronized consumer can observe an invalid state. Process Synchronization and Semaphores explains the missing ordering layer.

3. Pipe descriptors, fork() and EOF: follow ownership before data

Fork diagram shows parent and child descriptor ownership, close actions and the EOF consequence of an inherited writer.

Assume descriptors 0, 1 and 2 are already open. Parent PID 4200 creates p2c = {3,4} and c2p = {5,6}, then forks child PID 4201. In both arrays, index [0] is the read end and [1] is the write end.

Both processes initially hold descriptors 3 through 6. The parent closes 3 and 6, keeping request writer 4 and response reader 5. The child closes 4 and 5, keeping request reader 3 and response writer 6. Each number identifies an entry in that process's descriptor table; fd 4 in the parent is not a system-wide handle.

Linux pipe(7) defines a boundary-free byte stream. EOF needs every write-end descriptor closed, so one inherited writer can cause an indefinite wait.

4. Worked example: compute 37 squared through two pipes

Two-pipe trace follows REQ 37, the 37 squared computation, RES 1369, byte counts, parsing and EOF closures.

The parent sends REQ 37\n, seven ASCII bytes: R E Q space 3 7 newline. The child computes 37 × 37 = 1369 and returns RES 1369\n, nine bytes: R E S space 1 3 6 9 newline. Payload is 7 + 9 = 16 bytes.

One valid trace:

  1. write(4, request, 7) returns 7.

  2. Fd 3 yields REQ (3), 3 (2), 7\n (2). Parsing waits for newline after 3 + 2 + 2 = 7.

  3. After computing 1369, write(6, response, 9) returns 9.

  4. Fd 5 yields RES (4), 1369\n (5). At 4 + 5 = 9, parent verifies tag and value.

Closing fd 4 lets the child get 0 after seven bytes because its copy is closed. Closing fd 6 gives the parent EOF after nine. Both close remaining readers.

5. Carry the same protocol over a socket

A Unix-domain SOCK_STREAM connection or socketpair() replaces two pipes with one full-duplex stream. Linux socket(7) still requires newline framing because streams preserve no application boundaries.

Server PID 5100 listens on 127.0.0.1:5050; client PID 5200 connects from local port 43000. A real client can use any ephemeral port assigned by the operating system. accept() creates a connected descriptor while the listening descriptor remains open, and the connection carries both lines.

Linux tcp(7) specifies a reliable, ordered, full-duplex byte stream. Payload remains 16 bytes; headers, acknowledgements and possible retransmissions add network traffic.

6. Blocking, partial I/O and concurrency determine whether the design works

Append positive reads until newline, handle interruption or error separately, and treat 0 as EOF. If write() accepts fewer than 7 or 9 bytes, advance an offset and call it again; the full-length returns in the numbered trace are one valid outcome.

With three clients and 200 ms sequential work each, the third starts after roughly 200 + 200 = 400 ms. Process-per-connection or thread-per-connection designs overlap work but add resource, synchronization and cleanup concerns.

A no-data read may block while a writer remains. All writers closing produces EOF after buffered data. Per pipe(7), no readers causes a broken pipe. TCP can close its sending direction while continuing to receive. EOF, timeout, refusal and reset differ.

7. IPC traps and the ways exams test them

Remember: an ordinary pipe is one-way; fd 4 is table-local; one write() need not equal one read(); TCP orders bytes, not messages; UDP preserves datagrams without guaranteeing delivery or order; shared memory needs mutual exclusion; semaphores carry no REQ 37\n; accept() returns a connected socket.

Exam questions test mechanism choice, post-fork() descriptor ownership, blocking and EOF, payload totals, transport type, the descriptor returned by accept(), and the synchronization missing from a shared-memory design.

Checks: after all request writers close and seven bytes drain, the next read returns 0; payload is 16 bytes; two pipes handle opposite directions.

8. Short version and the next learning step

Choose scope, data shape and direction. Then define framing, synchronization, closure and failures. Two pipes carry 7 and 9 bytes, totalling 16, and 37 × 37 = 1369.

For a structured route through operating systems and computer networks, use Zero to Hero: Complete CS Course.

Repeat the byte trace with REQ 42\n and RES 1764\n. The request is 7 bytes, the response is 9 bytes, and the total application payload remains 16 bytes because 42 × 42 = 1764.