--sas

signals-number

Dec 4th, 2019
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.50 KB | None | 0 0
  1. // program for input
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7.  
  8. #include <sys/stat.h>
  9. #include <signal.h>
  10.  
  11. #define ER(number) {          \
  12.     printf("E%d\n", number);  \
  13.     exit(-1);                 \
  14. }
  15.  
  16. int i;
  17. int j = 0;
  18. pid_t pid1;
  19.  
  20. void handler(int n) {
  21.     if (j == 33) return;
  22.     if (n == SIGUSR1) {
  23.         int bit;
  24.         bit = (i<<j)>>31;
  25.         if (bit == -1) {
  26.             if (kill(pid1, SIGUSR1) < 0) ER(1);
  27.         }
  28.         else if (bit == 0) {
  29.             if (kill(pid1, SIGUSR2) < 0) ER(2);
  30.         }
  31.         else ER(3);
  32.  
  33.         ++j;
  34.     }
  35. }
  36.  
  37. int main() {
  38.     printf("%d\n", getpid());
  39.     scanf("%d", &pid1);
  40.     scanf("%d", &i);
  41.  
  42.     signal(SIGUSR1, handler);
  43.  
  44.     handler(SIGUSR1);
  45.  
  46.     while(j < 32);
  47.  
  48.     return 0;
  49. }
  50.  
  51.  
  52.  
  53. // program for output
  54.  
  55. #include <stdio.h>
  56. #include <stdlib.h>
  57. #include <unistd.h>
  58. #include <sys/types.h>
  59.  
  60. #include <sys/stat.h>
  61. #include <signal.h>
  62.  
  63. #define ER(number) {          \
  64.     printf("E%d\n", number);  \
  65.     exit(-1);                 \
  66. }
  67.  
  68. int j = 0;
  69. int i = 0;
  70. pid_t pid0;
  71.  
  72. void handler (int n) {
  73.     if (j == 32) return;
  74.     i <<= 1;
  75.     if (n == SIGUSR1)
  76.         i += 1;
  77.     // if (n == SIGUSR2) i += 0;
  78.     if (kill (pid0, SIGUSR1) < 0) ER(1);
  79.     ++j;
  80. }
  81.  
  82. int main() {
  83.     printf("%d\n", getpid());
  84.     scanf("%d", &pid0);
  85.  
  86.     signal(SIGUSR1, handler);
  87.     signal(SIGUSR2, handler);
  88.  
  89.     while(j < 32);
  90.  
  91.     printf("%d\n", i);
  92.  
  93.     return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment