Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
3.12 Including the initial parent process, how many processes are created by the program shown in Figure 3.32? #include <stdio.h> #include <unistd.h> int main() { int i; for (i = 0; i < 4; i++) fork(); return 0; } Figure 3.32 parent and 4 children 1-first child generates another three children First child generates another two children 1-1-1 First child generates another one child 1-1-1-1 one child generates nothing 1-1-2 second child generates nothing 1-2 second child generates another one child 1-2-1 one child generates nothing 1-3 last one generates nothing 2-second child generates another two children , 2-1 First child generates another one child 2-1-1 one child generates nothing 2-2 second child generates nothing 3-third child generates another one child , 3-1 one child generates nothing 4-last one generates nothing , so the number of processes including the parent Is 16 processes. 3.13 Explain the circumstances under which which the line of code marked printf("LINE J") in Figure 3.33 will be reached. #include <sys/types.h> #include <stdio.h> #include <unistd.h> 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; int i; for (i = 0; i < 4; i++) fork(); return 0; } Figure 3.33 when will “LINE J” be reached ?? when pid of fork() =0 it means that the current process is child. Besides, when the fork works but the execlp doesn`t work , that's when that line will be printed and only in the child process. If both the fork and the execlp worked, execlp replaces the contents of the child process with the /bin/ls executable and won`t be code of the original program after that.