int sigaction(intconststructstruct sigaction *oldact);
The function of the Sigaction function is to inspect or modify the processing action associated with the specified signal (both operations can be done simultaneously).
Parameter description:
Signum: Signal to operate
Act: A new way to set the signal to be processed
Oldact: The original Signal processing method
Return value: Successfully returned 0, failed return-1, and set errno
struct Sigaction structure prototype:
struct sigaction { void (*sa_handler)(int); //老类型的信号处理函数指针,与 single 函数一样 void (*sa_sigaction)(intvoid//新类型的信号处理函数指针 sigset_t sa_mask; //将要被阻塞的信号集合 int sa_flags; //信号处理方式 void (*sa_restorer)(void//保留};
single 函数一样
The three parameters of the Sa_sigaction function pointer mean:intIsignnum: Incoming signal siginfo_t *psigninfo: Some information related to this signal, he is a struct prototype as follows siginfo_t {intSi_signo;/ * signal value, meaning for all signals * / intSi_errno;/ * errno value, meaning for all signals * / intSi_code;/ * The cause of the signal is meaningful for all signals * / intSi_trapno;/* Trap number that caused hardware-generated signal (unused O N most architectures) * /pid_t Si_pid;/ * Process ID to send the signal * /uid_t Si_uid;/ * Real user ID for sending signal process * / intSi_status;/ * to out state, meaning to SIGCHLD * /clock_t Si_utime;/ * User-consumed time, meaning for SIGCHLD * /clock_t Si_stime;/ * Time consumed by kernel, meaning for SIGCHLD * /sigval_t Si_value;/* Signal value, for all real-time meaningful, is a federated data structure that can be an integer (indicated by Si_int, or can be a pointer, by SI_PTR) * / intSi_int;/ * POSIX.1B signal * / void*si_ptr;/ * POSIX.1B signal * / intSi_overrun;/ * Timer overrun count; POSIX.1B Timers * / intSi_timerid;/ * Timer ID; POSIX.1B Timers * / void*SI_ADDR;/ * Trigger fault memory address, meaning for sigill,sigfpe,sigsegv,sigbus signal * / LongSi_band;/ * make sense for sigpoll signal * / intSI_FD;/ * make sense for sigpoll signal * / ShortSI_ADDR_LSB;/* Least significant bit of address (since kernel 2.6.32) */}void*preserved: All the information on the Internet is said to be (reserved, useless)
sa_mask : 是一个包含信号集合的结构体,该结构体内的信号表示在进行信号处理时,将要被阻塞的信号。
sa_flags 是一组掩码的合成值,指示信号处理时所应该采取的一些行为,各掩码的含义为:SA_RESETHAND : 处理完毕要捕捉的信号后,将自动撤消信号处理函数的注册,即必须再重新注册信号处理函数,才能继续处理接下来产生的信号。SA_NODEFER : 在处理信号时,如果又发生了其它的信号,则立即进入其它信号的处理,等其它信号处理完毕后,再继续处理当前的信号,即递规地处理。如果sa_flags包含了该掩码,则结构体sigaction的sa_mask将无效;SA_RESTART : 如果在发生信号时,程序正阻塞在某个系统调用,例如调用read()函数,则在处理完毕信号后,接着从阻塞的系统返回。该掩码符合普通的程序处理流程,所以一般来说,应该设置该掩码,否则信号处理完后,阻塞的系统调用将会返回失败;SA_SIGINFO : 指示结构体的信号处理函数指针是哪个有效,如果sa_flags包含该掩码,则sa_sigactiion指针有效
Example:
#include <signal.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/types.h>#include <unistd.h>voidMy_sigaction (intSigNum, siginfo_t *siginfo,void*BUF) {Switch(SigNum) { CaseSIGUSR1:printf("recv form pid is%d, signal are SIGUSR1, Signum is%d\n", Siginfo->si_pid, Siginfo->si_signo); Break; CaseSIGUSR2:printf("recv form pid is%d, signal are SIGUSR2, Signum is%d\n", Siginfo->si_pid, Siginfo->si_signo); Break;default:printf("recv form pid is%d, Signum is%d\n", Siginfo->si_pid, Siginfo->si_signo); } Sleep (Ten);}intMainintargcChar*argv[]) {structSigaction m_act; M_act.sa_sigaction = my_sigaction; M_act.sa_flags = Sa_restart | Sa_siginfo; Sigemptyset (&m_act.sa_mask); Sigaddset (&m_act.sa_mask, SIGUSR1); Sigaddset (&m_act.sa_mask, SIGUSR2); Sigaction (SIGUSR1, &m_act, NULL); Sigaction (SIGUSR2, &m_act, NULL); Sigaction (SIGINT, &m_act, NULL);printf("My pid is%d\n", Getpid ()); while(1);return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Linux sigaction function (registration signal) use