Advertisement
ptkrisada

sigprocmask

Feb 28th, 2020
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.13 KB | None | 0 0
  1. /* To ignore the signal. Sometimes we don't want a process *
  2.  * to receive some certain signal(s). We can ignore those  *
  3.  * signal(s). For instance, SIGINT (ctrl-C), which is used *
  4.  * to stop the process and the process can't continue. But *
  5.  * SIGINT differs from SIGKILL and SIGSTOP as SIGINT can   *
  6.  * be caught or ignored.                                   *
  7.  * The following code, simply ignores SIGINT.              */
  8. #include <signal.h>
  9. signal(SIGINT, SIG_IGN);
  10. /* After this code is reached any ctrl-C keyboard hit      *
  11.  * can't terminate the process.                            */
  12.  
  13. /* ------------------------------------------------------- */
  14.  
  15. /* To block signal. In case we want to defer or postpone   *
  16.  * delivery of some signals. We can simply do ...          */
  17.  
  18.     /* variables are the set of signals i.e. oldmask & newmask */
  19.     sigset_t oldmask, newmask; /* sigset_t is type of the set. */
  20.  
  21.     /* Make the set empty. */
  22.     sigemptyset(&newmask);
  23.  
  24.     /* Add SIGINT to the set 'newmask'. */
  25.     sigaddset(&newmask, SIGINT);
  26.  
  27.     /* Add SIGQUIT to the set 'newmask'. */
  28.     sigaddset(&newmask, SIGQUIT);
  29.  
  30.     /* Add SIGTSTP to the set 'newmask'. */
  31.     sigaddset(&newmask, SIGTSTP);
  32.  
  33.     /* Call sigprocmask(2) to defer those signals in 'newmask'. *
  34.      * The 1st arg can be either SIG_BLOCK, SIG_UNBLOCK or      *
  35.      * SIG_SETMASK. In this example we want to block signals    *
  36.      * (actually defer or postpone delivery of the signal to    *
  37.      * the process). This example will block signals listed in  *
  38.      * 'newmask' and save the previous mask in 'oldmask'.       */
  39.     sigprocmask(SIG_BLOCK, &newmask, &oldmask);
  40.     /* Do some stuff here. */
  41.     /* All signals in the set, if any, are temporarily blocked. */
  42.  
  43.     /* We new restore 'oldmask' and allow all pending signals   *
  44.      * to deliver to the process. The 3rd arg is simply NULL.   */
  45.     sigprocmask(SIG_SETMASK, &oldmask, NULL);
  46.     /* After this code is reached, the signals in the set are   *
  47.      * unblocked. All pending signals are delivered to the      *
  48.      * process.                                                 */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement