Advertisement
Vladislav8653

8 lab

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