gpt4 book ai didi

c - sigprocmask() 问题

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

下面的代码来自 Unix 环境中的高级编程,W. Richard Stevens

关于这本密码书说;

"If the signal is sent to the process while it is blocked, the signal delivery will be deferred until the signal is unblocked. To the application, this can look as if the signal occurs between the unblocking and the pause (depending on how the kernel implements signals). If this happens, or if the signal does occur between the unblocking and the pause, we have a problem. Any occurrence of the signal in this window of time is lost in the sense that we might not see the signal again, in which case the pause will block indefinitely. This is another problem with the earlier unreliable signals."

它建议在重置信号掩码之前使用 sigsuspend() 而不是 pause() 因为它会重置信号掩码并使进程在单个原子中休眠手术。但我不希望我的进程等到信号在走出临界区后才出现。 那么这个问题也适用于我的情况吗?如果是这样,在使用 sigprocmask() 重置信号掩码时我应该使用什么来不丢失信号?

 sigset_t     newmask, oldmask;

sigemptyset(&newmask);
sigaddset(&newmask, SIGINT);

/* block SIGINT and save current signal mask */
if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
err_sys("SIG_BLOCK error");

/* critical region of code */

/* reset signal mask, which unblocks SIGINT */
if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
err_sys("SIG_SETMASK error");

/* window is open */
pause(); /* wait for signal to occur */

/* continue processing */

最佳答案

使用 sigsuspend 是为了避免 sigprocmask 和 pause 之间的过程。如果在收到信号之前不需要暂停线程,那么 sigsuspend 无法为您做任何事情。您没有提供足够的信息来了解您的上下文中是否还有其他问题来源。

关于c - sigprocmask() 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5688607/

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