This section uses the sigaction installation signal. The sigaction function is described as follows:
#include <signal.h>int sigaction(int sig, const struct sigaction *restrict act, struct sigaction *restrict oact);
Parameter description:
1)SIG: Enter a parameter to specify the signal to be installed.
2)Act: Enter a parameter to specify the data structure of the installation signal. This parameter is a structure pointer of the struct sigaction type. Struct is defined in the header file <bits/sigaction. h> and is defined as follows:
/* Structure describing the action to be taken when a signal arrives. */struct sigaction { /* Signal handler. */#ifdef __USE_POSIX199309 union { /* Used if SA_SIGINFO is not set. */ __sighandler_t sa_handler; /* Used if SA_SIGINFO is set. */ void (*sa_sigaction) (int, siginfo_t *, void *); } __sigaction_handler;# define sa_handler __sigaction_handler.sa_handler# define sa_sigaction __sigaction_handler.sa_sigaction#else __sighandler_t sa_handler;#endif /* Additional set of signals to be blocked. */ __sigset_t sa_mask; /* Special flags. */ int sa_flags; /* Restore handler. */ void (*sa_restorer) (void); };
2) This structure is the core of sigaction. The structure members are described as follows:
_ Sigaction_handler: Used to define the signal processing function, which is actually a pointer to the signal processing function. It can be seen that this member is a union type. One is to call the same processing function as signal, and its type is _ sighandler_t. This type of function has only one input parameter, that is, the signal value; the other is the function supported by the sigaction system call. The declaration of this function is: void (* sa_sigaction) (INT, siginfo_t *, void *).
Sa_mask: Signal mask. During the execution of the signal processing function, the signal defined in the mask will be blocked.
Sa_flags: Flag. Sigaction supports several flags, including the sa_siginfo and sa_restart parameters. The sa_siginfo mark indicates that other data can be attached to the signal when it is sent; The sa_restart parameter indicates that if the signal interrupts a system call to the city, the system automatically restarts the system call. If this parameter is not specified, the system call that is interrupted will return failure with the error code eintr.
_ Oact: Output parameter, which is a pointer to the struct sigaction structure. After sigaction is successfully called, The system returns the original signal processing method. If you do not need to obtain the original signal processing method, this parameter can be null.
3) return value description:
0: The call is successful;
-1: The call failed. You can view the error message returned by errno.
Sample Code:
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include <signal.h>void HandleSigint(int signo, siginfo_t *info, void *none){ printf("receive signal %d, addtional data is %d\n", signo, info->si_value.sival_int);}int main(void){ struct sigaction act, oact; memset(&act, 0x00, sizeof(struct sigaction)); sigemptyset(&act.sa_mask); act.sa_sigaction = HandleSigint; act.sa_flags = SA_SIGINFO; if (sigaction(SIGINT, &act, &oact) == -1) { perror("sigaction"); exit(0); } pause(); return 0;}
Running structure:
[Root @ localhost linuxc] #./mysigaction <-enter <Ctrl> + <C> receive signal 2, addtional data is 0