This project implements a custom Linux shell with basic command execution and process scheduling capabilities. It uses shared memory and semaphores to manage and schedule processes, and it maintains a history of executed commands.
- Basic Shell Functionality: Execute standard shell commands.
- Process Scheduling: Submit and schedule processes with different priorities.
- Shared Memory: Utilize shared memory to communicate between parent and child processes.
- Semaphores: Ensure proper synchronization using semaphores.
- History Tracking: Keep a history of executed commands.
- Signal Handling: Gracefully handle the termination signal (Ctrl+C) to print process information before exiting.
Stores information about a scheduled process.
typedef struct schedInfo {
int pid;
char name[1000];
int execTime;
int waitTime;
int priority;
} schedInfo;Stores information about an executed child process.
typedef struct execInfo {
int pid;
time_t timeExecuted;
float duration;
int exitStatus;
} execInfo;Shared memory buffer structure.
typedef struct shmbuf {
int isRunning;
sem_t sem_lock;
sem_t sem_check;
int ncpu;
int tslice;
int pid_cnt;
schedInfo pids[BUF_SIZE];
int completed_cnt;
schedInfo completed[BUF_SIZE];
int pids_completed_cnt;
int pids_completed[BUF_SIZE];
} shmbuf;Initializes shared memory and semaphores.
Prints information about all executed processes.
Prints the history of executed commands.
Creates and runs child processes based on the provided arguments.
Parses user input and executes the appropriate command.
Handles the termination signal (Ctrl+C) and prints process information before exiting.
Compile the code using gcc:
gcc -o custom_shell custom_shell.c -pthread -lrtRun the compiled program:
./custom_shell- Basic Commands: Any standard shell command can be executed.
- Submit Commands: Use the
submitkeyword to schedule a process. For example,submit ls -l. - History: The
historycommand prints the history of executed commands.
Enter number of CPUs: 2
Enter tslice in milliseconds: 100
Waiting for scheduler
Scheduler hooked
> ls
<output of ls command>
> submit ls -l
<scheduled output of ls -l command>
> history
ls
submit ls -l
- Ensure you have appropriate permissions to create shared memory objects.
- The
submitkeyword is used to schedule commands with a specified priority. - The program gracefully handles Ctrl+C, printing process information before exiting.
- pthread library: Used for semaphores.
- librt: Used for shared memory.
gcc -o custom_shell custom_shell.c -pthread -lrt