gpt4 book ai didi

c - 为什么在 C 中使用 `sigsetjmp` 而不是 `setjmp` 函数?

转载 作者:太空狗 更新时间:2023-10-29 17:19:13 32 4
gpt4 key购买 nike

为什么有人会选择在 C 中使用 sigsetjmp 而不是 setjmp

我在一本书上看到在信号相关的代码中使用setjmp有一些缺点,所以实现了sigsetjmp功能。

谁能举个例子解释一下?

提前致谢。

最佳答案

参见 section 10.15 in “Advanced Programming in the UNIX® Environment” :

the setjmp and longjmp functions, which can be used for nonlocalbranching. The longjmp function is often called from a signal handlerto return to the main loop of a program, instead of returning from thehandler. We saw this in Figures 10.8 and 10.11.

There is a problem in calling longjmp, however. When a signal iscaught, the signal-catching function is entered with the currentsignal automatically being added to the signal mask of the process.This prevents subsequent occurrences of that signal from interruptingthe signal handler. If we longjmp out of the signal handler, whathappens to the signal mask for the process?

Under FreeBSD 5.2.1 and Mac OS X 10.3, setjmp and longjmp save and restore the signal mask. Linux 2.4.22 and Solaris 9, however, do notdo this. FreeBSD and Mac OS X provide the functions _setjmp and_longjmp, which do not save and restore the signal mask.

To allow either form of behavior, POSIX.1 does not specify the effectof setjmp and longjmp on signal masks. Instead, two new functions,sigsetjmp and siglongjmp, are defined by POSIX.1. These two functionsshould always be used when branching from a signal handler.

...

The only difference between these functions and the setjmp and longjmpfunctions is that sigsetjmp has an additional argument. If savemask isnonzero, then sigsetjmp also saves the current signal mask of theprocess in env. When siglongjmp is called, if the env argument wassaved by a call to sigsetjmp with a nonzero savemask, then siglongjmprestores the saved signal mask.

关于c - 为什么在 C 中使用 `sigsetjmp` 而不是 `setjmp` 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20755260/

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