Question
#include #include #define MAXLINE 80 /* The maximum length command */ int main(void) { char *args[MAXLINE/2 + 1]; /* command line arguments */ int should_run
#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.
The output of one program can be the input to another program.
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