Use of sigaction()

别说谁变了你拦得住时间么 提交于 2019-12-10 13:06:52

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!