Question
This assignment will involve using the fork(), exec() and wait() system calls to implement a UNIX-like shell interface. You may write this program using a
This assignment will involve using the fork(), exec() and wait() system calls to implement a UNIX-like shell interface. You may write this program using a Linux or macOS system.
This program involves designing a C program to serve as a shell interface that accepts user commands and then executes each command in a separate process. The shell interface gives the user a prompt after which the next command is entered. The example below illustrates the prompt osh> and the users next command cat prog.c . (This command displays the file prog.c on the terminal using the cat command):
osh> cat prog.c
You will have the parent process first read the command the user enters and then create a separate child process that performs the command, and the parent process will wait for the child process to terminate before resuming.
Use the starting code simple-shell.c below as a starting point. You will notice this program currently prompts the user and reads what is entered. It also provides the function parse_command() that parses the command and places each element in the command into the args array. It is crucial that you run this example program and are familiar with its functionality, especially the parse_command() function. (Currently, this program exits by entering
You will use the fork() system call to create the child process and have the child process run the command entered by the user using the execvp() system call which has the following API:
execvp(char *command, char *params[])
Here, command represents the command to be performed and params` stores the parameters to this command. For example, if the user enters
osh> cat prog.c
the parse_command() function would set the args array as:
args[0] = "cat" args[1] = "prog.c"
The execvp() function would be invoked as execvp(args[0], args). It is important that the array element in the args array following the last parameter be set to NULL. In the example above, this means setting args[2] = NULL.
Requirements for modified code:
-
The child process will be created using the fork() system call and it will run the command using execvp(). The parent will wait for the child to terminate before proceeding.
-
The program will terminate when the user enters the command exit.
-
If an invalid command is entered your program should output the message Command not found and the child process should terminate. (Hint - there is a very easy was to determine if the command is invalid: think of what happens if execvp() cannot run.)
-
You will need to keep track of the most-recent command that has been run - including if the most recent command is invalid. If the user enters !! you will run the most-recent command. Be sure to check the special case if !! is entered as the first command to the shell program as this means there is no recent command. If this occurs, you must output the message No command history found.
Code to Modify: #include#include #include #include // function prototypes int parse_command(char command[], char *args[]); #define MAX_LINE 80 /* 80 chars per line, per command */ int main(void) { char *args[MAX_LINE/2 + 1]; /* command line (of 80) has max of 40 arguments */ int should_run = 1; int command_length; char command[MAX_LINE]; while (should_run) { printf("osh>"); fflush(stdout); fgets(command, MAX_LINE, stdin); command_length = parse_command(command, args); for (int i = 0; i < command_length; i++) printf("[%d] [%s] ", i, args[i]); } return 0; } /** * Parses the command and places each token * into args array. */ int parse_command(char command[], char *args[]) { char *spart = strtok(command, " "); int length = 0; while (spart != NULL) { args[length] = spart; length++; spart = strtok(NULL, " "); } return length; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started