Signal mounting signal
typedef void (*__sighandler_t) (int);
__sighandler_t Signal (int __sig, __sighandler_t __handler) : The signal handler is installed, the 1th parameter is the received signal, the 2nd parameter is the function pointer that is processed, and the return value is also a function pointer.
Three related macros
- #define Sig_err ((__sighandler_t)-1)//Return error (syntactically 1 cast for __sighandler_t type, function pointer type)
- #define SIG_DFL ((__sighandler_t) 0)//Perform signal default action
- #define sig_ign ((__sighandler_t)-1)//Ignore signal
If set more than once, the last set operation is finally in effect. Failed to return Sig_err
#include <stdio.h>#include<unistd.h>#include<signal.h>voidSIG_USR (intsig);intMain () {inti =0; if(Signal (SIGUSR1, sig_usr) = =sig_err) printf ("Cannont catch sigusr1\n"); if(Signal (SIGUSR2, sig_usr) = =sig_err) printf ("Cannont catch sigusr2\n"); while(1) {printf ("%2d\n", i); Pause (); //waiting for a signal to comei++; } return 0;}voidSIG_USR (intSIG) { if(sig = =SIGUSR1) printf ("Received sigusr1\n"); Else if(sig = =SIGUSR2) printf ("Received sigusr2\n"); Elseprintf ("Undeclared signal%d\n", SIG);}
With two terminals, one terminal executes the code, and the other terminal sends a signal.
The signal sent is:
Processing results are:
Knowledge Point: Execute code followed by & to show process number
Sigaction Mounting Signal
int sigaction (int __sig, struct sigaction * __act, struct sigaction *__oact)
Where the sigaction structure is defined as:
structsigaction{union{__sighandler_t _sa_handler; //SIG_DFL, Sig_ign signal, similar to signal function void(*_sa_sigaction) (int,structSiginfo *,void*);//signal capture function, can get additional information}__u; sigset_t Sa_mask; //other set of signals to be masked during signal capture function executionUnsignedLongSa_flags;//special signs affecting the behavior of signals void(*sa_restorer) (void);//not used};#defineSa_handler _u._sa_handler//Redefine two members#defineSa_sigaction _u._sa_sigaction
The Sigkill and SIGSTOP signals cannot be shielded from the shielded signal set sa_mask.
If the member Sa_flags is set to sa_siginfo, the signal handler function is set through sa_sigaction . Its function pointer type is:
void (*_sa_sigaction) (intstructvoid// signal capture function, can get additional information
First parameter: corresponding signal
Third argument: A pointer to an object of type ucontext_t to refer to the context of the receiving process or thread that was interrupted when the signal was passed.
The second parameter, struct Siginfo, describes some of the information that is interrupted by the signal. The specific structure is defined as follows:
The Si_sigo member contains the system-generated signal encoding.
Si_errno members may contain additional error information related to the implementation. If it is not 0, the member will contain an error number that represents the condition that caused the signal to be generated
The Si_code member contains a code that identifies the cause of the signal generation
The subsequent union of different signals will be filled with different parts.
Example:
#include <stdio.h>#include<unistd.h>#include<signal.h>#include<stdlib.h>voidFuncintSigno, siginfo_t *info,void*p) {printf ("Signo =%d\n", Signo); printf ("Sender pid =%d\n", info->si_pid);//Print sender's PID}intMain () {structSigaction Act, oact; Sigemptyset (&act.sa_mask);//set mask to nullAct.sa_flags = Sa_siginfo;//need to modify Sa_flagsAct.sa_sigaction = func;//processing FunctionsSigaction (SIGUSR1, &act, &oact);//Installation Signal while(1) {printf ("pid is%d Hello world.\n", Getpid ()); Pause (); //waiting for a signal }}
The terminal that sends the signal
The terminal that executes the code
There is a big paragraph about sa_flags in the back, not looking carefully.
Compare Sigaction's Sa_handler and signal
First block the signal, send yourself a signal, unblock
Signal
#include <stdlib.h>#include<signal.h>Static voidSIG_USR1 (Signo) {printf ("SIGUSR1 function\n");}Static voidSIG_USR2 (Signo) {printf ("SIGUSR2 function\n");}Static voidSig_alarm (Signo) {printf ("SIGALRM function\n");}intMainvoid) {sigset_t newmask, oldmask; //installing signal processing functions if(Signal (SIGUSR1, SIG_USR1) <0|| Signal (SIGUSR2, SIG_USR2) | |signal (SIGALRM, Sig_alarm)) perror ("signal\n"); Sigemptyset (&newmask);//Mask EmptySigaddset (&Newmask, SIGUSR1); Sigaddset (&Newmask, SIGUSR2); Sigaddset (&Newmask, SIGALRM); Sigprocmask (Sig_block,&newmask, &oldmask); printf ("Sigusr is blocked\n"); Kill (Getpid (), SIGUSR2); Kill (Getpid (), SIGUSR1); Kill (Getpid (), SIGALRM); Sigprocmask (Sig_setmask,&Oldmask, NULL);}
Strange, the book said Signal has a bug, the signal will be executed many times, but I am not a problem here ah?
Sigaction's
#include <stdlib.h>#include<signal.h>Static voidSIG_USR1 (Signo) {printf ("SIGUSR1 function\n");}Static voidSIG_USR2 (Signo) {printf ("SIGUSR2 function\n");}Static voidSig_alarm (Signo) {printf ("SIGALRM function\n");}intMainvoid){ structsigaction Act1, Act2, Act3; Act1.sa_handler=SIG_USR1; Sigemptyset (&act1.sa_mask); Act2.sa_handler=SIG_USR2; Sigemptyset (&act2.sa_mask); Act3.sa_handler=Sig_alarm; Sigemptyset (&act3.sa_mask); //installing signal processing functionsSigaction (SIGUSR1, &Act1, NULL); Sigaction (SIGUSR2,&Act2, NULL); Sigaction (SIGALRM,&Act3, NULL); sigset_t Newmask, Oldmask; Sigemptyset (&newmask);//Mask EmptySigaddset (&Newmask, SIGUSR1); Sigaddset (&Newmask, SIGUSR2); Sigaddset (&Newmask, SIGALRM); Sigprocmask (Sig_block,&newmask, &oldmask); printf ("Sigusr is blocked\n"); Kill (Getpid (), SIGUSR2); Kill (Getpid (), SIGUSR1); Kill (Getpid (), SIGALRM); Sigprocmask (Sig_setmask,&Oldmask, NULL);}
The same effect
"Linux Advanced Programming" (tenth) Linux asynchronous signal processing mechanism 2