Rewrite Program Step 1 Child Process Runs Ls Command Parent Parent Process Waits Child Pro Q43893469
Rewrite the program in Step 1., so that the child process runsthe ls command, and that the parent parent process waits until thechild process terminates before it exits. Demonstrate your code tothe TA. You may use the following code snippet.else if(pid == 0){execlp(“/bin/ls”, “ls”, NULL);}else{wait(NULL);printf(“Child Complete”);exit(0);}
Step 1:
#include <stdio.h> /* printf, stderr */#include <sys/types.h> /* pid_t */#include <unistd.h> /* fork */#include <stdlib.h> /* atoi */#include <errno.h> /* errno *//* main function with command-line arguments to pass */int main(int argc, char *argv[]) {pid_t pid;int i, n = atoi(argv[1]); // n microseconds to input fromkeyboard for delayprintf(“n Before forking.n”);pid = fork();if (pid == -1) {fprintf(stderr, “can’t fork, error %dn”, errno);}if (pid){// Parent processfor (i=0;i<100;i++) {printf(“t t t Parent Process %d n”,i);usleep(n);}}else{// Child processfor (i=0;i<100;i++) {printf(“Child process %dn”,i);usleep(n);}}return 0;}
Expert Answer
Answer to Rewrite the program in Step 1., so that the child process runs the ls command, and that the parent parent process waits …
OR