gpt4 book ai didi

linux - kill(getpid(), sig)之后,信号是否保证在下一条语句之前送达?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:38:43 24 4
gpt4 key购买 nike

假设您处于单线程进程中,并且相关信号未被阻塞或忽略,是否可以保证:

kill(getpid(), sig);

是否会导致在执行下一行代码之前传递信号?

特别是,如果信号是一个没有处理程序的信号,并且整个默认操作是终止进程(例如 SIGTERM、SIGALRM),是否保证不会执行下一行代码?

我一直认为(至少在 Linux 上)答案是"is",因为我认为在从任何系统调用返回之前,内核总是会检查是否有任何挂起的信号,如果有则传递它们。但我认为我已经观察到(当在重负载的多核系统上运行时)情况并非总是如此,尽管这很难重现,所以我很感激确认我没有看到东西。

[这个问题与Is a signal sent with kill to a parent thread guaranteed to be processed before the next statement?非常相似除了那个问题是关于多线程进程的(答案肯定是“否”),而这个问题是关于单线程进程的。]

最佳答案

是的。如果您在单线程进程中,POSIX gives guarantees you this .

If the value of pid causes sig to be generated for the sending process, and if sig is not blocked for the calling thread and if no other thread has sig unblocked or is waiting in a sigwait() function for sig, either sig or at least one pending unblocked signal shall be delivered to the sending thread before kill() returns.

关于linux - kill(getpid(), sig)之后,信号是否保证在下一条语句之前送达?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11248552/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com