Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Lab5.c */
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- int main(){
- pid_t pid;
- int i;
- int j;
- int numChild;
- for (i = 0; i < 5; i++){
- pid = fork();
- if (pid == 0){
- if (i%2 ==0){
- numChild = 4;
- }
- else{
- numChild = 5;
- }
- for(j = 0 ; j < numChild; j++){
- pid = fork();
- if (pid == 0){
- printf("I am subchild %d of child %d\n", j,i);
- exit(0);
- }
- }
- printf("I am child %d\n", i);
- return 0;
- }
- }
- while(wait(NULL) != -1);
- printf("I am parent\n");
- return 0;
- }
Add Comment
Please, Sign In to add comment