Question: How would you modify the following code so that the parent process waits for all children to finish execution? #include #include #include int main (int
How would you modify the following code so that the parent process waits for all children to finish execution?
#include
int main (int argc, char *argv[]) { pid_t childpid = 0; int i, n; if (argc != 2){ /* check for valid number of command-line arguments */ fprintf(stderr, "Usage: %s processes ", argv[0]); return 1; } n = atoi(argv[1]); for (i = 1; i < n; i++) if ((childpid = fork()) <= 0) break; fprintf(stderr, "i:%d process ID:%ld parent ID:%ld child ID:%ld ", i, (long)getpid(), (long)getppid(), (long)childpid); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
