Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 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("P1 PID: %d\n", getpid());
  10. if(fork()==0) // P1
  11. {
  12. printf("P2 PID: %d, PPID: %d\n", getpid(), getppid());
  13. if(fork() == 0) // P1A
  14. {
  15. printf("P4 PID: %d, PPID: %d\n", getpid(), getppid());
  16. sleep(2);
  17. return 0;
  18. }
  19. if(fork() == 0) // P1B
  20. {
  21. printf("P5 PID: %d, PPID: %d\n", getpid(), getppid());
  22. sleep(2);
  23. return 0;
  24. }
  25. if(fork() == 0) // P1C
  26. {
  27. printf("P6 PID: %d, PPID: %d\n", getpid(), getppid());
  28. if(fork() == 0)
  29. {
  30. execlp("pstree", "pstree", "-p", pid, (char *)NULL); //pstree
  31. sleep(2);
  32. return 0;
  33. }
  34. sleep(2);
  35. return 0;
  36. }
  37. sleep(2);
  38. return 0;
  39. }
  40. if(fork()==0) // P2
  41. {
  42. printf("P2 PID: %d, PPID: %d\n", getpid(), getppid());
  43. if(fork() == 0) // P1A
  44. {
  45. printf("P7 PID: %d, PPID: %d\n", getpid(), getppid());
  46. sleep(2);
  47. return 0;
  48. }
  49. if(fork() == 0) // P1B
  50. {
  51. printf("P8 PID: %d, PPID: %d\n", getpid(), getppid());
  52. sleep(2);
  53. return 0;
  54. }
  55. if(fork() == 0) // P1C
  56. {
  57. printf("P9 PID: %d, PPID: %d\n", getpid(), getppid());
  58. sleep(2);
  59. return 0;
  60. }
  61. sleep(2);
  62. return 0;
  63. }
  64.  
  65. sleep(2);
  66. return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement