anechka_ne_plach

sm18

Apr 18th, 2022 (edited)
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.04 KB | None | 0 0
  1. 14-4
  2.  
  3. 1 │ #include <stdio.h>
  4. 2 │ #include <signal.h>
  5. 3 │ #include <unistd.h>
  6. 4 │ #include <stdlib.h>
  7. 5 │ #include <sys/types.h>
  8. 6 │ #include <sys/stat.h>
  9. 7 │ #include <fcntl.h>
  10. 8 │ #include <errno.h>
  11. 9 │ #include <string.h>
  12. 10 │
  13. 11 │ volatile sig_atomic_t current_signal = 0;
  14. 12 │
  15. 13 │ void handler(int s) {
  16. 14 │ current_signal = s;
  17. 15 │ }
  18. 16 │
  19. 17 │ int main(int argc, char* argv[]) {
  20. 18 │ sigset_t sigset, empty;
  21. 19 │ sigemptyset(&sigset);
  22. 20 │ sigemptyset(&empty);
  23. 21 │ sigaddset(&sigset, SIGTERM);
  24. 22 │ for (int i = 1; i <= 20; ++i) {
  25. 23 │ sigaddset(&sigset, SIGRTMIN + (i - 1));
  26. 24 │ }
  27. 25 │ sigprocmask(SIG_BLOCK, &sigset, NULL);
  28. 26 │
  29. 27 │ sigaction(SIGTERM, &(struct sigaction) { .sa_handler=handler }, NULL);
  30. 28 │ for (int i = 1; i <= 20; ++i) {
  31. 29 │ sigaction(SIGRTMIN + (i - 1), &(struct sigaction) { .sa_handler=handler }, NULL);
  32. 30 │ }
  33. 31 │
  34. 32 │ int fd[argc];
  35. 33 │ long long ans[argc];
  36. 34 │ char buf[argc][17];
  37. 35 │ int len[argc];
  38. 36 │ memset(ans, 0, sizeof(ans));
  39. 37 │ memset(buf, 0, sizeof(buf));
  40. 38 │ memset(len, 0, sizeof(len));
  41. 39 │ for (int i = 1; i < argc; ++i) {
  42. 40 │ fd[i] = open(argv[i], O_RDONLY);
  43. 41 │ }
  44. 42 │
  45. 43 │ int already_triggered = 0;
  46. 44 │
  47. 45 │ printf("%d\n", getpid());
  48. 46 │ fflush(stdout);
  49. 47 │
  50. 48 │ while (1) {
  51. 49 │ if (!already_triggered) {
  52. 50 │ sigsuspend(&empty);
  53. 51 │ } else {
  54. 52 │ already_triggered = 0;
  55. 53 │ }
  56. 54 │ if (current_signal == SIGTERM) {
  57. 55 │ for (int i = 1; i < argc; ++i) {
  58. 56 │ printf("%lld\n", ans[i]);
  59. 57 │ fflush(stdout);
  60. 58 │ if (fd[i] >= 0) {
  61. 59 │ close(fd[i]);
  62. 60 │ }
  63. 61 │ }
  64. 62 │ _exit(0);
  65. 63 │ } else {
  66. 64 │ int cur_fd = current_signal - (int)SIGRTMIN + 1;
  67. 65 │ if (cur_fd <= 0 || cur_fd >= argc || fd[cur_fd] < 0) {
  68. 66 │ continue;
  69. 67 │ }
  70. 68 │ while (1) {
  71. 69 │ sigprocmask(SIG_UNBLOCK, &sigset, NULL);
  72. 70 │ errno = 0;
  73. 71 │ int value = read(fd[cur_fd], buf[cur_fd] + len[cur_fd], 16 - len[cur_fd]);
  74. 72 │ sigprocmask(SIG_BLOCK, &sigset, NULL);
  75. 73 │ if (errno == EINTR || value < 0) {
  76. 74 │ already_triggered = 1;
  77. 75 │ break;
  78. 76 │ }
  79. 77 │ if (value == 0) {
  80. 78 │ close(fd[cur_fd]);
  81. 79 │ fd[cur_fd] = -1;
  82. 80 │ break;
  83. 81 │ }
  84. 82 │ len[cur_fd] += value;
  85. 83 │ //// printf("%d\n", value);
  86. 84 │ if (len[cur_fd] == 16) {
  87. 85 │ ans[cur_fd] += strtoll(buf[cur_fd], NULL, 10);
  88. 86 │ len[cur_fd] = 0;
  89. 87 │ }
  90. 88 │ }
  91. 89 │ }
  92. 90 │ }
  93. 91 │ }
  94.  
  95.  
  96. 14-5
  97. 1 │ #include <stdio.h>
  98. 2 │ #include <signal.h>
  99. 3 │ #include <unistd.h>
  100. 4 │ #include <stdlib.h>
  101. 5 │ #include <sys/types.h>
  102. 6 │ #include <sys/wait.h>
  103. 7 │ #include <sys/stat.h>
  104. 8 │ #include <sys/signalfd.h>
  105. 9 │ #include <fcntl.h>
  106. 10 │
  107. 11 │ int n;
  108. 12 │
  109. 13 │ void work(int my_id, FILE* file1, FILE* file2, int sfd) {
  110. 14 │ while (1) {
  111. 15 │ struct signalfd_siginfo sss;
  112. 16 │ read(sfd, &sss, sizeof(sss));
  113. 17 │
  114. 18 │ int val;
  115. 19 │ fscanf(file1, "%d", &val);
  116. 20 │ if (val > n) {
  117. 21 │ return;
  118. 22 │ }
  119. 23 │ printf("%d %d\n", my_id, val);
  120. 24 │ fflush(stdout);
  121. 25 │ ++val;
  122. 26 │ fprintf(file2, "%d\n", val);
  123. 27 │ fflush(file2);
  124. 28 │
  125. 29 │ kill(sss.ssi_pid, SIGUSR1);
  126. 30 │ if (val >= n) {
  127. 31 │ return;
  128. 32 │ }
  129. 33 │ }
  130. 34 │ }
  131. 35 │
  132. 36 │ int main(int argc, char* argv[]) {
  133. 37 │ sigset_t ss;
  134. 38 │ sigemptyset(&ss);
  135. 39 │ sigaddset(&ss, SIGUSR1);
  136. 40 │ sigprocmask(SIG_BLOCK, &ss, NULL);
  137. 41 │ int sfd = signalfd(-1, &ss, 0);
  138. 42 │
  139. 43 │ n = strtol(argv[1], NULL, 10);
  140. 44 │ if (n > 0) {
  141. 45 │ int pfd[2];
  142. 46 │ pipe(pfd);
  143. 47 │ FILE* file1 = fdopen(pfd[0], "r");
  144. 48 │ FILE* file2 = fdopen(pfd[1], "w");
  145. 49 │
  146. 50 │ pid_t pid1;
  147. 51 │ if (!(pid1 = fork())) {
  148. 52 │ work(1, file1, file2, sfd);
  149. 53 │ fclose(file1);
  150. 54 │ fclose(file2);
  151. 55 │ close(sfd);
  152. 56 │ _exit(0);
  153. 57 │ }
  154. 58 │
  155. 59 │ if (!fork()) {
  156. 60 │ fprintf(file2, "1\n");
  157. 61 │ fflush(file2);
  158. 62 │ kill(pid1, SIGUSR1);
  159. 63 │
  160. 64 │ work(2, file1, file2, sfd);
  161. 65 │ fclose(file1);
  162. 66 │ fclose(file2);
  163. 67 │ close(sfd);
  164. 68 │ _exit(0);
  165. 69 │ }
  166. 70 │ while (wait(NULL) > 0) {}
  167. 71 │ fclose(file1);
  168. 72 │ fclose(file2);
  169. 73 │ }
  170. 74 │
  171. 75 │ close(sfd);
  172. 76 │ }
Add Comment
Please, Sign In to add comment