Advertisement
ptkrisada

signal implemention

Feb 27th, 2020
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.37 KB | None | 0 0
  1. /* signal.c: Don't compile this program. This is only the implementation of signal(3), *
  2.  * but without existence of main(). So it can't be compiled unless a specific flag -c  *
  3.  * is given to the compiler e.g. gcc -c. signal(3) complies with POSIX.1. But it is    *
  4.  * only a wrapper function. signal(3) is actually implemented using sigaction(2).      */
  5.  
  6. #include <signal.h>
  7.  
  8. typedef void Sigfunc(int);
  9.  
  10. Sigfunc *signal(int signo, Sigfunc *func)
  11. {
  12.     struct sigaction act, oact;
  13.  
  14.     act.sa_handler = func;
  15.     sigemptyset(&act.sa_mask); /* Empty signal set. */
  16.     act.sa_flags = 0;
  17.     /* the purpose of generating SIGALRM is normally to place a timeout *
  18.      * on an I/O operation. If SIGALRM caught, system calls will never  *
  19.      * restart regardless of SA_RESTART.                                */
  20.     if (signo == SIGALRM) {
  21. #ifdef SA_INTERRUPT
  22.         act.sa_flags |= SA_INTERRUPT; /* SunOS 4.x */
  23. #endif
  24.     } else {
  25. #ifdef SA_RESTART
  26.         act.sa_flags |= SA_RESTART; /* SVR4 & BSD */
  27. #endif
  28.     }
  29.     /* The 1st arg to sigaction(2) is signal number to be caught. *
  30.      * The 2nd arg is ptr to the current disposition.             *
  31.      * The 3rd arg is ptr to the old disposition.                 */
  32.     if (sigaction(signo, &act, &oact) == -1)
  33.         return SIG_ERR;
  34.     return oact.sa_handler; /* the previous disposition of the signal */
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement