Calling a standard library function in signal handler

主宰稳场 提交于 2020-01-03 15:32:33

问题


Why is calling a standard library function inside a signal handler discouraged?


回答1:


This is explained in the GNU LibC documentation.

If you call a function in the handler, make sure it is reentrant with respect to signals, or else make sure that the signal cannot interrupt a call to a related function.

And just in case, here's the Wikipedia page on reentrant functions.

A computer program or routine is described as reentrant if it can be safely called again before its previous invocation has been completed (i.e it can be safely executed concurrently).




回答2:


Its not only re-entrancy issues, depending on the signal being services you also want to avoid inadvertent calls to malloc() (i.e. asprintf()) and other variadic expansion (i.e. printf()).




回答3:


It is all running fine and stuff, until you run into some mysterious bugs which are totally untraceable :)

man 7 signal will give you a list of system calls which are safe to call from a signal handler. It is described in POSIX as well.




回答4:


Because the library function may not be reentrant.



来源:https://stackoverflow.com/questions/2461583/calling-a-standard-library-function-in-signal-handler

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