Why only async-safe functions should be called from a signal handler?

十年热恋 提交于 2019-11-28 10:48:51

问题


I understand that, from a signal handler function sigaction() I should only call those functions that are "async-safe". But why is so?


回答1:


Calling an unsafe function may lead to undefined behavior.

The Open Group Base Specifications Issue 7 (POSIX.1-2008), in its treatment of "Signal Concepts", says:

[W]hen a signal interrupts an unsafe function ... and the signal-catching function calls an unsafe function, the behavior is undefined.

As to why unsafe functions are unsafe, there may be many reasons in a given implementation.

However, a previous version of the standard, Issue 6 (POSIX.1-2004), hints at one possible reason on some implementations. That version describes async-signal-safe functions as "either reentrant or non-interruptible by signals". So, consider a function which relies on static data to keep state but is interrupted by itself midway through its execution — can that data be trusted once control returns to the interrupted function?



来源:https://stackoverflow.com/questions/46354925/why-only-async-safe-functions-should-be-called-from-a-signal-handler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!