Using sigaction ()

I need to send two signals to the process SIGUSR1 and SIGUSR2 in order to change a specific logical variable in the program ( SIGUSR1 sets true, SIGUSR2 sets false). So I wrote a signalHandler() function to control the behavior of SIGUSR1 or SIGUSR2 . The problem is this: how to set sigaction() to handle this particular task? I spent a lot of time on Google, I read everywhere that sigaction() should be used instead of signal() sigaction() . On the man page, I found this

 int sigaction(int signum, const struct sigaction *act,struct sigaction *oldact); 

in signum I need to set the type of signal that I want to process, then I have the sigaction parameter of the structure:

  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 do not know how to set other fields.

Finally, what is the use of: struct sigaction *oldact ?

+6
source share
1 answer

See the sigaction (2) page. All of this is described here.

Basically, you set either sa_handler or sa_sigaction depending on whether you want more information about the signal.

If you install a later version, you need to add SA_SIGINFO to the flags. Otherwise, the flags should be 0 for your case. You probably want the system calls to fail with errno EINTR when the signal is interrupted (default behavior), so you can consider the new variable value before restarting, but if you decide to automatically restart them ( select and poll never restart), you can set the flag SA_RESTART .

sa_mask is a set of signals that should be delayed during the execution of this handler. You must set at least two signals, so they do not mix if they go fast.

Lastly, sa_restorer deprecated and not used anyway.

+7
source

Source: https://habr.com/ru/post/904142/


All Articles