Question
This program consists of designing a C program to serve as a shell interface that accepts user commands and then executes each command in a
This program consists of 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
The technique you will use is to have the parent process first read what the user enters on the command line, and then create a separate child process that performs the command. The parent process will wait for the child process to terminate before continuing.
An example program has been provided for you simple-shell.c as a starting point. You will notice this program currently prompts the user and reads in what the user en- ters. It also provides the function parse command() that parses the command and places each token of the command into the args array. It is crucial that you run this example program and be familiar with its functionality, including the parse command() function. (Your currently exit this program by entering
You will use the fork() system call to create a child process, and have the child process run the command that has been entered from the command line. The child process will execute this command using the execvp() system call which has the following prototype:
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 entered
osh> cat prog.c The parse command() function would set the args array as follows:
1
args[0] = "cat" args[1] = "prog.c"
For this program, the execvp() function should 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 above situation, this means setting args[2] = NULL; Specifics
-
The child process will be created using the fork() system call, it will run the com- mand using execvp(), and the parent will wait for the child to terminate before proceeding.
-
If the user enters the command exit, the program will terminate.
-
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 way to determine if the command is invalid .... think what happens if execvp() cannot run.)
-
You will need to keep track of the most recent command that has been run. If the user enters !!, you will run the last command entered. (Be sure to check the special case if !! is entered as the first command. In this case, there is no recent command. If this occurs, you must output the message No command history found.)
Your submission will also require providing a Makefile for compiling and building your shell.
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