Question
Design a C program to serve as a shell interface that accepts user commands and then executes each command in a separate process. A shell
Design a C program to serve as a shell interface that accepts user commands and then executes each command in a separate process. A shell interface gives the user a prompt, after which the next command is entered.
A C program that provides the general operations of a command-line shell is as follows:
#include
#include
#include
#define MAXLINE 80
int main(void) {
char *args[MAXLINE/2 + 1]; /* command line with max 40 arguments */
int should_run = 1; /* flag to determine when to exit program */
while (should_run) {
printf("user>");
fflush(stdout);
/* After reading user input, the steps are:
* (1) fork a child process using fork()
* (2) the child process will invoke execvp()
* (3) if command included &, parent will NOT invoke wait()
*/
}
return 0;
}
The main() function presents the prompt and outlines the steps to be taken after input from the user has been read. The main() function continually loops as long as should_run equals 1; when the user enters exit at the prompt, your shell will set should_run to 0 and terminate.
You should modify the main() function so that a child process is forked and executes the command specified by the user. This will require parsing what the user has entered into separate tokens and storing the tokens in an array of character strings args. For example, if the user enters the command ps af at the prompt, the values stored in the args array are:
args[0] = "ps"
args[1] = "-af"
args[2] = NULL
This args array will be passed to the execvp() function, 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. The execvp() function should be invoked as execvp(args[0], args). Be sure to check whether the user included an & to determine whether or not the shell (parent) process is to wait for thechild to exit.
When fork() or execvp() failed, the shell should print out error messages, output the prompt and accept the next command from user.
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