且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何阻止在线程中的所有信号,而无需使用sigwait的?

更新时间:2023-02-27 09:10:59

通过 S = pthread_sigmask(SIG_BLOCK,&安培;集,NULL); ,你不堵任何东西。

With s = pthread_sigmask(SIG_BLOCK, &set, NULL); , you're not blocking anything.

使用:

sigfillset(&set);
sets = pthread_sigmask(SIG_SETMASK, &set, NULL);

如果您想要阻止每一个信号,或者明确添加要阻止的信号设置如果你使用SIG_BLOCK。

If you want to block every signal, or explicitly add the signals you want to block to the set if you're using SIG_BLOCK.

您已经创建线程后,您需要恢复信号屏蔽,否则没有线程将捕获任何信号。

After you've created the threads, you need to restore the signal mask, otherwise no threads will catch any signal.

不过,看着你的previous的问题,它可能是线捕捉信号不处理被打断。也就是说,如果你正在做阻塞系统调用,信号到达时,即系统调用被中止。某些操作系统默认为自动再次调用系统调用,有的返回一个错误,errno设置为EINTR,应用程序必须处理 - 如果这不是处理可能发生不好的事情

However, looking at your previous question, it might be that the thread catching the signal doesn't handle being interrupted. That is, if you're blocked doing a syscall, and a signal arrives, that syscall gets aborted. Some operating systems defaults to automatically call the system call again, some returns an error and sets errno to EINTR, which the application must handle - and bad things might happen if that's not handled.

相反,随着sigaction()信号(),而不是和设置 SA_RESTART 标记,这将导致系统调用的情况下,自动重新启动它得到了由信号中断。

Instead, install your signal handlers with sigaction() instead of signal() , and set the SA_RESTART flag, which will cause system calls to automatically restart in case it got aborted by a signal.