A signal-related structure in Linux struct sigaction is used primarily in sigaction signal installation and sigqueue signal delivery
The structure is located in/usr/include/bits/sigaction.h
In which the description of the structure can be found
/* Structure describing the action to being 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 is blocked. */
__sigset_t Sa_mask;
* Special flags. */
int sa_flags;
/* Restore handler. */
void (*sa_restorer) (void);
};
I checked some books on the internet, this structure is probably like this.
Among them, Sa_restorer, which is obsolete, POSIX does not support it and should not be used again.
1, the joint data structure of the two elements _sa_handler and *_sa_sigaction specified signal correlation function, that is, the user-specified signal processing functions. In addition to being a user-defined processing function, you can also be a SIG_DFL (using the default processing) or sig_ign (ignoring the signal).
2, the processing function specified by _sa_handler has only one parameter, namely the signal value, so the signal can not pass any information other than the signal value, and the signal processing function specified by _sa_sigaction is provided with three parameters, which is set for real-time signal (of course, it also supports non-real time signal), It specifies a 3-parameter signal-processing function. The first parameter is a signal value, and the third parameter is not used (POSIX does not use the standard for this parameter), the second parameter is a pointer to the siginfo_t structure that contains the data values that the signal carries, and the parameters point to the following structure:
This is defined in the/usr/include/bits/siginfo.h.
Is it too complicated to have a compact version on the internet I moved in here to use
siginfo_t {
int Si_signo; /* signal value, meaning for all signals
int Si_errno; /* errno value, meaning for all signals * *
int Si_code; /* The cause of the signal, for all signals meaningful * *
union{/* Joint data structure, different members to adapt to different signals * *
Ensure large enough storage is allocatedSpace
int _pad[si_pad_size];
A meaningful structure for the Sigkill
struct{
...
}...
... ...
... ...
A meaningful structure for Sigill, SIGFPE, SIGSEGV, Sigbus
struct{
...
}...
... ...
}
}
<!--[If!supportlinebreaknewline]-->
<!--[endif]-->
3. SA_MASK specifies which signals should be blocked during the execution of the signal processing program. By default, the current signal itself is blocked, preventing the nesting of the signals from being sent unless a sa_nodefer or Sa_nomask flag bit is specified.
Note: Note that the Sa_mask specified signal blocking condition is blocked by the signal specified by Sa_mask during the execution of the processing function of the sigaction () installation signal.
4, Sa_flags contains a number of signs, including just mentioned Sa_nodefer and sa_nomask sign bit. Another important sign is sa_siginfo, when the flag bit is set, the parameters that come with the signal can be passed to the signal processing function, so the handler function should be specified for the sa_sigaction in the sigaction structure, not the Sa_ handler specifies the signal processing function, otherwise, setting the flag becomes meaningless. Even if the signal processing function is specified for sa_sigaction, if the sa_siginfo is not set, the signal processing function cannot get the data transmitted by the signal, and access to the information in the signal processing function will result in a segment error (segmentation fault).
Note: In many literatures, it is considered that if the sign bit is set, the three-parameter signal processing function must be defined. Actually, the verification method is very simple: realize a single parameter signal processing function, and set the flag bit in the program, you can see the running result of the program. In fact, you can think of the sign as a switch that signals whether the parameter is passed, and if you set that bit, pass the parameter, otherwise, do not pass the parameter.
Structure said about the same, in fact, when I saw these things to get dizzy several times, simply say a few words is
If you want to trigger some action when a signal is generated, we can install the signal.
Signal used to install unreliable signals Linux is now implemented with Sigaction
Sigaction is used to install reliable signals of course he can also install unreliable signals and can be accompanied by more information
Signal install signal only need to pass 2 parameters one is the value of a signal is a function that triggers when a signal occurs, and the function accepts an integer
Sigaction installed with 3 parameters The first parameter is the value of the signal, and the second is the sigaction structure, which describes the function that is invoked when the signal occurs and some other information, the main member is Sa_ Handler the specified trigger function takes only one parameter, the value of the signal, which is no different from the signal call, sa_sigaction the specified trigger function with 3 arguments the first parameter is the value of the signal, and the second parameter is the structure of the packet-attached information siginfo, The third parameter is null if you want to pass additional information to the triggering function, you must sigaction the sa_flag of the structure to the second parameter passed to Sigaction Sa_siginfo
It says so many structures and complex siginfo, in fact siginfo we do not need to initialize him or do anything in practical applications , but we can extract some information from this structure when the signal is triggered.
sigaction function prototype: int sigaction (int signo,const struct sigaction *restrict-act,struct sigaction *restrict);
The first parameter is the signal number, the second is the sigaction structure, and the third is generally null.
Example:
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
static void sig_usr (int signum)
{
if (Signum = = SIGUSR1)
{
printf ("SIGUSR1 received\n");
}
else if (Signum = = SIGUSR2)
{
printf ("SIGUSR2 received\n");
}
Else
{
printf ("Signal%d received\n", Signum);
}
}
int main (void)
{
Char buf[512];
int n;
struct Sigaction sa_usr;
sa_usr.sa_flags = 0;
Sa_usr.sa_handler = SIG_USR; Signal Processing function
Sigaction (SIGUSR1, &SA_USR, NULL);
Sigaction (SIGUSR2, &SA_USR, NULL);
printf ("My PID is%d\n", getpid ());
while (1)
{
if ((n = Read (Stdin_fileno, buf, 511)) = = 1)
{
if (errno = = eintr)
{
printf ("Read is interrupted by signal\n");
}
}
Else
{
Buf[n] = ' the ';
printf ("%d bytes read:%s\n", N, buf);
}
}
return 0;
}