Part 1 of 6 · 1 min read
Why this page comes fourth
In this module you will
- Describe what a shell does as a loop of read, parse, run and wait.
- Write a working shell of about 50 lines of C.
- Explain why
cdandexitmust be built into the shell and cannot be separate programs. - Implement output redirection with
openanddup2betweenforkandexec. - Read the exit status of a command and report failures.
Page 3 introduced fork, exec and wait as separate calls. A shell is the program that uses all three all day. Building one is the best test of whether the three calls are understood, and it explains why Unix keeps fork and exec apart. Page 6 adds signals to the shell, and page 7 adds pipes. The main sources are "Operating Systems: Three Easy Pieces" (OSTEP), chapter 5, "The Linux Programming Interface" (TLPI), chapters 24 to 27, and "Operating System Concepts" (OSC), chapter 3. All code was compiled with -Wall -Wextra and run.
About this example About this example
Real: The C programs, commands and outputs on this page were compiled with gcc -Wall -Wextra -pthread and run on a Linux test machine (2 processors, kernel 6.18, glibc 2.39).
Scripted: The worked traces use small numbers chosen so they can be followed by hand.
Not real: The page simplifies real systems. Actual kernels add more cases, limits and tuning than a short page can show.