多个信号(signal)同时触发时,信号处理程序只会执行一次?

新手上路,请多包涵

在unp中有这样一段话:
image.png

我理解这里第一段的意思是:如果5个SIGCHLD信号都在信号处理程序执行之前产生,那么信号处理函数只会执行1次。

我不理解的是,信号在内核中应该是会在一个队列中,信号处理函数是怎么做到只执行1次的呢?难道在执行之前遍历了队列,把要处理的信号全部剔除,然后再执行信号处理程序?

阅读 3.1k
1 个回答

因为它不是一个队列。
man signal

Queueing and delivery semantics for standard signals

If multiple standard signals are pending for a process, the order
in which the signals are delivered is unspecified.

Standard signals do not queue. If multiple instances of a standard signal are generated while that signal is blocked, then only one instance of the signal is marked as pending (and the signal will be delivered just once when it is unblocked). In the case where a standard signal is already pending, the siginfo_t structure (see sigaction(2)) associated with that signal is not overwritten on arrival of subsequent instances of the same signal. Thus, the process will receive the information associated with the first instance of the signal.

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题