Mini Shell (C)
A small but capable POSIX-style shell
Overview
This group project implements a simple shell in C that runs on openlab with GCC 11. It supports built-in commands, executes local binaries in the foreground or background, manages jobs with signals, and handles input and output redirection.
Features
- Built-ins:
cd,pwd,jobs,bg,fg,kill,quit. Built-ins run in the shell process.jobslists running and stopped background tasks.fgandbgaccept either%JIDorPID. - Foreground execution: spawn with
fork, replace withexecvorexecvp, andwaitpidfor completion. Ctrl-C sendsSIGINTto the foreground child only. - Background execution: append
&to run jobs without blocking the prompt. Children are reaped by aSIGCHLDhandler. - Job control: track
pid, status, and command line in a job table. Ctrl-Z stops the foreground job withSIGTSTP,fgandbgsendSIGCONT.killterminates by id or pid. - I/O redirection:
<,>, and>>are parsed and applied viaopenanddup2with the required modes. One input and one output redirect at most, as specified. - Parsing: whitespace-tokenized argv with background and redirection handling. Both absolute and PATH-based execution are supported through
execvandexecvp.
Architecture
- Parser:
parselinetokenizes the command line, detects&, and resolves optional<,>,>>into file descriptors stored for laterdup2. - Built-ins:
buildin_commandimplements control-flow commands directly.jobs,fg,bg, andkillread or update the job table. -
Signal layer:
-
SIGINThandler targets the foreground child. -
SIGTSTPstops the foreground child and updates status to Stopped. -
SIGCHLDreaps finished background jobs without blocking.
-
- Evaluator:
evalwires parsing, built-ins, forking, redirection, and waiting. For background jobs, it prints the pid and returns the prompt immediately.
How to Build and Run
# Build the shell
gcc -o shell hw2.c
# Optional helpers from the assignment packet
gcc -o counter counter.c # prints a line every second
gcc -o add add.c # prints n+2 for input n
Examples
prompt > pwd
/home/uci-user
prompt > cd ..
prompt > counter
Counter: 0
Counter: 1
^C
prompt > counter &
12345 counter &
prompt > jobs
[1] (12345) Running counter &
prompt > kill %1
prompt > jobs
Redirection
# number.txt contains: 40
prompt > add < number.txt > added_number.txt
prompt > cat added_number.txt
42
The above follows the assignment’s redirection contract.
Notes on Design Choices
- The shell accepts both
/bin/lsstyle absolute commands andlsresolved viaPATH. This is achieved by tryingexecvfirst, thenexecvp. - Foreground and background states are tracked in a fixed-size job array with pid, status text, and the original command line, which makes
jobs,fg, andbgstraightforward.