Skip to content

Latest commit

 

History

31 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

POSIX Shell Implementation

Introduction

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().

Key Features

  • 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.

Technical Highlights

  • 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.

Built With

  • 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.

Architecture Overview

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.


Key Features

Command Execution

  • Process creation using fork()
  • Program execution using execvp()
  • Foreground and background process handling
  • Parent-child synchronization using waitpid()

I/O Redirection

  • Input redirection (<)
  • Output overwrite redirection (>)
  • Output append redirection (>>)
  • File descriptor manipulation using dup2()

Pipeline Execution

  • Multi-stage command pipelines using pipe()
  • Inter-process communication between child processes
  • Chained execution of shell commands

Built-in Commands

  • cd
  • pwd
  • echo
  • history
  • pinfo

Interactive Shell Features

  • Persistent command history
  • Custom shell prompt
  • Background process execution (&)
  • Command parsing and tokenization

Technologies & Concepts

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

Project Structure

src/
├── commands/      # Built-in shell commands
├── core/          # Execution engine and pipelines
├── features/      # Shell features and utilities
├── utilities/     # Helper modules and prompt handling

Build & Execution

Build

make -f Makefile.mk

Run

./Posix_Shell

Example Usage

Standard Commands

pwd
ls
echo hello world

Pipeline Execution

ls | wc -l
cat file.txt | grep main

I/O Redirection

echo hello > out.txt
cat < out.txt

Background Execution

sleep 10 &

Challenges & Solutions

  • Challenge: Managing child processes without resource leakage.
    Solution: Used waitpid() to synchronize and clean up child processes.

  • Challenge: Handling file descriptors across pipelines and redirections.
    Solution: Used dup2() 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.


QA

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 &

Future Scope

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

Contributions

Welcome contributions from the community! If you're interested in contributing, please follow these steps:

  1. Fork the repository.
  2. Create a new branch for your feature or bug fix.
  3. Make your changes and commit them with clear messages.
  4. Push your changes to your forked repository.
  5. 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!

About

POSIX-compliant command-line shell in C++ using fork(), execvp(), pipe(), dup2(), and waitpid().

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Contributors

Languages