Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <memory.h>
  4. #include <signal.h>
  5. #include <zconf.h>
  6.  
  7.  
  8. volatile int flag = 0;
  9.  
  10. void action(int sig) {
  11.     static int val = 0;
  12.     flag = 1;
  13.     if (sig == SIGUSR1) {
  14.         val += 5;
  15.     } else if (sig == SIGUSR2) {
  16.         val -= 4;
  17.     }
  18.     printf("%d %d\n", sig, val);
  19.     fflush(stdout);
  20.     if (val < 0) {
  21.         exit(0);
  22.     }
  23. }
  24.  
  25.  
  26. int main(int argc, char *argv[]) {
  27.     printf("%d\n", getpid());
  28.     fflush(stdout);
  29.     struct sigaction act = {};
  30.     act.sa_handler = action;
  31.     sigset_t set, oldset;
  32.     sigemptyset(&set);
  33.     sigaddset(&set, SIGUSR1);
  34.     sigaddset(&set, SIGUSR2);
  35.     act.sa_mask = set;
  36.     sigaction(SIGUSR1, &act, 0);
  37.     sigaction(SIGUSR2, &act, 0);
  38.     while (1) {
  39.         while (!flag) {
  40.             sigsuspend(&oldset);
  41.         }
  42.         flag = 0;
  43.     }
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement