关于调用sigwait模棱两可的说明() [英] About the ambiguous description of sigwait()

查看:143
本文介绍了关于调用sigwait模棱两可的说明()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在没有信号
         集是在调用时之前,该线程应
  挂起,直到一个或多个变
  悬而未决。通过集中定义的信号
  应被挡在
         呼叫的时间sigwait的();否则,其行为是不确定的。
  调用sigwait对信号的影响()
  对于信号的行动集
  不确定的。

If no signal in set is pending at the time of the call, the thread shall be suspended until one or more becomes pending. The signals defined by set shall have been blocked at the time of the call to sigwait(); otherwise, the behavior is undefined. The effect of sigwait() on the signal actions for the signals in set is unspecified.

这是真的很模糊,什么是悬而未决之间的差异在这里?

This is really ambiguous ,what's the difference between pending and block here?

及其之间如何选择结束调用sigwait 的sigaction 目前尚不清楚在所有的:

And its conclusion on how to choose between sigwait and sigaction is not clear at all:

在摘要中,当有必要对
  为响应code运行
  异步信号来通知
  螺纹,调用sigwait()应被用来
  处理信号。 Alterna-
         tively,如果实现提供信号量,它们也可以是
  使用以下两种调用sigwait()或
  从一个信号处理例行程序内
  previously注册
         用的sigaction()。

In summary, when it is necessary for code run in response to an asynchronous signal to notify a thread, sigwait() should be used to handle the signal. Alterna- tively, if the implementation provides semaphores, they also can be used, either following sigwait() or from within a signal handling routine previously registered with sigaction().

有人可以做的原因调用sigwait 更合理?

Can someone make the reason of sigwait more rational?

推荐答案

每一道工序都有什么叫做的信号屏蔽的与之相关联的,它定义了它们的受阻的信号集。信号掩码可以查询或 setprocmask(2) (单线程code)和 pthread_sigmask(3) (多线程code)。

Every process has what's called a signal mask associated with it, which defines the set of signals which are blocked. The signal mask can be queried or set with setprocmask(2) (for single-threaded code) and pthread_sigmask(3) (for multithreaded code).

每当产生一个信号(无论是通过明确 杀(2) 加薪(3) ,或者通过一些其他的机制,例如分割故障筹集 SIGSEGV ),该信号与当前的信号掩码检查。如果信号没有被阻塞,那么它会被立即采取行动:如果设置相应的信号处理程序被调用,否则默认操作(通常与异常状态退出,或者忽略它)运行。如果该信号是由信号掩模阻挡,则该信号的状态被设置为<青霉>未决的,程序继续执行。

Whenever a signal is raised (either explicitly via kill(2) or raise(3), or via some other mechanism such as a segmentation fault raising SIGSEGV), the signal is checked against the current signal mask. If the signal is not blocked, then it is acted upon immediately: the corresponding signal handler is called if set, otherwise the default action (typically exiting with abnormal status or ignoring it) is run. If the signal is blocked by the signal mask, then the state of the signal is set to pending, and the program continues execution.

所以,考虑下面的示例程序:

So consider the following example program:

#include <signal.h>
#include <stdio.h>

void on_sigusr1(int sig)
{
  // Note: Normally, it's not safe to call almost all library functions in a
  // signal handler, since the signal may have been received in a middle of a
  // call to that function.
  printf("SIGUSR1 received!\n");
}

int main(void)
{
  // Set a signal handler for SIGUSR1
  signal(SIGUSR1, &on_sigusr1);

  // At program startup, SIGUSR1 is neither blocked nor pending, so raising it
  // will call the signal handler
  raise(SIGUSR1);

  // Now let's block SIGUSR1
  sigset_t sigset;
  sigemptyset(&sigset);
  sigaddset(&sigset, SIGUSR1);
  sigprocmask(SIG_BLOCK, &sigset, NULL);

  // SIGUSR1 is now blocked, raising it will not call the signal handler
  printf("About to raise SIGUSR1\n");
  raise(SIGUSR1);
  printf("After raising SIGUSR1\n");

  // SIGUSR1 is now blocked and pending -- this call to sigwait will return
  // immediately
  int sig;
  int result = sigwait(&sigset, &sig);
  if(result == 0)
    printf("sigwait got signal: %d\n", sig);

  // SIGUSR1 is now no longer pending (but still blocked).  Raise it again and
  // unblock it
  raise(SIGUSR1);
  printf("About to unblock SIGUSR1\n");
  sigprocmask(SIG_UNBLOCK, &sigset, NULL);
  printf("Unblocked SIGUSR1\n");

  return 0;
}

输出:

SIGUSR1 received!
About to raise SIGUSR1
After raising SIGUSR1
sigwait got signal: 30
About to unblock SIGUSR1
SIGUSR1 received!
Unblocked SIGUSR1

这篇关于关于调用sigwait模棱两可的说明()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆