C中的信号队列 [英] Signal queuing in C

查看:36
本文介绍了C中的信号队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Linux 下有一个简单的程序,它在一个循环中向其子进程发送 SIGUSR1 信号.但是当我发送例如有时会发生 10 个信号,表明孩子只收到了其中的 3 个.最后发送的信号始终是 SIGUSR2,并且每次都会收到.

I have a simple program under Linux which sends SIGUSR1 signal to its child process in a cycle. But when I send e.g. 10 signals, sometimes happens, that the child received only 3 of them. Last sent signal is always SIGUSR2 and that is received every time.

信号是否在排队,或者当进程没有处理前一个信号时,它只是被覆盖了?有没有办法可以在队列中发送信号?

Are the signals queuing, or when process didn't process the previous, it is simply overwritten? Is there a way I can send signals in a queue?

推荐答案

会发生以下情况:

  1. 接收到的第一个信号,即 SIGUSR1,处理程序被调用并正在运行
  2. 收到第二个信号,因为来自 nr1 的处理程序仍在运行,所以信号 nr2 处于挂起状态并被阻塞.
  3. 收到第三个信号,因为来自 nr1 的处理程序仍在运行,所以信号 3 被丢弃.
  4. 丢弃与信号 nr1 相同类型的第四、第五...等信号.

一旦信号处理程序处理完信号 nr1,它将处理信号 nr2,然后信号处理程序将处理 SIGUSR2.

Once signal handler is done with signal nr1, it will process signal nr2, and then signal handler will process the SIGUSR2.

基本上,相同类型的待处理信号不会排队,而是被丢弃.不,没有简单的方法可以以这种方式突发"发送信号.人们总是假设可能有几个信号被丢弃,并试图让处理程序完成清理工作并找出要做什么(例如收割孩子,如果所有孩子同时死亡).

Basically, pending signals of the same type are not queued, but discarded. And no, there is no easy way to "burst" send signals that way. One always assumes that there can be several signals that are discarded, and tries to let the handler do the work of cleaning and finding out what to do (such as reaping children, if all children die at the same time).

这篇关于C中的信号队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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