陷阱“段错误"是否有意义? [英] Is there a point to trapping "segfault"?

查看:77
本文介绍了陷阱“段错误"是否有意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,如果有足够的上下文,人们可能希望从 segfault 条件中进行建设性的使用(即恢复).

I know that, given enough context, one could hope to use constructively (i.e. recover) from a segfault condition.

但是,付出努力值得吗?如果是,在什么情况下?

But, is the effort worth it? If yes, in what situation(s) ?

推荐答案

您真的不能希望从段错误中恢复过来.您可以检测到它发生了,并在可能的情况下转储相关的特定于应用程序的状态,但是您无法继续该过程.这是因为(以及其他)

You can't really hope to recover from a segfault. You can detect that it happened, and dump out relevant application-specific state if possible, but you can't continue the process. This is because (amongst others)

  • 失败的线程无法继续,因此您唯一的选择是longjmp或终止线程.在大多数情况下都不安全.
  • 无论哪种方式,您都可以将互斥锁/锁定保持在锁定状态,这会导致其他线程永远等待
  • 即使没有发生,您也可能会泄漏资源
  • 即使您不执行上述任何一项操作,遇到故障的段隔离线程也可能使应用程序的内部状态不一致.内部状态不一致可能会导致数据错误或其他进一步的不良行为,这不仅会导致退出,还会导致更多的问题.

因此,通常来说,除了以相当突然的方式终止进程之外,没有任何必要诱捕它并做任何事情.试图将(重要的)数据写回到光盘或继续做其他有用的工作没有任何意义.将状态转储到日志中有一点-许多应用程序都会这样做-然后退出.

So in general, there is no point in trapping it and doing anything EXCEPT terminating the process in a fairly abrupt fashion. There's no point in attempting to write (important) data back to disc, or continue to do other useful work. There is some point in dumping out state to logs- which many applications do - and then quitting.

一个可能有用的事情可能是执行exec()您自己的进程,或者让一个监视程序在崩溃时将其重新启动. (注意:如果您的进程具有> 1个线程,则exec并不总是具有良好定义的行为)

A possibly useful thing to do might be to exec() your own process, or have a watchdog process which restarts it in the case of a crash. (NB: exec does not always have well defined behaviour if your process has >1 thread)

这篇关于陷阱“段错误"是否有意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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