Skip to content

VaNsH-IIIT/c-shell-implementation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

C-Shell Implementation

A custom Unix shell built from scratch in C using POSIX system calls and Linux process management primitives. This project recreates the core functionality of a Unix shell, including command execution, parsing, piping, I/O redirection, job control, background execution, and signal handling.

The primary objective of this project was to gain hands-on experience with Operating Systems concepts such as process creation, inter-process communication, file descriptor management, signals, and shell architecture.


Features

Shell Interface

  • Interactive Unix-style shell prompt
  • Displays username, hostname, and current working directory
  • Supports home directory (~) expansion in the prompt
  • Continuously accepts and executes user commands

Command Parsing

The shell includes a dedicated parser capable of interpreting complex command structures.

Supported syntax includes:

  • Multiple command arguments
  • Sequential execution (;)
  • Background execution (&)
  • Multi-stage pipelines (|)
  • Input redirection (<)
  • Output redirection (>)
  • Output append redirection (>>)

The parser validates command syntax before execution and separates parsing from execution using a modular architecture.


Process Management

The shell executes external commands using Linux process management primitives.

Implemented features include:

  • Child process creation
  • Foreground process execution
  • Background process execution
  • Process synchronization
  • Child process cleanup
  • Process tracking

System calls used:

  • fork()
  • execvp()
  • wait()
  • waitpid()

Pipes

Supports multi-stage command pipelines such as:

cat file.txt | grep hello | sort

Implementation uses:

  • pipe()
  • dup2()
  • Multiple child processes
  • File descriptor management

I/O Redirection

Supports:

command < input.txt
command > output.txt
command >> output.txt

Implemented using:

  • open()
  • dup2()
  • close()

Supports combining pipes and redirection.


Sequential Execution

Supports execution of multiple commands in order using:

command1 ; command2 ; command3

Each command completes before the next one begins.


Background Execution

Supports background jobs using:

sleep 10 &

Features include:

  • Non-blocking execution
  • Background process tracking
  • Automatic cleanup of completed jobs
  • Interactive shell remains responsive

Job Control

Supports Unix job management through built-in commands:

  • fg
  • bg
  • activities

Capabilities include:

  • Bringing jobs to the foreground
  • Resuming stopped jobs
  • Listing active jobs
  • Maintaining process state information

Signal Handling

The shell correctly handles interactive Unix signals.

Implemented support for:

  • SIGINT (Ctrl+C)
  • SIGTSTP (Ctrl+Z)

The shell forwards signals to foreground processes while ensuring the shell itself continues running.


Built-in Commands

hop

Changes the current working directory.

Supports:

  • Home directory
  • Parent directory
  • Previous directory
  • Relative paths
  • Absolute paths

reveal

Lists directory contents.

Supports:

  • Hidden files
  • Multiple flags
  • Directory traversal

log

Persistent command history.

Supports:

  • Viewing history
  • Clearing history
  • Re-executing previous commands

activities

Displays currently running or stopped processes managed by the shell.


ping

Sends Unix signals to a specified process.


fg

Moves a background or stopped process to the foreground.


bg

Resumes a stopped process in the background.


Operating Systems Concepts Demonstrated

This project provides practical implementation of the following Operating Systems concepts:

  • Unix Shell Architecture
  • POSIX System Programming
  • Process Creation
  • Process Synchronization
  • Process Control
  • Job Control
  • Inter-Process Communication
  • Pipes
  • File Descriptor Management
  • Input/Output Redirection
  • Signal Handling
  • Background Process Management
  • Command Parsing
  • Modular Systems Programming

System Calls Used

The implementation makes extensive use of POSIX system calls, including:

System Call Purpose
fork() Create child processes
execvp() Execute external commands
wait() Wait for child processes
waitpid() Synchronize and manage child processes
pipe() Create communication channels between processes
dup2() Redirect standard input and output
open() Open files for redirection
close() Close file descriptors
chdir() Change working directory
getcwd() Retrieve current working directory
kill() Send signals to processes
sigaction() Install signal handlers

Project Structure

shell/
├── Makefile
├── include/
│   └── shell.h
└── src/
    ├── activities.c
    ├── background.c
    ├── builtin.c
    ├── executor.c
    ├── hop.c
    ├── job_control.c
    ├── log.c
    ├── main.c
    ├── parser.c
    ├── ping.c
    ├── process_mgmt.c
    ├── prompt.c
    ├── reveal.c
    ├── sequential.c
    └── signals.c

File Descriptions

File Responsibility
main.c Shell initialization and main execution loop
prompt.c Displays the interactive shell prompt
parser.c Parses user commands and shell operators
executor.c Executes parsed commands
builtin.c Dispatches built-in shell commands
hop.c Directory navigation implementation
reveal.c Directory listing functionality
log.c Persistent command history
background.c Background process execution and tracking
process_mgmt.c Process bookkeeping and management
job_control.c Foreground/background job management
signals.c Signal registration and handling
activities.c Lists active jobs managed by the shell
ping.c Sends signals to processes
sequential.c Sequential command execution

Building

Compile the project using:

make all

Run the shell:

./shell.out

Example Usage

reveal

hop ..

hop ~

cat file.txt

cat input.txt > output.txt

sort < input.txt

cat file.txt | grep hello

cat file.txt | grep hello | sort

sleep 15 &

activities

fg

bg

log

log execute 2

Challenges

Some of the major implementation challenges included:

  • Designing a modular command parser
  • Handling multiple child processes simultaneously
  • Implementing multi-stage pipelines
  • Managing file descriptors during redirection
  • Handling Unix signals without terminating the shell
  • Implementing reliable foreground and background job control
  • Synchronizing concurrent processes
  • Maintaining a persistent command history

Learning Outcomes

This project strengthened my understanding of:

  • Linux internals
  • POSIX programming
  • Unix process lifecycle
  • Process synchronization
  • Shell implementation
  • Job scheduling
  • Signal handling
  • File descriptor management
  • Inter-process communication
  • Modular software design in C

Future Improvements

Potential enhancements include:

  • Environment variable expansion
  • Wildcard (*) expansion
  • Command auto-completion
  • Alias support
  • Command substitution
  • Quoted string handling
  • Shell scripting support
  • Improved error reporting
  • Configuration file support
  • Plugin architecture

Technologies

  • Language: C
  • Platform: Linux
  • Standards: POSIX
  • Build System: Make
  • Core Concepts: Operating Systems, Unix System Programming, Process Management, Inter-Process Communication, Signals, File Systems

About

A POSIX-compliant Unix shell implemented in C with support for pipelines, I/O redirection, background jobs, built-in commands, command history, and process management using Unix system calls.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors