This project is a custom implementation of a simple unix shell, designed to execute user commands similarly to the bash shell. The shell supports basic command execution, background process handling, signal management, and both serial and parallel command execution.
- Basic Command Execution: Supports execution of standard Unix commands.
- Background Processes: Allows running commands in the background using
&. - Serial Command Execution: Supports sequential execution of multiple commands using
&&. - Parallel Command Execution: Supports parallel execution of multiple commands using
&&&. - Signal Handling: Handles
SIGINTsignal to interrupt and terminate processes. - Graceful Exit: Implemented
exitcommand to terminate the shell and all background processes.
- GCC compiler
- Unix based operating system (Linux)
To start the shell, compile and run the provided source code:
gcc -o my_shell my_shell.c
./my_shell-
Change Directory:
$ cd /path/to/directory $ cd ..
-
Execute a Command:
$ ls $ echo "Hello, World!"
-
Background Execution:
$ sleep 10 & -
Foreground Serial Execution:
$ command1 && command2 && command3
-
Foreground Parallel Execution:
$ command1 &&& command2 &&& command3
-
Exit the Shell:
$ exit
- Terminate Foreground Process: Press
Ctrl+Cto sendSIGINTto the foreground process. The shell will remain running, and only the foreground process will be terminated.
-
The shell reads user input, tokenizes it, and uses
fork,exec, andwaitsystem calls to execute commands. -
Commands are executed in the foreground by default.
-
Commands followed by
&are executed in the background, allowing the shell to accept new commands immediately. -
Background processes are periodically reaped to prevent zombie processes.
-
The
exitcommand terminates the shell and all running background processes. -
The shell cleans up dynamically allocated memory before exiting.
-
Custom handling of
SIGINTto ensure the shell does not terminate but only the foreground process does. -
Background processes are placed in separate process groups to isolate them from
SIGINT. -
Commands separated by
&&are executed serially. -
Commands separated by
&&&are executed in parallel. -
The shell ensures proper termination and cleanup of all foreground processes when
SIGINTis received.