Signals for programming Linux systems (IV)

Source: Internet
Author: User
Tags sigint signal

Today continue to explore the signal-related east, not much to say, is into the subject:

The signal is represented in the kernel:The following diagram is used to further describe the state of the signal from generation to delivery ( signal blocking and non-tactic): How did that come to be decided? The following is an example of decomposition: So, through these graphs, can describe the signal from the generation to the recursion of a process, the above may be a bit difficult to understand, the following code to further elaborated, before the experiment, but also to understand the use of some functions, these functions in the experiment will be used, that is, signal set operation function. Signal set operation function:One explanation of sigset_t, Baidu Encyclopedia is interpreted as: and the meaning of this function is to the 64-bit clear 0 of the meaning of this function is to the 64 bits of the shielding word to 1 will this signal corresponds to the position of 1 to the position of the signal is 0 to detect this signal the corresponding bit is currently 0 or 1 These are the five related functions of the operating signal set, but note that: These five functions simply change the signal set variables, such as set, and do not really change the shielding word in the process signal, so the next function is to change the shielding word in the signal. Sigprocmask:Well, theory said a lot, the following formally began to experiment, to experience a signal from the generation to the recursion of a state transition process: first, we print out the system of the non-tactic signal, the purpose is to observe the state after:
#include <unistd.h>#include<sys/stat.h>#include<sys/wait.h>#include<sys/types.h>#include<fcntl.h>#include<stdlib.h>#include<stdio.h>#include<errno.h>#include<string.h>#include<signal.h>#defineErr_exit (M) Do{perror (M);     Exit (Exit_failure); }  while(0)voidHandlerintsig);void Printsigset (sigset_t *set)//prints out the state of the signal set, where the parameter set is the signal set of the undefined state {int i;            for (I=1; i<nsig; ++i)//nsig represents the maximum value of the signal, which is equal to the number of {if (Sigismember (set, i))//description is a signal of the non-state        Putchar (' 1 '); else Putchar (' 0 ');    //Description is not a signal of the non-tactic status } printf ("\ n");}intMainintargcChar*argv[])    {sigset_t PSet;  for (;;) {sigpending (&PSet); //This function is a set of signals that gets the pset state of the process, stored in the Printsigset (&PSet); //Print the status of the signal set to see if there is no state signal to produce sleep (1); }    return 0;}

"description": Sigpending is used to obtain all the non-tactic signal sets in the process:

Now look at the effect of the operation:

Can be found that the current state has no non-tactic signal, because has not been blocked signal too, the signal has not been produced, so there is no way to have the status of the.
At this point, we will install a SIGINT signal:

#include <unistd.h>#include<sys/stat.h>#include<sys/wait.h>#include<sys/types.h>#include<fcntl.h>#include<stdlib.h>#include<stdio.h>#include<errno.h>#include<string.h>#include<signal.h>#defineErr_exit (M) Do{perror (M);     Exit (Exit_failure); }  while(0)voidHandlerintsig);voidPrintsigset (sigset_t *Set){    inti;  for(i=1; i<nsig; ++i) {if(Sigismember (Set, i)) Putchar ('1'); ElsePutchar ('0'); } printf ("\ n");}intMainintargcChar*argv[])    {sigset_t PSet; if (signal (SIGINT, handler) = = Sig_err)//install a SIGINT signal err_exit ("Signal error");  for (;;) {sigpending (&PSet); Printsigset (&PSet); Sleep (1); }    return 0;}void Handler (int sig) {printf ("recv a sig=%d\n", SIG);}

Then look at the effect:

From the results, the signal is directly handed up, so this time did not see 1 of the signal, because the signal must be blocked to appear in the non-state, so the next SIGINT signal using the function described above to block it out:

#include <unistd.h>#include<sys/stat.h>#include<sys/wait.h>#include<sys/types.h>#include<fcntl.h>#include<stdlib.h>#include<stdio.h>#include<errno.h>#include<string.h>#include<signal.h>#defineErr_exit (M) Do{perror (M);     Exit (Exit_failure); }  while(0)voidHandlerintsig);voidPrintsigset (sigset_t *Set){    inti;  for(i=1; i<nsig; ++i) {if(Sigismember (Set, i)) Putchar ('1'); ElsePutchar ('0'); } printf ("\ n");}intMainintargcChar*argv[])        {sigset_t PSet;    sigset_t bset; Sigemptyset ( & bset); //Signal set clear 0 Sigaddset (     & bset, SIGINT); //Place the SIGINT in the corresponding position 1 if(Signal (SIGINT, handler) = =Sig_err) Err_exit ("Signal Error"); Sigprocmask (Sig_block, &bset, NULL); //Change the signal mask word in the process, where the third parameter is passed NULL because it does not care about its original signal shielding word  for (;;) {sigpending (&PSet); Printsigset (&PSet); Sleep (1); }    return 0;}voidHandlerintSIG) {printf ("recv a sig=%d\n", SIG);}

Compile run:

From the results, the SIGINT signal, as added to the signal screen Word is 1, so it will be blocked off, and you can see the SIGINT corresponding bit is also printed as 1.

"description": The corresponding bit of SIGINT refers to:

Below, we do one thing, that is, when we press ctrl+\ unblocked, so that the signal in the non-state will be passed, then the corresponding non-state bit will be reduced to 0, the specific code is as follows:

#include <unistd.h>#include<sys/stat.h>#include<sys/wait.h>#include<sys/types.h>#include<fcntl.h>#include<stdlib.h>#include<stdio.h>#include<errno.h>#include<string.h>#include<signal.h>#defineErr_exit (M) Do{perror (M);     Exit (Exit_failure); }  while(0)voidHandlerintsig);voidPrintsigset (sigset_t *Set){    inti;  for(i=1; i<nsig; ++i) {if(Sigismember (Set, i)) Putchar ('1'); ElsePutchar ('0'); } printf ("\ n");}intMainintargcChar*argv[])    {sigset_t PSet;    sigset_t bset; Sigemptyset (&bset); Sigaddset (&bset, SIGINT); if(Signal (SIGINT, handler) = =Sig_err) Err_exit ("Signal Error"); if (signal (sigquit, handler) = = Sig_err)//Register a CTRL + C signal err_exit ("Signal Error"); Sigprocmask (Sig_block,&bset, NULL);  for (;;) {sigpending (&PSet); Printsigset (&PSet); Sleep (1); }    return 0;}void Handler (int sig) {if (sig = = SIGINT) printf ("Recv a sig=%d\n", SIG); else if (sig = = Sigquit) {sigset_t uset;         When ctrl+\ is pressed, the SIGINT signal is unblocked sigemptyset (&uset);        Sigaddset (&uset, SIGINT);    Sigprocmask (Sig_unblock, &uset, NULL); }}

Compile run:

As you can see, when we press ctrl+\, we do not exit, but unblock, so the corresponding SIGINT bit also becomes 0.

In addition, look at this situation:

Press CTRL + C multiple times, you can press ctrl+\ to unblock, only respond to a signal processing function, which is also because SIGINT is unreliable signal, does not support queuing.

In addition, since we have captured the ctrl+\ signal, so there is no way to quit the process, then how to do it, you can use the shell command to force it to kill the following:

Well, what you learn today may be a little jerky, need to digest well, see you next day!

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.