Skip to content
/ posh Public

Interactive POSIX shell supporting piping, I/O redirection, and background processing

Notifications You must be signed in to change notification settings

n5q/posh

Repository files navigation

PoSH

Design Choices

PoSH was implemented with modularity in mind. Each file handles a responsibility:

  • posh.c initializes the shell, parses input, and coordinates execution.
  • exec.c manages external command execution and pipelines.
  • posh_cmds.c implements built-in commands (cd, exit, jobs, fg, bg).
  • jobs.c provides job tracking, background/foreground handling, and status updates.
  • posh_signals.c sets custom signal handlers for SIGCHLD, SIGINT, and SIGTSTP to support job control.

For job control, a linked list of jobs was chosen to easily add, remove, and update processes. Built-ins were implemented separately from external execution to simplify parsing. Signal handling was designed to avoid race conditions by blocking signals during critical job table updates. Pipes and redirection were integrated into exec.c using fork(), dup2(), and pipe() for straightforward chaining.

System Calls Used

Core Shell Features

  • fork(): Create child processes for external commands
  • execve(): Execute external programs
  • waitpid(): Wait for foreground processes and reap background processes
  • chdir(): Change directory (cd command)
  • getcwd(): Get current directory (pwd command)
  • getline(): Read user input line

I/O Redirection

  • open(): Open files for redirection
  • dup2(): Duplicate file descriptors for stdin/stdout redirection
  • close(): Close file descriptors

Pipes

  • pipe(): Create pipe file descriptors
  • dup2(): Redirect stdin/stdout to pipe ends

Signal Handling

  • sigaction(): Set up handlers for SIGINT, SIGTSTP, SIGCHLD
  • signal(): Ignore SIGQUIT
  • kill(): Forward signals to foreground processes and terminate children

Background Jobs

  • waitpid() with WNOHANG: Non-blocking reaping of terminated background processes
  • waitpid() with WUNTRACED: Detect stopped processes for job control

Testing

Basic Commands

  • Tested builtin commands: cd, pwd, jobs, exit
  • Verified error handling for invalid paths and missing arguments
  • Tested external commands with relative paths (e.g., ./program)

I/O Redirection

  • Input redirection: ./cat < input.txt
  • Output redirection: ./echo hello > output.txt
  • Combined: ./cat < input.txt > output.txt
  • Tested syntax error detection (missing filenames)

Pipes

  • Simple pipes: /bin/sleep 6 | /bin/sleep 7
  • Verified both processes execute correctly
  • Tested Ctrl+Z on piped commands

Background Execution

  • Ran commands with &: /bin/sleep 10 &
  • Verified PID output and job list tracking
  • Tested multiple background jobs simultaneously
  • Confirmed jobs command shows correct state

Signal Handling

  • Ctrl+C: Tested with foreground processes (terminates child, not shell) and without (no action)
  • Ctrl+Z: Suspended foreground processes, verified they appear in jobs list as 'T' (stopped)
  • SIGCHLD: Confirmed automatic cleanup of terminated background jobs

Memory Management

  • Verified proper cleanup on shell exit using cleanup_children() and job_free_all()
  • Tested for memory leaks with background jobs and pipes
  • Ensured dynamically allocated pipe argv arrays are freed

Sources

  • Linux man pages: Reference for system call specifications
  • CMPUT 379 Course materials (Textbook + Lecture notes)

About

Interactive POSIX shell supporting piping, I/O redirection, and background processing

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •