简体   繁体   English

kill会被信号打断吗?

[英]Will kill be interrupted by a signal?

For example, if I use kill (a function in C library signal.h) to emit a SIGINT signal to a child, will the SIGCHLD signal from the child be caught before the kill function returns?例如,如果我使用 kill (a function in C library signal.h) 向孩子发出 SIGINT 信号,那么来自孩子的 SIGCHLD 信号会在 kill ZC1C425268E68384F14AB50 返回之前被捕获吗?

While the kill syscall isn't interruptible (at least according to the man page ), there's still at least two cases in which the SIGCHLD handler could run before the function returns:虽然kill系统调用不可中断(至少根据手册页),但仍有至少两种情况下SIGCHLD处理程序可以在 function 返回之前运行:

  1. If there's another thread in your process, the kernel could choose to run the signal handler there even before the kill syscall returns in your first thread.如果您的进程中有另一个线程,则 kernel 甚至可以在您的第一个线程中的kill系统调用返回之前选择在那里运行信号处理程序。
  2. You're probably using a wrapper function for kill from your libc.您可能正在使用包装器 function 从您的 libc 中kill The signal handler could run in between the syscall returning to it, and it returning to your code.信号处理程序可以在返回给它的系统调用和它返回给您的代码之间运行。

So if you want to make sure you don't get the SIGCHLD until after kill returns, then you need to use sigprocmask to block it before the kill until you're ready for it.因此,如果您想确保在kill返回之前不会获得SIGCHLD ,那么您需要在kill返回之前使用sigprocmask来阻止它,直到您准备好为止。

I don't know if this answers your question but, reading the Linux man page, the possible return values of kill are EINVAL , EPERM , and ESRCH .我不知道这是否回答了您的问题,但是,阅读 Linux 手册页, kill的可能返回值是EINVALEPERMESRCH EINTR is not one of them. EINTR不是其中之一。 That makes me think that the function won't be interrupted.这让我觉得 function 不会被打断。 At least, the system call won't be interrupted.至少,系统调用不会被中断。

EDIT: I mean errno , not return, values.编辑:我的意思是errno ,而不是返回值。

Signal handling is asynchronous.信号处理是异步的。 There is therefore no guarantee either way about whether the SIGCHLD emitted when the child terminates will be received by the parent before the kill call returns.因此,无论哪种方式都不能保证子进程终止时发出的SIGCHLD是否会在kill调用返回之前被父进程接收。 My guess would be that kill will usually win the race and return before the SIGCHLD arrives, but it is not safe to depend on that.我的猜测是kill通常会SIGCHLD到达之前赢得比赛并返回,但依赖它是不安全的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM