Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this assignment you will write your own shell. This will be quite a simple shell and will not have most of the features of

In this assignment you will write your own shell. This will be quite a simple shell and will not have most of the features of existing shells. The aim of the exercise is to gain some insight into the operation of a shell and how processes are combined in the shell. The mysh.h file contains the declarations of the data structures that are produced by the command parser. The parser.c and mysh.h files are taken from a working solution, so there should be no need to modify them.

Input to mysh is processed one line at a time. There will be one command on each line and commands cannot span several lines. Shell files can have comments, they start with the # character and go to the end of the line. Your program should read comments, but doesnt to do anything with them. This is handled in the command parser. In general, an input line will look like the following: command file A command consists of one or more strings, separated by white space. The pipe and redirection are optional. In the case of shell commands there are no pipes and no redirection. They are easily be parsed as a special case of the above. The shell commands that you must support are: exit exit from the shell prompt prompt_string change the interactive prompt string to prompt_string

In the case of a command the first string is the name of the program to execute and the remaining strings are the arguments to the command. This will all be packaged into an argv array when the program is executed. The end of a command is indicated by an end of line, or one of |, . Examples of commands are: program program output program arg1 arg2 arg3 prog1 arg1 | prog2 arg1 arg2 | prog3 >output The data structure constructed by the parser consists of a linked list with one entry for each command in the pipeline. This entry has any file redirections along with a pointer to another list with one entry for each argument. This data structure is dynamically allocated as the command line is parsed. Pipeline Execution In general, there will be several processes that are connected by pipes. The first process may have a redirected input file and the last process may have a redirected output file. The execution module needs to create these processes and the pipes that connect them. If there is more than one process in a pipeline, there must be a pipe connecting them. The execution module will need to fork a child process for each command in the pipeline. The child process will construct the argv array for the process, create a pipe if required, and do redirection if required. One of the exec functions that does a PATH search should then be used to start the program. This can be done in a loop. This part of the program waits for the last process in the pipeline to terminate and then returns. The shell command can be handled as a special case before the loop used to create the pipeline. Note, this part of the program is responsible for freeing the command line data structure that was allocated in the command line parser.

image text in transcribedimage text in transcribedimage text in transcribed

exec.c The exec function that executes a command pipeline. #include #include #include #include sunistd.h> #include #include #include #include #include "mysh.h" Execute a pipeline void executeCommand(char **argv) { int r = execvp(*argv, argv); if fir #include #include #include #include #include #include "mysh.h" int main(int argc, char **argv) { struct Pipeline *pipe; struct stat status; /* * Create the initial prompt string * Determine whether we are connected to a terminal 8 9 10 11 12 13 14 15 16 * 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 Prompt = (char*) malloc(2); strcpy(Prompt, "?"); fstat(@,&status); /* * Loop parsing one line at a time and executing it while(1) { if(S_ISCHR( status.st_mode)) printf("%s", Prompt); pipe = parse(stdin); if(pipe == NULL) break; if(exec(pipe)) break; } free (Prompt); 41 } exec.c The exec function that executes a command pipeline. #include #include #include #include sunistd.h> #include #include #include #include #include "mysh.h" Execute a pipeline void executeCommand(char **argv) { int r = execvp(*argv, argv); if fir #include #include #include #include #include #include "mysh.h" int main(int argc, char **argv) { struct Pipeline *pipe; struct stat status; /* * Create the initial prompt string * Determine whether we are connected to a terminal 8 9 10 11 12 13 14 15 16 * 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 Prompt = (char*) malloc(2); strcpy(Prompt, "?"); fstat(@,&status); /* * Loop parsing one line at a time and executing it while(1) { if(S_ISCHR( status.st_mode)) printf("%s", Prompt); pipe = parse(stdin); if(pipe == NULL) break; if(exec(pipe)) break; } free (Prompt); 41 }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Probabilistic Databases

Authors: Dan Suciu, Dan Olteanu, Christopher Re, Christoph Koch

1st Edition

3031007514, 978-3031007514

More Books

Students also viewed these Databases questions

Question

Question What is a Roth 403 (b) plan?

Answered: 1 week ago

Question

Question Can a Keogh plan fund be reached by the owners creditors?

Answered: 1 week ago