gpt4 book ai didi

linux - sigaddset 与 pthread_sigmask

转载 作者:太空宇宙 更新时间:2023-11-04 03:39:52 25 4
gpt4 key购买 nike

sigaddset()pthread_sigadd() 函数有什么区别?在下面的代码中,如果 pthread_sigmask() 函数调用未注释,则信号将会到来。

    sigemptyset(&signal_set);
if( sigaddset(&signal_set,SIGUSR1) == -1){
printf"sigaddset() retuned error number %d\n",errno);
}
//pthread_sigmask ( SIG_BLOCK, &signal_set, NULL );
while(1){
if(sigwait(&signal_set, &sig_number) == 0){
printf("Got signal\n");
}
}

最佳答案

sigaddset()pthread_sigadd() 一起使用,但执行不同的操作。

  • sigaddset():

http://man7.org/linux/man-pages/man3/sigsetops.3.html :

These functions allow the manipulation of POSIX signal sets.
sigaddset() and sigdelset() add and delete respectively signal from set.

  • pthread_sigadd():

设置信号后,可以通过 sigprocmask()/pthread_sigmask() 屏蔽它。

http://man7.org/linux/man-pages/man2/sigprocmask.2.html ,
http://man7.org/linux/man-pages/man3/pthread_sigmask.3.html :

sigprocmask() [as well as pthread_sigmask()] is used to fetch and/or change the signal mask of the calling thread. The signal mask is the set of signals whose delivery is currently blocked for the caller (see also signal(7) for more details).

关于您发布的代码,收到信号是其预期的工作方式。

http://pubs.opengroup.org/onlinepubs/009695399/functions/sigwait.html :

The signals defined by set shall have been blocked at the time of the call to sigwait(); otherwise, the behavior is undefined.

因此,您必须始终先阻止信号,然后等待它们。

关于linux - sigaddset 与 pthread_sigmask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30193762/

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