Question
please someone help me with this assignment. * Figure 3.33 */ #include #include #include #include int main() { pid_t pid; /* fork a child process
please someone help me with this assignment.
* Figure 3.33 */
#include
int main() { pid_t pid; /* fork a child process */ pid = fork();
if (pid < 0) { /* error occurred */ fprintf(stderr, "Fork Failed"); return 1; } else if (pid == 0) { /* child process */ execlp("/bin/ls","ls",NULL); printf("LINE J"); } else { /* parent process */ /* parent will wait for the child to complete */ wait(NULL); printf("Child Complete"); }
return 0; }
Compilation using: g++ -g Wall std=c++14 o NameOfExecutable source.cpp
Part #1.1:Show/Run the Shell code Fig3-33.cpp as it is described above and attach a screen shot
Part #1.2: Modify the code as follows
- Instead of execlp, make use of execvp as described below:
The first task is to modify the main() function in the above code so that a child process is forked and executes the command specified by the user. This will require parsing what the user has entered into separate tokens and storing the tokens in an array of character strings(agrs). For example, if the user enters the command ps ael at the mysh> prompt, the values stored in the args array are:
args[0] = ps
args[1] = -ael
args[2] = NULL
This args array will be passed to the execvp() function, which has the following prototype
execvp(char *command, char *params[]);
Here, command represents the command to be performed and params stores the parameters for this command. For this part of the Lab #6, the execvp() function should be invoked as execvp(args[0], args).
Another example to run command from shell: ls l | wc l
char *ls_args[] = {"ls", "-l", NULL}; //for parent process
char *wc_args[] = {"wc", "-l", NULL}; //for child process
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.
- 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