Starting from:
$55

$49

Programming Assignment #1: A Simple Shell Solution

This project consists of modifying a C program which serves as a shell interface that accepts user
commands and then executes each command in a separate process. A shell interface provides the
user a prompt after which the next command is entered. The example below illustrates the
prompt sh> and user’s next command: “cat prog.c”. This command displays the contents of the
file prog.c on the terminal using the UNIX cat command.

sh> cat prog.c

One technique for implementing a shell interface is to have the parent process first read what the
user enters on the command line (i.e. cat prog.c), and then create a separate child process that
performs the command. Unless otherwise specified, the parent process waits for the child to exit
before continuing. This is similar in functionality to what is illustrated in Figure 3.8 (10th edition)
or Figure 3.10 (9th edition) in the text. However, UNIX shells typically also allow the child
process to run in the background (concurrently with the parent) as well by specifying the
ampersand (&) at the end of the command. By rewriting the above command as:

sh> cat prog.c &

The parent and child processes now run concurrently. The separate child process is created using
the fork( ) system call and the user’s command is executed by using one of the system calls in
the exec( ) family (as described in the text, Section 3.3.1).

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 user’s 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 user 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.)

#include
#include
#define MAX_LINE 80
/* setup( ) reads in the next command line, separating it into distinct tokens using whitespace as
delimiters. setup( ) modifies the args parameter so that it holds pointers to the null-terminated
strings that are the tokens in the most recent user command line as well as a NULL pointer,
indicating the end of the argument list, which comes after the string pointers that have been
assigned to args. */

void setup (char inputBuffer[], char *args[], int *background)
{
/* full source code posted separately */
}

int main (void)
{
char inputBuffer[MAX_LINE] ; /* buffer to hold command entered */
int background; /* equals 1 if a command is followed by ‘& ’ */
char *args [MAX_LINE/2 + 1]; /* command line arguments */

while (1) {
background = 0 ;
printf (“ COMMAND ->”) ;
setup(inputBuffer, args, &background) ;
/** the steps are:
(0) If command is built-in, print command results to terminal. If command is exit, print
statistics and exit()
(1) fork a child process using fork( )
(2) the child process will invoke execvp( )
(3) if background == 0, the parent will wait(),
otherwise it will invoke the setup( ) function again. */
}
}
Code I. Outline of simple shell.

This project is organized into two parts: (1) creating the child process and executing the
command in the child, and (2) modifying the shell to add built-in commands such as a history
feature.

Creating a Child Process
The first part of this project is to modify the main( ) function in Code I so that upon returning
from setup( ), a child process is forked and executes the command specified by the user
As noted above, the setup ( ) function loads the contents of the args array with the command
specified by the user. This args array will be passed to the execvp( ) function, which has the
following interface:
execvp (char *command, char *params[ ] ) ;
where “command” represents the command to be performed and “params” stores the parameters
to this command. For this project, the execvp( ) function should be invoked as execvp(args[0],
args); be sure to check the value of background to determine if the parent process is to wait for
the child to exit or not.

More products