Question
A Simple Shell A C program that provides the basic operation of a command line shell is supplied in Code I. This program is composed
A Simple Shell A C program that provides the basic operation of a command line shell is supplied in Code I. This program is composed of two functions: main( ) and setup( ). The setup( ) function reads in the users next command (which can be up to 80 characters), and then parses it into separate tokens that are used to fill the argument vector for the command to be executed. (If the command is to be run in the background, it will end with & , and setup( ) will update the parameter background so the main( ) function can act accordingly. This program is terminated when the user enters exit. The main( ) function presents the prompt COMMAND-> and then invokes setup( ) , which waits for the users to enter a command. The contents of the command entered by the user are loaded into the args array. For example, if the user enters ls l at the COMMAND-> prompt, args[0] becomes equal to the string ls and args[1] is set to the string to -l. (By string, we mean a null-terminated, C-style string variable.)
Code 1: Shell
#include start = -1; inputBuffer[i] = '\0'; } } } args[ct] = NULL; /* just in case the input line was > 80 */ } int main(void) { char inputBuffer[MAX_LINE]; /* buffer to hold the command entered */ int background; /* equals 1 if a command is followed by '&' */ char *args[(MAX_LINE/2)+1]; /* command line (of 80) has max of 40 arguments */ while (1){ /* Program terminates normally inside setup */ background = 0; printf(" COMMAND-> "); setup(inputBuffer,args,&background); /* get next command */ /* the steps are: (0) if built-in command, handle internally (1) if not, fork a child process using fork() (2) the child process will invoke execvp() (3) if background == 0, the parent will wait, otherwise returns to the setup() function. */ } } ---------------------------------------------------------------------------------------------------------------------- Code 2: Signal handeling #include ---------------------------------------------------------------------------------------------------------------------- 1. When your shell starts up, it must print a greeting and its pid. You can use getpid() to get the pid. Example: "Welcome to kbshell. My pid is 26357." 2. The shell prompt must be "xxshell[n]:", where xx are your initials, and n is the number of prompts shown so far (starting at 1 and increasing). Example: kbshell[1]: 3. The shell must support the following built-in commands: stop Example output kbshell[1]: time sleep 8 & [Child pid = 26358, background = TRUE] kbshell[2]: date [Child pid = 26359, background = FALSE] /* Note that the order of the next couple statements depends on the scheduler */ Wed Feb 11 22:48:42 PDT 2021 Child process complete kbshell[3]: real 0m8.018s /* Note this is the child output which is printed after the parent has completed another command */ user 0m0.001s sys 0m0.001s kbshell[4]: time sleep 8 [Child pid = 26360, background = FALSE] real 0m8.018s /* This is the child output before the parent prints another prompt. */ user 0m0.001s sys 0m0.001s Child process complete kbshell[5]: Note that, in the foreground case, since we don't know whether the child or parent will execute first after the fork, the pid message may come before or after child's output.
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