Longjmp 没有信号处理程序? [英] Longjmp out of signal handler?

查看:38
本文介绍了Longjmp 没有信号处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自问题:

是好的编程习惯吗在 C 中使用 setjmp 和 longjmp?

留下的两条评论说:

"你不能在信号处理程序中抛出异常,但你可以做一个longjmp 安全——只要你知道你在做什么.– 迪特里希埃普 8 月 31 日 19:57@Dietrich:+1 对您的评论.这是一个鲜为人知的完全被低估的事实.有很多问题不使用 longjmp 就无法解决(讨厌的竞争条件)信号处理程序.阻塞系统调用的异步中断是经典例子."

"You can't throw an exception in a signal handler, but you can do a longjmp safely -- as long as you know what you are doing. – Dietrich Epp Aug 31 at 19:57 @Dietrich: +1 to your comment. This is a little-known and completely-under-appreciated fact. There are a number of problems that cannot be solved (nasty race conditions) without using longjmp out of signal handlers. Asynchronous interruption of blocking syscalls is the classic example."

我的印象是,内核在遇到异常情况(例如除以 0)时会调用信号处理程序.此外,只有在您专门注册时才会调用它们.

I was under the impression that signal handlers were called by the kernel when it encountered an exceptional condition (e.g. divide by 0). Also, that they're only called if you specifically register them.

这似乎暗示(对我而言)它们不是通过您的正常代码调用的.

This would seem to imply (to me) that they aren't called through your normal code.

继续这个想法......据我所知,setjmp 和 longjmp 用于将堆栈折叠到先前的点和状态.我不明白在调用信号处理程序时如何折叠堆栈,因为它是从内核作为一次性情况而不是从您自己的代码调用的.信号处理程序堆栈中的下一件事是什么!?

Moving on with that thought... setjmp and longjmp as I understand them are for collapsing up the stack to a previous point and state. I don't understand how you can collapse up a stack when a signal handler is called since its called from the Kernel as a one-off circumstance rather than from your own code. What's the next thing up the stack from a signal handler!?

推荐答案

longjmp 不执行正常的堆栈展开.相反,堆栈指针只是从 setjmp 保存的上下文中恢复.

longjmp does not perform normal stack unwinding. Instead, the stack pointer is simply restored from the context saved by setjmp.

这里有一个说明这可能会让您在代码中使用非异步安全的关键部分.建议例如在关键代码期间屏蔽违规信号.

Here is an illustration on how this can bite you with non-async-safe critical parts in your code. It is advisable to e.g. mask the offending signal during critical code.

这篇关于Longjmp 没有信号处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆