获取触发某些信号的进程的 pid
是否有可能找出引起某些信号的进程的进程ID。在我的场景中,我有一个进程的多个子进程正在运行,我想知道其中哪个子进程发送了信号。
Is it possible to find out the process id of the process which has caused some signal. In my scenario, I have multiple children of a process running, and I want to know which one of them sent the signal.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一种非阻塞方法,用于获取在 Unix 中触发了 SIGCHLD 信号的子 PID:
It's a non-blocking method to get a child PID which has triggered SIGCHLD signal in Unix:
我相信这是不可能的 - 操作系统只是不将此信息传递给目标进程。
I believe it is not possible - the OS just does not pass this information to the target process.
使用 python 3(终于!)非常简单。
以下是使用 python 3.3.3 进行测试的:
It is (finally!) very simple with python 3.
The following is tested with python 3.3.3:
POSIX Linux 确实提供此信息,请检查 man sigaction(2): http: //linux.die.net/man/2/sigaction
在 C 语言中,我设法让它轻松运行:
与我的 3.1.6 vanilla 内核和 gcc 4.4.5 配合得很好——但我在 python 中找不到任何支持。
所以我开始尝试自己构建一些东西(但因为我以前从未做过 C/Python 交互,所以它可能以某种方式扭曲了......)
我或多或少地接近 http://docs.python.org/extending/extending.html 并根据 http://docs.python.org/extending/building.html#building
sigpidmodule.c
setup.py 用于构建模块:
因此
,仍然缺少使用该模块的 python 脚本:test.py
一切都工作得很好...直到:
或者我必须实际调用它才能获得正确的库:
输出:
我不知道为什么会出现这个段错误,而且我已经没有办法修复它了。我想这一定与c和python如何交互有关(我可以想到一些原因,但这只是猜测)。也许在 c-python-interaction 方面有更多经验的人可以在这里提供帮助(或者至少解释一下,问题到底是什么)。我们可能有办法解决那里的问题,至少在 Linux 上。
POSIX Linux does provide this information, check man sigaction(2): http://linux.die.net/man/2/sigaction
In C, I managed to get it running easily:
Works pretty well with my 3.1.6 vanilla kernel and gcc 4.4.5 -- but I could not find any support for it in python.
So i started to try and build something on my own (but since I never did C/Python-Interaction before, it's probably somehow twisted up...)
I'm more or less keeping close to the example at http://docs.python.org/extending/extending.html and building the module according to http://docs.python.org/extending/building.html#building
sigpidmodule.c
setup.py for building the module:
building the module with
So, what's still missing is the python script using the module: test.py
Everything works very well... up to:
or as I have to actually call it to get the lib right:
output:
I don't know why I get this segfault, and I'm running out of ideas to fix it. I guess it must have something to do with how c and python interact (I can think of some reasons, but it's all only guessing). Maybe someone with more experience in c-python-interaction can help here (or at least explain, what's the problem exactly). We might have a way to solve the problem there, at least on linux.