Advertisement
Guest User

Untitled

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