Advertisement
Benjamin_Loison

C signal kill

Nov 20th, 2020 (edited)
697
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <sys/wait.h>
  6. #include <termios.h>
  7.  
  8. void print0()
  9. {
  10.     printf("a");
  11. }
  12.  
  13. void print1()
  14. {
  15.     printf("b");
  16. }
  17.  
  18. int main(void)
  19. {
  20.     struct termios mode;
  21.     tcgetattr(0,&mode);
  22.     mode.c_lflag &= ~ECHO;
  23.     mode.c_lflag &= ~ICANON;
  24.     tcsetattr(0, TCSANOW, &mode);
  25.     setbuf(stdout,NULL);
  26.  
  27.     pid_t pid = fork();
  28.  
  29.     if(pid == 0)
  30.     {
  31.         signal(SIGUSR1, print0);
  32.         signal(SIGUSR2, print1);
  33.         //alarm(100);
  34.         while(1)
  35.             pause();
  36.     }
  37.     else
  38.     {
  39.         while(1)
  40.         {
  41.             char c = getchar();
  42.             if(c == '0') kill(pid, SIGUSR1);
  43.             else if(c == '1') kill(pid, SIGUSR2);
  44.         }
  45.     }
  46.  
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement