Advertisement
ptkrisada

signal example

Feb 27th, 2020
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.93 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <signal.h>
  3. #include <unistd.h>
  4.  
  5. void sig_handler(int signo) /* signal handler */
  6. {
  7.     if (signo == SIGINT)
  8.         printf("received SIGINT\n");
  9. }
  10.  
  11. int main(void)
  12. {
  13.     /* signal(3) can return only 3 dispositions i.e. SIG_DFL, SIG_IGN or SIG_ERR.  *
  14.      * After main() calling this fx, whenever SIGINT is delivered to the process,  *
  15.      * sig_handler fx will be invoked. This is called signal handler. And we can't *
  16.      * check the current disposition without catching the signal, unless we        *
  17.      * explicitly use sigaction(2).                                                *
  18.      * Note that we can't catch or ignore SIGKILL and SIGSTOP.                     */
  19.     if (signal(SIGINT, sig_handler) == SIG_ERR)
  20.         fprintf(stderr, "can't catch SIGINT\n");
  21.     /* A long wait so that we can easily issue a signal to this process. */
  22.     while (1)
  23.         sleep(1);
  24.  
  25.     return 0;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement