Menu

(Solved) : Part Implement Command Line Interpreter K Shell Shell Operate Basic Way Type Command Resp Q31407540 . . .

In this part, you will implement a command-line interpreter(a.k.a. a shell). The shell should

operate in this basic way: when you type in a command (inresponse to the prompt), the shell

creates a child process that executes the command you entered(by running the corresponding

program), displays the output of the command, and then promptsfor more user input when it

has finished.

More specifically, you are expected to extend the

myshell.c

program (provided earlier as an

example of multiprocessing). Your shell shall run in theinteractive interactive mode, where it will

display a prompt (using any string of your choosing) and theuser of the shell will type in a

command at the prompt. Your shell stops accepting new commandswhen it sees a

quit

command on a line or reaches the end of the input stream (i.e.,when the user types ‘Ctrl-D’).

The shell should wait for running processes have been properlyterminated before exit the

program.

Each line (typed at the prompt) may contain multiple commandsseparated with the semicolon

(;) character. Each of the commands separated by semicolonshould be run concurrently. The

shell should not print the next prompt and take more input untilall of the foreground commands

have finished executing (the

wait()

and/or

waitpid()

system calls may be useful here).

Foreground commands are those commands not ended with & asopposed to the background

commands (see below).

You will also need to implement to run programs in thebackground. If a command ends with the

& character (this is not an argument), it means that theprogram should run in background. That

is, the shell immediately prints out the PID of the programcorresponding to the command and

then proceeds to the next input line without waiting for theprogram to finish. In case there are

multiple commands in the same command line (separated by the ;character), some commands

may be run in background (those followed by &) and some inforeground (those without). In this

case, the shell needs to prompt the PIDs of the backgroundcommands and waits for the

foreground commands to finish before moving onto the nextline.

Note that your shell program should maintain the list of currentrunning background programs so

that when it quits execution, it needs to wait for theseprograms to finish before exiting.

Your shell program should handle different situations and handlethem properly. The following

are examples (these are not complete):

During the shell’s execution, when the input command is notfound, the shell should

prompt a useful message and continue.

If the input command is empty (an empty command line or onlyspaces between

semicolons), the command should be ignored.

The shell should be able to handle extra white spaces betweencommands, arguments,

and semicolons.

The shell should be able to handle quit command or when usertypes ‘Ctrl-D’ as

command in the interactive mode.

Examples:

The following contains some examples of the command lines(where

“JohnSmith>”

is the

prompt; you can choose your own). You should include theseexamples and output in your

report.

Start the interactive shell and then quit from the shell:

$./myshell

JohnSmith> quit

Run a single program (with zero or more arguments) in theforeground; the shell will wait for the

program to complete before it goes onto the next input line.(The output from the commands are

ignore below.)

JohnSmith> /bin/ls

JohnSmith> /bin/ls -alF

JohnSmith> /usr/bin/grep string myfile

Run multiple programs simultaneously in the foreground; theshell will wait for all programs to

complete before it goes onto the next input line. The outputfrom multiple programs (not shown

here) may be interleaved due to concurrency:

JohnSmith> /bin/ls; /bin/cat myfile

///////////////////myshell.c/////////////////

/* This example is stolen from Dr. Raju Rangaswami’s original4338

demo and modified to fit into our lecture. */

#include <stdio.h>

#include <sys/wait.h>

#include <unistd.h>

#include <string.h>

#include <stdlib.h>

#define MAX_ARGS 20

#define BUFSIZ 1024

int get_args(char* cmdline, char* args[])

{

int i = 0;

/* if no args */

if((args[0] = strtok(cmdline, “nt “)) == NULL)

return 0;

while((args[++i] = strtok(NULL, “nt “)) != NULL) {

if(i >= MAX_ARGS) {

printf(“Too many arguments!n”);

exit(1);

}

}

/* the last one is always NULL */

return i;

}

void execute(char* cmdline)

{

int pid, async;

char* args[MAX_ARGS];

int nargs = get_args(cmdline, args);

if(nargs <= 0) return;

if(!strcmp(args[0], “quit”) || !strcmp(args[0], “exit”)) {

exit(0);

}

/* check if async call */

if(!strcmp(args[nargs-1], “&”)) { async = 1; args[–nargs] =0; }

else async = 0;

pid = fork();

if(pid == 0) { /* child process */

execvp(args[0], args);

/* return only when exec fails */

perror(“exec failed”);

exit(-1);

} else if(pid > 0) { /* parent process */

if(!async) waitpid(pid, NULL, 0);

In this part, you will implement a command-line interpreter (a.k.a. a shell). The shell should operate in this basic way: when you type in a command (in response to the prompt), the shell creates a child process that executes the command you entered (by running the corresponding program), displays the output of the command, and then prompts for more user input when it has finished. More specifically, you are expected to extend the myshell.c program (provided earlier as an example of multiprocessing). Your shell shall run in the interactive interactive mode, where it will display a prompt (using any string of your choosing) and the user of the shell will type in a command at the prompt. Your shell stops accepting new commands when it sees a quit command on a line or reaches the end of the input stream (i.e., when the user types Ctrl-D The shell should wait for running processes have been properly terminated before exit th program. Each line (typed at the prompt) may contain multiple commands separated with the semicolon (;) character. Each of the commands separated by semicolon should be run concurrently. The shell should not print the next prompt and take more input until all of the foreground commands have finished executing (the wait) and/or waitpid) system calls may be useful here) Foreground commands are those commands not ended with & as opposed to the background commands (see below) You will also need to implement to run programs in the background. If a command ends with the & character (this is not an argument), it means that the program should run in background. That is, the shell immediately prints out the PID of the program corresponding to the command and

In this part, you will implement a command-line interpreter (a.k.a. a shell). The shell should operate in this basic way: when you type in a command (in response to the prompt), the shell creates a child process that executes the command you entered (by running the corresponding program), displays the output of the command, and then prompts for more user input when it has finished. More specifically, you are expected to extend the myshell.c program (provided earlier as an example of multiprocessing). Your shell shall run in the interactive interactive mode, where it will display a prompt (using any string of your choosing) and the user of the shell will type in a command at the prompt. Your shell stops accepting new commands when it sees a quit command on a line or reaches the end of the input stream (i.e., when the user types ‘Ctrl-D The shell should wait for running processes have been properly terminated before exit th program. Each line (typed at the prompt) may contain multiple commands separated with the semicolon (;) character. Each of the commands separated by semicolon should be run concurrently. The shell should not print the next prompt and take more input until all of the foreground commands have finished executing (the wait) and/or waitpid) system calls may be useful here) Foreground commands are those commands not ended with & as opposed to the background commands (see below) You will also need to implement to run programs in the background. If a command ends with the & character (this is not an argument), it means that the program should run in background. That is, the shell immediately prints out the PID of the program corresponding to the command and Show transcribed image text

Expert Answer


Answer to Part Implement Command Line Interpreter K Shell Shell Operate Basic Way Type Command Resp Q31407540 . . .

OR