This project implements a POSIX-compliant command-line shell in C++. It supports command execution, pipelines, I/O redirection, background processes, and built-in commands using POSIX system calls such as fork(), execvp(), pipe(), dup2(), and waitpid().
- History: Keeps track of previously executed commands for easy retrieval.
- Autocompletion: Suggests commands and file paths to improve user experience.
- Signals: Handles system signals for effective process management.
- pinfo: Displays information about running processes.
- Background and Foreground Execution: Supports executing commands in both modes with and without arguments.
- Pipeline: Allows chaining commands, passing output from one as input to another.
- Redirection: Supports input and output redirection with pipeline functionality.
- Low-Level System Calls: Implements direct POSIX-compliant system calls like
getcwd,chdir, and others, simulating native shell behaviors. - Error Handling and Validation: Every function call is designed with extensive error handling, ensuring robust operation even under unexpected conditions.
- Memory Management: Utilizes efficient memory handling practices to ensure low overhead for better performance.
- C++: Core logic and system interactions.
- POSIX APIs: For seamless, UNIX-compatible system call handling.
- Version Control: Managed using Git and organized for seamless collaboration.
The shell is designed with a modular architecture separating:
- Core execution engine
- Pipeline handling
- I/O redirection
- Built-in command implementations
- Shell utilities
- Interactive shell features
This separation improves maintainability, extensibility, and debugging efficiency while mirroring the layered design commonly used in production-grade systems software.
- Process creation using
fork() - Program execution using
execvp() - Foreground and background process handling
- Parent-child synchronization using
waitpid()
- Input redirection (
<) - Output overwrite redirection (
>) - Output append redirection (
>>) - File descriptor manipulation using
dup2()
- Multi-stage command pipelines using
pipe() - Inter-process communication between child processes
- Chained execution of shell commands
cdpwdechohistorypinfo
- Persistent command history
- Custom shell prompt
- Background process execution (
&) - Command parsing and tokenization
| Domain | Technologies / Concepts |
|---|---|
| Language | C++ |
| OS Concepts | POSIX, UNIX Process Model |
| Process Management | fork(), execvp(), waitpid() |
| IPC | pipe() |
| File Systems | open(), dup2(), close() |
| Environment Management | getenv(), setenv() |
| Signals | Signal Handling |
| Shell Design | Parsing, Tokenization, Execution Flow |
src/
├── commands/ # Built-in shell commands
├── core/ # Execution engine and pipelines
├── features/ # Shell features and utilities
├── utilities/ # Helper modules and prompt handlingmake -f Makefile.mk./Posix_Shellpwd
ls
echo hello worldls | wc -l
cat file.txt | grep mainecho hello > out.txt
cat < out.txtsleep 10 &-
Challenge: Managing child processes without resource leakage.
Solution: Usedwaitpid()to synchronize and clean up child processes. -
Challenge: Handling file descriptors across pipelines and redirections.
Solution: Useddup2()for redirection and closed unused descriptors to prevent leaks. -
Challenge: Parsing shell commands with pipelines, redirection, and background execution.
Solution: Implemented modular command parsing and tokenization before execution. -
Challenge: Preserving shell state across commands.
Solution: Maintained the current working directory and command history throughout the shell session.
The shell was manually validated across multiple command execution scenarios, including:
- Foreground and background process execution
- Multi-stage pipelines
- File descriptor redirection correctness
- Built-in command behavior
- Invalid command handling
- Directory traversal edge cases
- Concurrent process execution scenarios
Example validation cases:
ls | grep cpp
echo hello world | wc
cat file.txt | grep main > out.txt
sleep 5 &Planned enhancements include:
- Job control (
jobs,fg,bg) - Advanced signal handling
- Multi-command execution using
; - Bash-style auto-completion
- Environment variable expansion
- Quote-aware command parsing
- Alias support
- Configuration file support (
.rc) - Improved terminal interaction using
readline - Process monitoring dashboard
Welcome contributions from the community! If you're interested in contributing, please follow these steps:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Make your changes and commit them with clear messages.
- Push your changes to your forked repository.
- Submit a PR detailing your changes and the motivation behind them.
Appreciate your interest in contributing and will review your PR as soon as possible!