Advertisement
Vladislav8653

8 6var

Dec 24th, 2023
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <sys/wait.h>
  6. #include <signal.h>
  7.  
  8.  
  9. #define createProc(parent, child) \
  10. if (currProc == parent) { \
  11. pid##child = fork(); \
  12. if (pid##child == 0) { \
  13. pid##child = getpid();\
  14. currProc = child; \
  15. } \
  16. }
  17.  
  18. int currProc;
  19. int sendCount_1, sendCount_2;
  20. int receiveCount_1, receiveCount_2;
  21. pid_t pid1, pid2, pid3, pid4, pid5, pid6, pid7, pid8;
  22.  
  23. void USR1_handler(int sig) {
  24. receiveCount_1++;
  25. }
  26.  
  27. void USR2_handler(int sig) {
  28. receiveCount_2++;
  29. }
  30.  
  31. void SIGTERM_handler(int sig) {
  32. printf("\n\nПроцесс: %d, pid: %d, ppid: %d остановился. Отправил %d usr1 и %d usr2\n\n", currProc, getpid(), getppid(), sendCount_1, sendCount_2);
  33. if (currProc != 1) {
  34. kill(getpid(), SIGKILL);
  35. }
  36. else {
  37. pid_t childPid = 0;
  38. while (childPid != -1) {
  39. childPid = wait(NULL);
  40. }
  41. exit(0);
  42. }
  43. }
  44.  
  45. void printStat() {
  46. if (currProc == 1 || currProc == 5 || currProc == 6 || currProc == 7 || currProc == 8) {
  47. printf("\nПроцесс: %d, pid: %d, ppid: %d. Отправил %d USR1 и получил %d USR1. Time: %d", currProc, getpid(), getppid(), sendCount_1, receiveCount_1, time(NULL));
  48. }
  49. else if (currProc == 2) {
  50. printf("\nПроцесс: %d, pid: %d, ppid: %d. Отправил %d USR2 и получил %d USR1. Time: %d", currProc, getpid(), getppid(), sendCount_2, receiveCount_1, time(NULL));
  51. }
  52. else if (currProc == 3 || currProc == 4) {
  53. printf("\nПроцесс: %d, pid: %d, ppid: %d. Отправил %d USR1 и получил %d USR2. Time: %d", currProc, getpid(), getppid(), sendCount_1, receiveCount_2, time(NULL));
  54. }
  55. }
  56.  
  57. void send(int proc, int sig, int groupId, int* counter) {
  58. if (currProc == proc) {
  59. while (*counter != 101) {
  60. kill(groupId, sig);
  61. (*counter)++;
  62. printStat();
  63. }
  64. printf("\n\nПроцесс: %d, pid: %d, ppid: %d остановился. Отправил %d usr1 и %d usr2\n\n", currProc, getpid(), getppid(), sendCount_1, sendCount_2);
  65. kill(groupId, SIGTERM);
  66. exit(0);
  67. }
  68. }
  69.  
  70. int main() {
  71. signal(SIGUSR1, USR1_handler);
  72. signal(SIGUSR2, USR2_handler);
  73. signal(SIGTERM, SIGTERM_handler);
  74.  
  75. createProc(0, 1);
  76. createProc(1, 2);
  77. createProc(2, 3);
  78. createProc(2, 4);
  79. createProc(4, 5);
  80. createProc(3, 6);
  81. createProc(6, 7);
  82. createProc(7, 8);
  83.  
  84. sleep(1);
  85.  
  86. if (currProc == 2) {
  87. setpgid(pid3, pid3);
  88. setpgid(pid4, pid3);
  89. }
  90.  
  91. sleep(1);
  92.  
  93. send(1, SIGUSR1, pid2, &sendCount_1);
  94. send(2, SIGUSR2, pid3, &sendCount_2);
  95. send(4, SIGUSR1, pid5, &sendCount_1);
  96. send(3, SIGUSR1, pid6, &sendCount_1);
  97. send(6, SIGUSR1, pid7, &sendCount_1);
  98. send(7, SIGUSR1, pid8, &sendCount_1);
  99. send(8, SIGUSR1, pid1, &sendCount_1);
  100. }
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement