关于sigwait()的模棱两可的描述

||   如果没有信号输入          设置在调用时是挂起的,线程应该是   暂停,直到一个或多个成为   待定。集合定义的信号   本应在          调用sigwait()的时间;否则,行为是不确定的。   sigwait()对信号的影响   设置信号的动作是   未指定。 这真是模棱两可,这里的
pending
block
有什么区别? 关于如何在
sigwait
sigaction
之间进行选择的结论还不清楚:   综上所述,何时需要   代码运行以响应   异步信号通知   线程,sigwait()应该用于   处理信号。 Alterna-          如果实现提供信号量,它们也可以是   用于sigwait()或   从信号处理例程中   先前注册          与sigaction()。 有人可以使ѭ2的理由更合理吗?     
已邀请:
        每个进程都有与之关联的所谓信号屏蔽,它定义了被阻止的信号集。可以查询信号掩码或使用with5ѭ(对于单线程代码)和
pthread_sigmask(3)
(对于多线程代码)进行设置。 每当发出信号时(显式地通过
kill(2)
raise(3)
或通过某种其他机制,例如分段错误提升
SIGSEGV
),都将根据当前信号掩码检查信号。如果未阻塞信号,则立即采取行动:如果设置了相应的信号处理程序,则将运行默认操作(通常以异常状态退出或忽略它)。如果信号被信号掩码阻止,则信号状态将设置为“未决”,程序将继续执行。 因此,请考虑以下示例程序:
#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
    
        在
signal(7)
手册页中:
Signal Mask and Pending Signals
    A  signal  may  be  blocked,  which means that it will not be delivered
    until it is later unblocked.  Between the time when it is generated and
    when it is delivered a signal is said to be pending.
\“ Pending \”和\“ blocked \”并不互斥。 同样在
signal(7)
手册页中:
Synchronously Accepting a Signal
    Rather than asynchronously catching a signal via a signal  handler,  it
    is  possible to synchronously accept the signal, that is, to block exe-
    cution until the signal is delivered, at which point the kernel returns
    information about the signal to the caller.  There are two general ways
    to do this:

    * sigwaitinfo(2), sigtimedwait(2),  and  sigwait(3)  suspend  execution
      until  one  of  the signals in a specified set is delivered.  Each of
      these calls returns information about the delivered signal.
因此,
sigaction()
用于允许其他代码运行,直到信号挂起为止,而
sigwait()
挂起线程的执行,直到信号挂起但被阻塞为止。     

要回复问题请先登录注册