SUShell is a simplified UNIX shell developed in C that simulates core operating system functionalities. It manages the execution of system commands, handles input/output redirection, and implements a robust pipeline architecture, including a unique "LoopPipe" structure for repetitive command execution.
Process Hierarchy Management: SUShell operates as a parent process that orchestrates the creation and synchronization of child processes using fork(), execvp(), and wait().
Sequential Pipelining: For a pipeline of n commands, the shell creates n-1 unidirectional communication channels (pipes).
LoopPipe Implementation: A specialized feature that flattens complex structures (Before | Loop | After) into a single linear pipeline before execution.
I/O Redirection: Supports input (<) and output (>) redirection by manipulating file descriptors via dup2().
Concurrent Execution: Commands in a pipeline are forked sequentially but run in parallel, synchronized naturally by the kernel's data availability on the pipes.
When a pipeline command is entered, the shell forks children sequentially from left to right. Each command runs as an independent child process. The parent process is responsible for:
- Arranging pipes for correct data flow.
- Setting up redirection with dup2().
- Closing unused pipe ends to ensure EOF propagation and avoid leaks.
The shell relies on UNIX pipes' implicit synchronization. No explicit locking is required because: Read Blocking: A process attempts to read from an empty pipe and blocks until data is written.
Write Blocking: A process attempts to write to a full pipe and blocks until the next command reads.
The LoopPipe structure allows a set of commands to be repeated while maintaining a continuous data flow.
Flattening: The shell flattens the entire command line into a single unified pipeline prior to execution.
Example: A structure like A | (B | C)_2 | D is expanded to A | B | C | B | C | D.
Input Handling: Input can originate from a redirection file, a preceding pipeline, or the standard terminal.