Guest User

Untitled

a guest
Feb 18th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. /* Lab5.c */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/wait.h>
  7. int main(){
  8. pid_t pid;
  9. int i;
  10. int j;
  11. int numChild;
  12. for (i = 0; i < 5; i++){
  13. pid = fork();
  14. if (pid == 0){
  15. if (i%2 ==0){
  16. numChild = 4;
  17. }
  18. else{
  19. numChild = 5;
  20. }
  21. for(j = 0 ; j < numChild; j++){
  22. pid = fork();
  23. if (pid == 0){
  24. printf("I am subchild %d of child %d\n", j,i);
  25. exit(0);
  26. }
  27. }
  28. printf("I am child %d\n", i);
  29. return 0;
  30. }
  31. }
  32. while(wait(NULL) != -1);
  33. printf("I am parent\n");
  34. return 0;
  35. }
Add Comment
Please, Sign In to add comment