问题
I have to send two signals to a process, SIGUSR1
and SIGUSR2
, in order to modify a particular boolean variable in the program (SIGUSR1
sets it to true, SIGUSR2
sets it to false). So I wrote a signalHandler()
function in order to control the behavior of SIGUSR1
or SIGUSR2
. The problem is: how to set sigaction()
to handle this particular task? I spent a lot of time on Google, I read everywhere that I should use sigaction()
instead of the obsolete signal()
. In the man page i found this
int sigaction(int signum, const struct sigaction *act,struct sigaction *oldact);
in signum I have to put the type of signal I want to handle, then I have a struct sigaction parameter:
struct sigaction {
void (*sa_handler)(int);
void (*sa_sigaction)(int, siginfo_t *, void *);
sigset_t sa_mask;
int sa_flags;
void (*sa_restorer)(void);
};
in the first field i thought i should set the name of my signal handler, but I don't know how can I set the other fields.
Finally, what is the use of: struct sigaction *oldact
?
回答1:
See the sigaction(2) manual page. It's all described there.
Basically you set either sa_handler
or sa_sigaction
depending on whether you want the extra signal info.
If you set the later, you need to add SA_SIGINFO
to the flags. Otherwise the flags should probably be 0 for your case. You probably want system calls to fail with errno EINTR
when interrupted with the signal (default behaviour), so you can consider the new value of the variable before restarting them, but if you ended up wanting to restart them automatically (select
and poll
are never restarted), you can set the SA_RESTART
flag.
The sa_mask
is set of signals that should be defered while this singal handler is running. You should set at least the two signals, so they don't get mixed up if they come in quick succession.
And the last, sa_restorer
is obsolete and unused anyway.
来源:https://stackoverflow.com/questions/8573842/use-of-sigaction