Advertisement
wowonline

Untitled

Dec 12th, 2021
708
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.97 KB | None | 0 0
  1. #include <signal.h>
  2. #include <unistd.h>
  3. #include <sys/wait.h>
  4. #include <fcntl.h>
  5. #include <stdio.h>
  6.  
  7. enum { BITS = 8 };
  8.  
  9. volatile sig_atomic_t flag = 0;
  10. volatile sig_atomic_t bit = -1;
  11.  
  12. void
  13. handler1(int s)
  14. {
  15.     bit = 0;
  16. }
  17.  
  18. void
  19. handler2(int s)
  20. {
  21.     bit = 1;
  22. }
  23.  
  24. void
  25. handler3(int s)
  26. {
  27.     flag = 1;
  28. }
  29.  
  30. void
  31. handler4(int s)
  32. {
  33.     _exit(0);
  34. }
  35.  
  36. int
  37. main(int argc, char *argv[])
  38. {
  39.     sigset_t s1;
  40.     sigemptyset(&s1);
  41.     sigaddset(&s1, SIGUSR1);
  42.     sigaddset(&s1, SIGUSR2);
  43.     sigaddset(&s1, SIGALRM);
  44.     sigaddset(&s1, SIGIO);
  45.     sigprocmask(SIG_BLOCK, &s1, NULL);
  46.     unsigned char byte;
  47.     pid_t pid1, pid2;
  48.     int fds[2];
  49.     if (pipe(fds) == -1) {
  50.         return 1;
  51.     }
  52.     pid1 = fork();
  53.     if (pid1 == -1) {
  54.         return 1;
  55.     } else if (!pid1) {    // son 1 process
  56.         close(fds[1]);
  57.         sigset_t s2;
  58.         sigemptyset(&s2);
  59.         sigaction(SIGUSR1, &(struct sigaction){ .sa_handler = handler1, .sa_flags = SA_RESTART }, NULL);
  60.         sigaction(SIGUSR2, &(struct sigaction){ .sa_handler = handler2, .sa_flags = SA_RESTART }, NULL);
  61.         sigaction(SIGIO, &(struct sigaction){ .sa_handler = handler4, .sa_flags = SA_RESTART }, NULL);
  62.         if (read(fds[0], &pid2, sizeof(pid2)) != sizeof(pid2)) {
  63.             _exit(1);
  64.         }
  65.         close(fds[0]);
  66.         int bitcnt = 1;
  67.         int output = 0;
  68.  
  69.         while (1) {
  70.             while (bit == -1) {
  71.                 sigsuspend(&s2);
  72.             }
  73.             output += bit;
  74.             if (bitcnt == 8) {
  75.                 putchar(output);
  76.                 fflush(stdout);
  77.                 bitcnt = 0;
  78.                 output = 0;
  79.             }
  80.             output <<= 1;
  81.             bit = -1;
  82.             bitcnt++;
  83.             kill(pid2, SIGALRM);
  84.         }
  85.         _exit(0);
  86.     }
  87.    
  88.     pid2 = fork();
  89.     if (pid2 == -1) {
  90.         return 1;
  91.     } else if (!pid2) {     // son 2 process
  92.         sigset_t s2;
  93.         sigemptyset(&s2);
  94.         sigaction(SIGALRM, &(struct sigaction){ .sa_handler = handler3, .sa_flags = SA_RESTART }, NULL);
  95.         close(fds[0]);
  96.         pid2 = getpid();
  97.         if (write(fds[1], &pid2, sizeof(pid2)) != sizeof(pid2)) {
  98.             _exit(1);
  99.         }
  100.         close(fds[1]);
  101.         int fd = open(argv[1], O_RDONLY);
  102.         if (fd == -1) {
  103.             _exit(1);
  104.         }
  105.         while (read(fd, &byte, sizeof(byte)) == sizeof(byte)) {
  106.             for (int i = 0; i < BITS; ++i) {
  107.                 if (byte & 0x80) {
  108.                     kill(pid1, SIGUSR2);
  109.                 } else {
  110.                     kill(pid1, SIGUSR1);
  111.                 }
  112.                 while (!flag) {
  113.                     sigsuspend(&s2);
  114.                 }
  115.                 flag = 0;
  116.                 byte <<= 1;
  117.             }
  118.         }
  119.         close(fd);
  120.         kill(pid1, SIGIO);
  121.         _exit(0);
  122.     }
  123.  
  124.     close(fds[0]);
  125.     close(fds[1]);
  126.     waitpid(pid1, NULL, 0);
  127.     waitpid(pid2, NULL, 0);
  128.  
  129.     return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement