Question
C++ and Linux Part 3: Working With External Commands (15 points) Modify the replaceProcess() function in the file named Processes.cpp as follows: The parent process
C++ and Linux
Part 3: Working With External Commands (15 points)
Modify the replaceProcess() function in the file named Processes.cpp as follows:
The parent process will use the fork() function to create a child process. This step has been done for you.
The child process must then change its memory image to a different program by using the execvp system call (http://linux.die.net/man/3/execvp). The parameter args that has been passed to the replaceProcess() function is the array of parameters to be passed to execvp, telling it which program to execute and what parameters to pass to that program.
For example, the test code provided to you executes the ls (directory list) program with the parameter -al by setting the args array as follows:
char * args[3] = {(char * )"ls", (char * )"-al", NULL}; IMPORTANT: Although the test code executes the ls program, we must be able to change the command that execvp executes. So, DO NOT hardcode the command to be passed to execvp. Simply use the args array provided.
Finally, in the parent process, you must make sure to invoke the necessary system call to wait for the child process to terminate. Once the child terminates, exit the program.
// Part 3: Working With External Commands" void replaceProcess(char * args[]) { // Spawn a process to execute the user's command. pid_t id = fork(); // TODO: Add your code here }
#endif /* TestProg_cpp */
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