Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. int main(void)
  5. {
  6. char pid[10];
  7. sprintf(pid, "%d", getpid());
  8. int myPID = getpid();
  9. printf("Parent PID: %d\n", getpid());
  10.  
  11. if(fork()==0) // P1
  12. {
  13. printf("P1 PID: %d, PPID: %d\n", getpid(), getppid());
  14. if(fork() == 0) // P1A
  15. {
  16. printf("P1A PID: %d, PPID: %d\n", getpid(), getppid());
  17.  
  18. if(fork() == 0) // P1Aa
  19. {
  20. printf("P1Aa PID: %d, PPID: %d\n", getpid(), getppid());
  21. sleep(2);
  22. return 0;
  23. }
  24.  
  25. if(fork() == 0) // P1Ab
  26. {
  27. printf("P1Ab PID: %d, PPID: %d\n", getpid(), getppid());
  28. sleep(2);
  29. return 0;
  30. }
  31. sleep(2);
  32. return 0;
  33. }
  34.  
  35.  
  36. if(fork() == 0) // P2B
  37. {
  38. printf("P1B PID: %d, PPID: %d\n", getpid(), getppid());
  39. sleep(2);
  40. return 0;
  41. }
  42.  
  43.  
  44. if(fork() == 0) // P2A
  45. {
  46. printf("P1C PID: %d, PPID: %d\n", getpid(), getppid());
  47. sleep(2);
  48. return 0;
  49. }
  50. sleep(2);
  51. return 0;
  52. }
  53.  
  54.  
  55.  
  56. if(fork()==0) // P2
  57. {
  58. printf("P2 PID: %d, PPID: %d\n", getpid(), getppid());
  59. if(fork() == 0) // P1A
  60. {
  61. printf("P2A PID: %d, PPID: %d\n", getpid(), getppid());
  62. sleep(2);
  63. return 0;
  64. }
  65. execlp("pstree", "pstree", "-c", pid, (char *)NULL); //pstrees
  66. sleep(2);
  67. return 0;
  68. }
  69.  
  70.  
  71.  
  72. if(fork()==0) // P3
  73. {
  74. printf("P3 PID: %d, PPID: %d\n", getpid(), getppid());
  75.  
  76. if(fork() == 0) // P1A
  77. {
  78. printf("P3A PID: %d, PPID: %d\n", getpid(), getppid());
  79. sleep(2);
  80. return 0;
  81. }
  82.  
  83. if(fork() == 0)
  84. {
  85. printf("P3B PID: %d, PPID: %d\n", getpid(), getppid());
  86. sleep(2);
  87. return 0;
  88. }
  89. sleep(2);
  90. return 0;
  91. }
  92. sleep(2);
  93. return 0;
  94.  
  95.  
  96.  
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement