1. Signal Processing function
The related function prototypes are as follows:
#include <signal.h>sighandler_t signal (Int signum, sighandler_t handler); The first parameter is the signal of the second parameter is the signal processor: 1. Can be SIG_DFL, the default action of the signal 2. Can be sig_ign, ignoring the signal 3. A handler function with an integer parameter. #include <signal.h>int sigaction (int signum, const struct sigaction * act, struct sigaction *oldact);signum signal to be processed act pointer to structure that describes the structure of the operation How to respond to the signal of the struct oldact pointer, to the struct that describes the structure of the substituted operation the replaced processing settings struct sigaction { void (*sa_handler) (int); void (*sa_sigaction) (int, siginfo_t *, void *); sigset_t sa_mask; int sa_flags; void (*sa_restorer) (void); };
How the process handles the signal (the early signal processing mechanism):
The process can tell the kernel how to handle the signal through the signal system call.
1. Accept the default processing
Processing signal (SIGINT,SIG_DFL) According to the original intent area of the signal
2. Ignore the signal
Signal (sigint,sig_ign) ignores SIGINT signal
3. Call a function
Signal (Sigint,functionname)
2. Problems with early signal processing mechanisms
One: Signal processing functions are disabled after each call (not different depending on the system)
Two: Do not know the reason why the signal was sent
Third: The processing function cannot safely block other signals
Four: signal interruption is not supported, signal will be blocked. (different systems may vary)
3.POSIX Signal Processing function
POSIX provides sigaction this signal processing function. A sigaction structure is provided accordingly.
The sa_flags in this structure defines a number of flags that deal with the problems of early signaling mechanisms, which can be combined with operations.
Only some of the flag bits are listed below:
Sa_resethand when a processing function is called to reset instead of disabling Sa_nodefer shutdown signal blocking, allowing recursive call signals Sa_restaat when system calls restart for some slow device or similar system calls instead of returning sa_siginfo indicating the use of SA The value of the _sigaction function, if the bit is not set, then the value of the function pointed to by the sa_handle is used, if sa_sigaction is used, then the function will be passed on not just the number of the signal, but the structure of the cause and condition of the signal.
The following example shows how to use sigaction to implement secure blocking of other signals
#include <stdio.h> #include <stdlib.h> #include <signal.h> #define INPUTLEN 100int main ( int argc, char *argv[] ) { struct sigaction newhandler; #定义一个信号集 sigset_t blocked; char x[INPUTLEN]; #设置信号处理函数 newhandler.sa_handler = inthandler; #设置信号处理函数重置 newhandler.sa_flags = SA_RESETHAND|SA_RESTART #清空信号集 sigemptyset (&blocked); # add Sigquit Signal set sigaddset (&block,sigquit); #设置屏蔽的信号集 newhandler.sa_ mask = blocked; #装载信号 if (Sigaction (sigint,&newhandler,null) == -1) perror ("Sigaction"); else while (1) { fgets (X,inputlen,stdin); printf ("input: %s ", x); } return exit_success;} /* ------ ---- end of function main ---------- */void inthandler ( Int s) { printf ("called with signal %d\n", s) ; sleep (s); printf ("done handling signal %d\n", s);} /* ----- end of function inthandler ----- */
This article is from the "Focus on Linux" blog, so be sure to keep this source http://forlinux.blog.51cto.com/8001278/1532969