Question: Part # 1.3 Outline of a simple shell #include #include #define MAXLINE 80 /* The maximum length command */ int main(void) { char *args[MAXLINE/2 +
Part # 1.3 Outline of a simple shell
#include
#include
#define MAXLINE 80 /* The maximum length command */
int main(void)
{
char *args[MAXLINE/2 + 1]; /* command line arguments */
int should_run = 1; /* flag to determine when to exit program */
char *prog = NULL;
char **args = NULL;
while (should_run) {
printf(mysh);
fflush(stdout);
/**
*After reading user input, the steps are:
* (1) fork a child process using fork()
* (2) the child process will invoke execvp()
*/
int child_pid = fork();
if (child_pid == 0) { /* Im the child process, run program with parents I/O
exec(prog, args);
}
else { /* Im the parent: wait for the child to complete
wait(child_pid);
return 0;
}
}
return 0;
}
The above pseudocode illustrates the basic operation of a shell. The shell reads a command line from the input, and it forks a process to execute that command. UNIX fork automatically duplicates all open file descriptors in the parent, incrementing the kernels reference counts for those descriptors, so the input and output of the child is the same as the parent. The parent waits for the child to finish before it reads the next command to execute.
Because the commands to read and write to an open file descriptor are the same whether the file descriptor represents a keyboard, screen, file, device, or pipe, UNIX programs do not need to be aware of where their input is coming from, or where their output is going. This is helpful in a number of ways:
- A program can be a file of commands.
- A program can send its output to a file.
- A program can read its input from a file.
sample code
#include
#define MAXLINE 80
int main(int count, char *argv[]) { int length = MAXLINE/2+1; char *arg[length]; int should_run = 1; char U_input[MAXLINE[;
/* char *args[MAXLINE/2 + 1]; //command line arguments int should_run = 1; //flag to determine when to exit program char *prog = NULL; char **args = NULL; */ while (should_run) { cout << "mysh%"; if(!strcmp(U_Input,"exit/n") -- 0) {
printf("mysh"); fflush(stdout); int child_pid = fork(); if (child_pid == 0){ //im the child process, run the proogram with the parent's I/O exec(prog, args); } else{ //im the parent: wait for the child to complete wait(child_pid); return 0; } } return 0; }
Show/Run the Shell code CustomShell.cpp as it is described above and attach a screen shot
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
