Advertisement
Guest User

Untitled

a guest
May 19th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/wait.h>
  3. #include <sys/types.h>
  4. main()
  5. {
  6. int k, status;
  7. int num;
  8. int num1;
  9. int num2;
  10. int num3;
  11. int num4;
  12. int char;
  13.  
  14. printf("I'm the original process. PID=%d, PPID=%d.\n", getpid(), getppid());
  15.  
  16. num=fork();
  17.  
  18. if ( num !=0 )
  19. {
  20. waitpid (num2, &status, WUNTRACED);
  21. printf("I'm the parent (P0).PID=%d, PPID=%d.\n", getpid(), getppid());
  22. printf("I have a child (P1) with PID %d.\n", num);
  23. num1=fork();
  24. if ( num1 !=0 ) {
  25. printf("I'm the parent (P1).PID=%d, PPID=%d.\n", getpid(), getppid());
  26. }
  27. else {
  28. printf("I'm the new child (P2). PID=%d, PPID=%d.\n", getpid(), getppid());
  29. num2=fork();
  30. if ( num2 !=0 )
  31. k=wait(&status);
  32. {
  33. printf("I'm the parent (P2).PID=%d, PPID=%d.\n", getpid(), getppid());
  34. printf("I'm the first child (P3) with PID %d.\n", num2);
  35. num3=fork();
  36. if ( num3 !=0 ) {
  37. k=wait(&status);
  38. printf("I'm the parent (P2).PID=%d, PPID=%d.\n", getpid(), getppid()) ;
  39. }
  40. else {
  41. printf("I'm the second child (P4). PID=%d, PPID=%d.\n", getpid(), getppid());
  42. num4=fork();
  43. if ( num4 !=0 ) {
  44. printf("I'm the parent (P2).PID=%d, PPID=%d.\n", getpid(), getppid());
  45. }
  46. else {
  47. printf("I'm the third child (P5). PID=%d, PPID=%d.\n", getpid(), getppid());
  48. }
  49. }
  50. }
  51. }
  52. }
  53. else
  54. {
  55. printf("I'm the child. PID=%d, PPID=%d.\n", getpid(), getppid());
  56. }
  57. printf("PID %d terminates.\n", getpid());
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement