Linux signal Masking

Source: Internet
Author: User
Tags signal handler

<span style= "FONT-SIZE:18PX;" > #include <sys/types.h> #include <unistd.h> #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <errno.h>/*sigemptyset (&newmask);//Get an empty shielded signal set Sigfillset (&newmask);// Gets the masked signal set that masks all the signals. In addition to the two Sigkill sigstopsigpending (&pendmask);//Gets the current set of masked signals in the process Sigaddset (&newmask, sigquit);//air shield signal set increased sigquit (Sigprocmask (Sig_block, &newmask, &oldmask) < 0)//Set (Sigprocmask (Sig_setmask, &oldmask, NULL) < 0)// Sets the original masked signal set (Sigismember (&fillmask, Sigquit))//infers whether Sigquit is in the current masked signal set */static voidsig_quit (int); Intmain (void) { Sigset_tnewmask, Oldmask, Pendmask,fillmask;if (Signal (sigquit, sig_quit) = = Sig_err)//Set signal processing function perror ("Can ' t catch Sigquit "),/* * Block sigquit and save current signal mask. */sigemptyset (&newmask);//Obtain an empty shielded signal set Sigaddset (&newmask, sigquit);//Add sigquitif to the empty shielded signal set (Sigprocmask (Sig_ BLOCK, &newmask, &oldmask) < 0)//Set Perror ("Sig_block error"), Sleep (5);/* Sigquit here'll remaiN Pending *///During sleep Press multiple ctrl+\ pending signal set hold once if (Sigpending (&pendmask) < 0)//Gets the current masked signal set in the process perror ("Sigpending Erro R "), if (Sigismember (&pendmask, Sigquit))//Infer whether Sigquit is in the current masking signal set printf (" \nsigquit pending\n ");/* * Restore signal Mask which unblocks sigquit. */if (Sigprocmask (Sig_setmask, &oldmask, NULL) < 0)//Set the original masked signal set. Because the sigquit retains a pending signal, the signal processing function is triggered here, and sigquit changes back to the default processing perror ("Sig_setmask error");p rintf ("Sigquit unblocked\n"); Sleep (5) ;/* Sigquit here would terminate with core file *////During this time press ctrl+\ to run the signal handler function and invoke the default processing. Game over! printf ("Fillmask test\n"), if (Sigfillset (&fillmask) < 0)//Gets a shielded signal set that masks all signals, except for those two Sigkill sigstopperror (" Sigfillset error\n "), if (Sigismember (&fillmask, Sigquit))//Infer whether Sigquit is in the current masked signal set printf (" \nsigquit pending\n ") ;//In If (Sigprocmask (Sig_setmask,&fillmask, &oldmask) < 0)//Set Mask all perror ("Fillmask error\n"); sleep (5); if ( Sigprocmask (Sig_setmask, &oldmask, NULL) < 0)//If you have pressed ctrl+\ during the Fillmask test. The following will not sleep for 5 seconds. The default handler for Sigquit is running Perror ("Sig_setmask error\n "); sleep (5); exit (0);} static voidsig_quit (int signo) {printf ("Caught sigquit\n"),//if (Signal (sigquit, SIG_DFL) = = Sig_err)//perror ("Can ' t Reset Sigquit ");} </span>

Linux signal Masking

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.