Processes and Pipes Lab
Retrieval Prompts
- Write the three-step lifecycle of a child process started by a parent: what calls, in what order, in which process?
- Explain why
forkreturns twice, and state the return value in the parent vs the child. - In a shell pipeline
A | B, which fd is duped onto which standard fd, in which process? - State the rule for closing pipe ends in the parent. Why does forgetting it hang the downstream process?
- Explain the difference between a zombie and an orphan.
Compare and Distinguish
Separate these pairs clearly:
forkvsexecexit(0)vs_exit(0)pipe()vssocketpair()dup(fd)vsdup2(fd, 1)waitpid(pid, ...)vswait(...)
Common Mistake Check
For each snippet, identify the bug:
- A child calls
exit(1)after a failedexecvp, flushing the parent's stdio buffers twice. - A parent forks
A | B | C, forgets to close all three pipe ends in itself, andCnever sees EOF. - A
SIGINThandler callsprintffrom the main loop's middle. - A program spawns 100 children and never calls
waitpid; the process table fills up. - A
fork/execprogram closes fd 1 in the parent, then opens a file, expecting fd 1 to point at it -- forgetting the child had already inherited the old fd 1.
Mini Application: Build a One-Stage Pipeline
Implement a program runpipe A B that runs A | B as a pipeline. Requirements:
- Use
pipe,fork,dup2,execvp,waitpid. - Close both pipe ends in the parent before waiting.
- Print the exit status of both children.
- Fail cleanly if either
execvpfails (use_exit(127)).
Verify on runpipe ls 'wc -l' (or with the arguments split).
Mini Application: Zombie Safari
Write a program that forks 5 children. Each sleeps a different number of seconds (1-5), then exits with its index as exit code. The parent must reap all of them in a SIGCHLD handler that loops waitpid(-1, ..., WNOHANG). Demonstrate that no zombies are left at the end (use ps in a second terminal during the run).
Mini Application: Redirection
Write a program that runs ls -l with stdout redirected to out.txt and stderr redirected to err.txt, without using the shell. Close the original fds before exec. Confirm the files contain the expected content.
Scenarios
For each, answer:
- Which syscall's failure mode is at issue?
- What symptom would the user see?
- What is the smallest fix?
- What diagnostic tool would confirm the fix?
Scenarios:
- A shell pipeline
cmd | grep foocompletescmdbutgrephangs indefinitely. - A server
forks per connection, runs for a week, andpsshows thousands of<defunct>entries. - A daemon's Ctrl-C handler sets a flag, but the daemon never exits because the main loop is blocked in
read. - A subprocess library deadlocks because it forgot
FD_CLOEXECand the exec'd program inherits a pipe neither side closes. - A child process inherits a
stdoutFILE *from the parent,forks, both callprintf, and the same line appears twice on screen.
Evidence Check
This lab is complete when you can, for each scenario, say which syscall sequence produced the symptom and what strace -f would show.