分段错误处理程序是否应该由应用程序程序员用 C 编写? [英] Should segmentation fault handlers be written at all by a application programmer in C?

查看:15
本文介绍了分段错误处理程序是否应该由应用程序程序员用 C 编写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果某人是操作系统程序员或编写系统级库代码,则编写分段错误处理程序是有意义的.例如,操作系统程序员会编写代码,向该应用程序进程发送信号 SIGSEGV.或者,系统库程序员可能会处理该信号 SIGSEGV,并且可能会撤消由库代码导致的用于创建分段故障的操作.但是为什么 C 中的应用程序程序员需要编写分段错误处理程序呢?如果他编写一个处理程序,他已经损坏了一些内存部分.你能举个例子,让应用程序程序员处理分段错误并继续执行程序吗?

If someone is a operating system programmer or writing a system level library code, it makes sense to write a segmentation fault handler. Like, for example, OS programmer would write code send a signal SIGSEGV to that application process. OR a systems library programmer might handle that signal SIGSEGV and may undo the operations caused by the library code for creating segmentation Fault. But why would an application programmer in C need to write segmentation fault handler? If he writes an handler, he has already corrupted some parts of memory. Can you give an instance, for an application programmer to handle segmentation fault and continue execution of the program?

推荐答案

处理 SIGSEGV 等可能允许保存状态并采取纠正措施.Mr 32(和其他人)是正确的,您不能简单地重新启动主行代码.相反,您可以 longjmp()siglongjmp();这允许主线重新开始.此外,您必须非常小心地仅调用 async safe 函数.这是非常棘手的.然而,有些应用程序是,

Handling SIGSEGV, etc, may allow saving state and taking corrective actions. Mr 32 (and others) are correct and you can not simply restart the main line code. Instead you can longjmp()siglongjmp(); this allows a re-start of the main line. Also, you have to be very careful to call async safe functions only. This is very tricky. However some applications are,

  1. 健康/安全 - 确保不会发生灾难性情况.
  2. 财务 - 交易数据丢失可能导致资金损失.
  3. 控制系统 - 化学家滴定软件示例.
  4. 诊断 - 可能会记录崩溃情况以改进未来的软件.根据 Jay

调用 exit() 可能不好,_exit() 会更好.区别在于 atexit() 调用.

Calling exit() is probably not good and _exit() would be better. The difference being atexit() calls.

另请参阅:证书异步安全Glibc 异步安全列表类似问题, longjmp() 和信号不可移植,
这些因操作系统而异.任何建议都将取决于系统!

See also: Cert async safe, Glibc async-safe list, Similar question, longjmp() and signals not portable, async-safe
These vary from OS to OS. Any advice will be system dependent!

其他问题

  • 程序使用的某些库可能会捕获 SIGSEGV.绝对是 Empress 数据库的版本.您必须知道您的库正在使用什么并链接到它们.
  • 堆栈和堆(malloc 等)可能被损坏,包括 jump_buf,因此您的错误处理可能特别偏执.
  • 还有许多其他替代解决方案,例如将关键部分推迟到另一个更简单的任务.
  • 根据 C99 标准,从信号调用的
  • longjmp()undefined,但它在大多数系统上都能正常工作.siglongjmp() 可以在以下情况下使用你比较迂腐.它可以用于诊断日志记录,但我不会将它用于列出的其他用途(安全等).通知看门狗任务可能更合适.
  • Some libraries used by the program may catch SIGSEGV. Definitely version of the Empress Database hook it. You have to know what your libraries are using and chain to/from them.
  • Stack and heap (malloc,etc) can be corrupted, including the jump_buf so your error handling maybe especially paranoid.
  • There are many other alternate solutions, such as defer critical portions to another task that is much simpler.
  • longjmp() called from a signal is undefined according to the C99 standard, but it will work well on most systems. siglongjmp() can be used if you are more pedantic. It would be fine for diagnostic logging, but I wouldn't use it for the other uses listed (safety, etc). Notifying a watchdog task maybe more appropriate.

这篇关于分段错误处理程序是否应该由应用程序程序员用 C 编写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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