Master the fundamental concepts of inter-process communication through this focused micro-challenge.
A shell pipeline wires stdout of command A to stdin of command B via pipe(), fork(), and dup2(). Each child closes unused fd ends so reads block correctly instead of hanging on the wrong descriptor.
Parent for cmd1 | cmd2:
pipe(pfd)dup2(pfd[1], 1), exec cmd1dup2(pfd[0], 0), exec cmd2For example, ls | wc -l connects listing bytes into wc stdin without temporary files.
This is literally how bash, zsh, and dash implement cmd1 | cmd2 | cmd3 internally, using pipe(), fork(), and dup2() in exactly the sequence you'll write here. Forgetting to close the pipe's write end in every process that isn't writing to it is a real, well-documented bug class that causes pipelines to hang forever because the reading command never receives EOF, which is why shell source code is meticulous about closing every unused descriptor after each fork.
Before you call the implementation done, walk failure modes on purpose. Test empty structures, single-element edge cases, maximum concurrency, and errno paths that must not crash the program. OS code usually fails in production when happy-path tests pass but invariants break under contention or memory pressure.
Keep structures small and name fields after kernel counterparts when possible. That lets you read man pages and kernel source side by side while you work. Print observable events during development; remove noisy logs once tests pass reliably.
You will parse two commands and a pipe token, then wire fds before execve. The task asks you to reap both children and propagate exit status of the last command in the pipe.
Implement shell pipeline connecting two commands.
Requirements:
Test:
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
All starter code and reference implementations are available for your local setup.
View on Github