Skip to content
All projects

Systems programming lab, 2023

Unix Shell

A command interpreter in C built straight on the Linux process API. It runs pipelines with redirection, tracks and signals every process it starts, and keeps a history you can replay.

A terminal running the shell next to its test suite. The shell suspends a background looper process, runs a four-stage pipeline, lists its processes and replays a command from history. All 50 tests pass.
  • Pipelines of any length, joined with pipe and dup2, with redirection allowed only where it makes sense.
  • A built-in process manager that suspends, wakes and interrupts jobs, with statuses read back from waitpid.
  • 50 end-to-end tests drive the real shell and signal the processes it prints, plus a valgrind leak check on every push.

What it is

A shell is the program that turns a line like cat < notes.txt | sort | uniq -c into running processes. This one is written in C on nothing but the system calls underneath: fork to make a process, execvp to turn it into another program, pipe and dup2 to connect them, waitpid to learn what happened to them and kill to send them signals.

I wrote it in 2023 over two labs of the Extended Systems Programming Lab at Ben-Gurion University. The first lab covered running programs, redirection and signals, and the second added pipelines, a process manager and history.

How it runs a line

Built-ins stay in the shell. cd has to change the shell’s own directory, so it can’t run in a child. The same goes for procs, history and the signal commands. Everything else is forked.

One child per stage. For a | b | c the shell creates a pipe between each pair of neighbours, forks once per stage and points each child’s stdin and stdout at the right ends with dup2. Redirection is applied last, so < on the first stage and > on the last work as you’d expect.

Bad pipelines never start. Redirecting the output of a stage that feeds a pipe would leave the pipe empty. Redirecting the input of a stage that reads from one would leave it hanging. Both are rejected before anything forks, and so is a line with no command at all.

On the left, a standalone program wires ls -l into tail -n 2 by hand and narrates every step. On the right, the shell rejects pipelines that redirect in the wrong place and replays commands from history.

Managing processes

Every process the shell forks goes into a linked list with its status. procs prints the list, suspend, alarm and blast send SIGTSTP, SIGCONT and SIGINT, and a process that has finished is shown once and then forgotten.

The status never comes from guessing. Before each prompt the shell asks waitpid about every live process with WNOHANG | WUNTRACED | WCONTINUED. That one call reports exits, stops and resumes, so even a process signalled from outside the shell shows the right state, and finished background jobs are collected instead of piling up as zombies.

Ctrl-C and Ctrl-Z work like in a real shell. The shell ignores both, so they reach only the job in the foreground. A job stopped with Ctrl-Z hands the prompt back and waits as Suspended until alarm wakes it, and background jobs live in their own process group so Ctrl-C at the prompt can’t touch them.

Details that matter

A pipe only ends when every writer is gone. The reader sees end-of-file only once every copy of the write end is closed. That includes the copies the shell and the other children inherited from fork. Forget one close and wc waits forever.

Ignored signals survive exec. exec resets handled signals to their default but leaves ignored ones ignored. Since the shell ignores Ctrl-C, its children would too, so each child restores the default before exec.

A handler can’t simply re-raise its own signal. The test program looper reports a signal and then passes it on to the default action. While a handler runs, the kernel blocks that same signal, so a plain raise would only leave it pending, and the re-armed handler would fire again forever. looper unblocks the signal first, and a test covers it.

Flush before you fork. When the shell’s output goes to a file instead of a terminal, its text can sit in a buffer while a child writes straight to the file. Replaying !3 would then print the command after its own output. The shell flushes before every fork, and a test checks the order.

Testing

The tests drive the real binary. Most feed it a few lines and check what it prints. The process tests keep a live shell open through a named pipe. They start a looper in the background, read its PID from the shell’s output, and then suspend, wake and interrupt it while checking procs after each step.

  • redirection truncates output files and never creates a missing input file
  • pipelines of up to five stages finish, and misplaced redirection starts no process
  • history keeps exactly the newest 20 lines, and !! and !n replay them
  • a stopped foreground job returns the prompt, and background jobs get their own process group
  • a full session under valgrind ends with no leaks

GitHub Actions builds with warnings as errors and runs the whole suite on every push.