Calling a standard library function in signal hand

2019-06-21 06:08发布

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

标签: c unix gcc signals
4条回答
Deceive 欺骗
2楼-- · 2019-06-21 06:34

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楼-- · 2019-06-21 06:43

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).

查看更多
Evening l夕情丶
4楼-- · 2019-06-21 06:48

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.

查看更多
劫难
5楼-- · 2019-06-21 06:49

Because the library function may not be reentrant.

查看更多
登录 后发表回答