views:

88

answers:

4

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

+5  A: 

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

Macmade
A: 

Because the library function may not be reentrant.

Paul R
+1  A: 

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

Tim Post
A: 

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.

Roman D