Question: Rewrite the program in Step 1., so that the child process runs the ls command, and that the parent parent process waits until the child

Rewrite the program in Step 1., so that the child process runs the ls command, and that the parent parent process waits until the child process terminates before it exits. Demonstrate your code to the 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 /* printf, stderr */
#include /* pid_t */
#include /* fork */
#include /* atoi */
#include /* 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 from keyboard for delay
printf(" Before forking. ");
pid = fork();
if (pid == -1) {
fprintf(stderr, "can't fork, error %d ", errno);
}
if (pid){
// Parent process
for (i=0;i<100;i++) {
printf("\t \t \t Parent Process %d ",i);
usleep(n);
}
}
else{
// Child process
for (i=0;i<100;i++) {
printf("Child process %d ",i);
usleep(n);
}
}
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!