Skip to content

Udaykumar3525/minishell

Repository files navigation

Mini Shell

Name: Udaykumar Upputuri
Description: Developed a Linux Mini Shell in C Language using Linux System Calls
Mail: udayupputuri3525@gmail.com

📌 Project Overview

The Mini Shell project is a command-line interpreter developed in C that mimics the basic behavior of a Linux Shell. The shell continuously accepts user commands, identifies whether they are Built-in or External commands, and executes them accordingly. The project demonstrates Linux System Programming concepts such as Process Creation, Process Management, Signal Handling, Pipes, Job Control, Dynamic Memory Allocation, and Linked Lists.

📖 What is a Shell

A Shell is a command interpreter that acts as an interface between the User and the Operating System. User Command -> Shell -> Linux Kernel -> Execute Command Examples: pwd, ls -l, cd Desktop, date, cat file.txt, ls -l | grep c | wc The shell accepts commands from the user and executes them using Linux system calls. Mini Shell helps in understanding: fork(), execvp(), waitpid(), pipe(), dup2(), signal handling, foreground and background processes

⚙️ Features

  • Custom Prompt Support (PS1)
  • Built-in Commands
  • External Commands
  • Single Pipe Support
  • Multiple Pipe Support
  • Signal Handling (Ctrl+C, Ctrl+Z)
  • Job Control (jobs, fg, bg)
  • Linked List based Stopped Process Management
  • Dynamic External Command Loading

📂 Project Structure

MiniShell
├── main.c                 - Entry point of the project
├── header.h               - Structures, macros and declarations
├── scan_input.c           - Input handling, signals and process management
├── commands.c             - Built-in commands, external commands and pipes
├── external_cmd.txt       - List of supported external commands
└── README.md              - Project documentation

🧠 Concepts Used

  • Process Management : fork(), waitpid(), execvp()
  • Signal Handling : SIGINT, SIGTSTP, SIGCHLD
  • IPC : pipe(), dup2()
  • Data Structure: Linked List - Used for storing stopped processes.

Overall Project Workflow

1️⃣ Shell Startup: Program starts from main() -> Prompt initialized -> External commands loaded -> Signal handlers registered 2️⃣ Read User Command -> Ex: minishell:~$ ls -l , Command is read using: fgets() 3️⃣ Extract Command -> Input: ls -l, Extracted Command: ls, Function: get_command() 4️⃣ Identify Command Type -> Function: check_command_type(), Checks whether command is: Built-in or External or Invalid 5️⃣ Execute Built-in Command : Ex: cd, pwd, exit, jobs, fg, bg, echo $$, echo $?, echo $SHELL -> Executed directly by the shell process. 6️⃣ Execute External Command : Ex: ls -l Flow: Parent Process calls -> fork() creates -> Child Process -> child executes execvp() -> Execute Command -> Child Terminates -> Parent waiting for child and after child termination it Continues 7️⃣ Pipe Execution : Ex: ls | grep c | wc Flow: Split Commands -> Create Pipes (for n commands create n-1 pipes) -> Create Child Processes -> Connect Pipes using dup2() -> Execute Commands (execvp) -> Display Output Supports: ls | wc , ls | grep c | wc , ls | grep c | sort | wc 8️⃣ Signal Handling ctrl+c -> SIGINT, Terminates foreground process, ex: sleep 30 ^C ctrl+c -> SIGTSTP, Stops foreground process, ex: sleep 30 ^Z -> Stopped process is stored in Linked List. 9️⃣ Job Control jobs - Displays all stopped processes. fg - Runs latest stopped process in foreground, Shell waits until process completes. bg - Runs latest stopped process in background, Shell immediately returns prompt.

main()
   |
   V
scan_input()
   |
   V
Load Commands + Register Signals
   |
   V
Read User Command
   |
   V
get_command()
   |
   V
check_command_type()
   |
   +-------------------+
   |                   |
   V                   V
Built-in          External
Command           Command
   |                  |
   |               fork()
   |                  |
   |             execvp()
   |                  |
   +--------waitpid---+
            |
            V
      Pipe Handling
            |
            V
 Signal Handling
(Ctrl+C / Ctrl+Z)
            |
            V
 Jobs / fg / bg
            |
            V
      Show Prompt

🔑 Signal Handling Logic

Both Parent and Child receive: SIGINT, SIGTSTP because they belong to the same foreground process group. Cases handled: No Child Running -> Shell handles signal -> Child Running -> Child handles signal -> Shell continues This prevents duplicate prompt printing.

🔧 Important Functions

Function                        Purpose

scan_input()                    Read and process commands
extract_external_commands()     Load external commands
get_command()                   Extract command name
check_command_type()            Identify command type
execute_internal_commands()     Execute built-in commands
execute_external_commands()     Execute external commands
signal_handler()                Handle signals
insert_first()                  Store stopped process
delete_first()                  Remove process from list
print_list()                    Display stopped jobs

🧠 Applications

  • Linux Shell Understanding
  • Linux System Programming
  • Process Management Learning
  • Signal Handling Learning
  • IPC Learning
  • Operating System Projects
  • Interview Preparation

✅ Advantages

  • Demonstrates Linux Shell Internals
  • Supports Multiple Pipes
  • Supports Job Control
  • Handles Signals Properly
  • Dynamic Command Execution
  • Good Understanding of Linux System Calls

❌ Limitations

  • No I/O Redirection Support
  • No Command History
  • No Auto Completion
  • No Environment Variable Expansion
  • Basic Job Control Implementation

🚀 Key Learnings

  • Linux Process Creation
  • Parent Child Synchronization
  • Signal Handling
  • Job Control Mechanism
  • Pipe Communication
  • Process States
  • Dynamic Memory Allocation
  • Linked List Management
  • Linux System Calls

🏁 Sample Execution

minishell:~$ sleep 30
^Z

[1]+ Stopped sleep 30

minishell:~$ jobs

[1]+ Stopped sleep 30

minishell:~$ fg

sleep 30

minishell:~$

-----------------------------------------> Thank You <-----------------------------------------

About

A Linux Mini Shell developed in C that supports built-in commands, external command execution, pipes, signal handling, and job control using Linux system calls.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages