CSE Roadmap

Core tier · about 3 hours · 6 parts

Part 6 of 6 · 3 min read

Exercises

Easy

Easy 1: Which end (20 minutes)

In ls | wc, say which program holds the write end of the pipe and which holds the read end, and which descriptor number each uses.

Done when: ls writes through descriptor 1 and wc reads through descriptor 0.

Hint

Descriptor 1 is standard output, descriptor 0 is standard input.

Easy 2: End of file (20 minutes)

A reader loops on read until it returns 0. The parent forgot to close its own write end. What happens at the end, and why?

Done when: you say the loop never ends, because the kernel still counts a writer.

Hint

The reader's own copies also count.

Easy 3: Pick the channel (20 minutes)

Pick a channel for each: a parent sending a result to a child, two unrelated daemons on one machine, a browser talking to a web server, and one process telling another to reload its settings.

Done when: a pipe, a named pipe or socket, a socket, and a signal.

Hint

A signal carries only its number.

Medium

Medium 1: Run the pipe program (30 minutes)

Compile and run the pipe example. Then remove close(fd[1]) from the parent and run again. Describe and explain the difference.

Done when: the second run hangs after the first line, and a sentence explains that the parent is still a writer.

Hint

Use ls /proc/PID/fd to see the stray descriptor.

Medium 2: Build a three-stage pipeline (30 minutes)

Write a program equivalent to cat /etc/passwd | grep root | wc -l. Use two pipes.

Done when: the output equals the shell version, and no stray descriptors stay open (check with ls /proc/PID/fd while it runs, if you add a sleep).

Hint

Two pipes, three forks, and close all unused ends in every process.

Medium 3: Measure the capacity (30 minutes)

Run the non-blocking test to find the capacity of a pipe on your machine. Then write 70,000 bytes into a pipe with no reader running in blocking mode and observe what happens.

Done when: a capacity figure (65,536 on many machines), and a note that the blocking write stops at that figure.

Hint

fcntl(fd, F_SETFL, O_NONBLOCK) on the write end.

Hard

Hard 1: Add pipes to the shell (60 minutes)

Extend the shell of page 4 so that cmd1 | cmd2 works for exactly one pipe. Test ls /etc | wc -l and echo hi | cat.

Done when: both commands print the correct output and the shell returns to its prompt without hanging.

Hint

Split the line at | before splitting words.

Hard 2: Throughput of a pipe (60 minutes)

Write a parent that sends 100 MB through a pipe to a child that discards it, in blocks of 4,096 bytes and then of 65,536 bytes. Time both. Compute the rate in MB/s and explain the difference.

Done when: two rates, and an explanation based on fewer system calls (page 2 of Level 0 showed the cost of each call).

Hint

Use clock_gettime and divide bytes by seconds.

Hard 3: Break the pipe (60 minutes)

Write a writer that writes forever into a pipe and a reader that reads 1,000 bytes and exits. Show the writer ending by SIGPIPE, then change it to ignore the signal and show the EPIPE error.

Done when: a wait status with signal 13 in the first case and the error text "Broken pipe" in the second.

Hint

signal(SIGPIPE, SIG_IGN) turns the signal into an error.