Vladislav8653

Untitled

Dec 26th, 2023
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <string.h>
  6. #include <sys/wait.h>
  7. #include <signal.h>
  8. #include <time.h>
  9. #include <sys/time.h>
  10.  
  11. int usr1AmountSend = 0;
  12. int usr2AmountSend = 0;
  13. int usr1AmountAccept = 0;
  14. int usr2AmountAccept = 0;
  15. int currProc;
  16.  
  17. void termProc(int sig){
  18. printf("(%d) PID - %d (PPID - %d) finished work after %d signal SIGUSR1 and %d signal SIGUSR2\n",
  19. currProc, getpid(), getppid(), usr1AmountSend,
  20. usr2AmountSend);
  21.  
  22. for (;;) {
  23. pid_t childPid = wait(NULL);
  24. if (childPid == -1) {exit(EXIT_SUCCESS);}
  25. }
  26.  
  27. }
  28.  
  29. void usr1Proc(int sig){usr1AmountAccept++;}
  30. void usr2Proc(int sig){usr2AmountAccept++;}
  31.  
  32. int main(){
  33. signal(SIGTERM, termProc);
  34. signal(SIGUSR1, usr1Proc);
  35. signal(SIGUSR2, usr2Proc);
  36.  
  37. pid_t pid_array[9];
  38.  
  39. currProc = 0;
  40. pid_array[currProc] = getpid();
  41. pid_t pid = fork();// process #1
  42. if (pid == 0){
  43. currProc = 1; // process #1
  44. pid_array[currProc] = getpid();
  45. pid = fork(); // process #2
  46. if (pid == 0){
  47. currProc = 2; // process #2
  48. pid_array[currProc] = getpid();
  49. pid = fork(); // process #3
  50. if (pid == 0){
  51. currProc = 3; // process #3
  52. pid_array[currProc] = getpid();
  53. pid = fork(); // process #7
  54. if (pid == 0){currProc = 7; pid_array[currProc] = getpid();} // process #7
  55. } else {
  56. pid = fork(); // process #4
  57. if (pid == 0){
  58. currProc = 4; // process #4
  59. pid_array[currProc] = getpid();
  60. pid = fork(); // process #6
  61. if (pid == 0){currProc = 6; pid_array[currProc] = getpid();} // process #6
  62. } else {
  63. pid = fork(); // process #5
  64. if (pid == 0){
  65. currProc = 5; // process #5
  66. pid_array[currProc] = getpid();
  67. pid = fork(); // process #8
  68. if (pid == 0){currProc = 8; pid_array[currProc] = getpid();} // process #8
  69. }
  70. }
  71. }
  72. }
  73.  
  74. for (int i = 0; i < 101; i++)
  75. {
  76.  
  77. switch (currProc)
  78. {
  79. case 1:
  80. kill(pid_array[2], SIGUSR2);
  81. usr2AmountSend++;
  82. break;
  83. case 2:
  84. kill(0, SIGUSR1);
  85. usr1AmountSend++;
  86. break;
  87. case 3:
  88. kill(pid_array[7], SIGUSR1);
  89. usr1AmountSend++;
  90. break;
  91. case 4:
  92. kill(pid_array[6], SIGUSR1);
  93. usr1AmountSend++;
  94. break;
  95. case 5:
  96. kill(pid_array[8], SIGUSR1);
  97. usr1AmountSend++;
  98. break;
  99. case 8:
  100. kill(pid_array[1], SIGUSR2);
  101. usr2AmountSend++;
  102. break;
  103. }
  104.  
  105. struct timeval currentTime;
  106. gettimeofday(&currentTime, NULL);
  107. char buffer[80];
  108. strftime(buffer, sizeof(buffer), "%H:%M:%S",
  109. localtime(&currentTime.tv_sec));
  110. sprintf(buffer, "%s.%06ld", buffer, currentTime.tv_usec);
  111.  
  112. printf("[%s] %d process (PID - %d|PPID - %d) send/accept USR1 (%d / %d) and USR2 (%d / %d)\n\n",
  113. buffer, currProc, getpid(), getppid(), usr1AmountSend, usr1AmountAccept, usr2AmountSend, usr2AmountAccept);
  114. }
  115. kill(0, SIGTERM);
  116. }
  117.  
  118.  
  119.  
  120. return 0;
  121. }
  122.  
  123.  
Add Comment
Please, Sign In to add comment