What is async-signal safe function
Linuxmisc » UNIX internals
Ravi> Hi, One is not allowed to use async-signal unsafe function in
Ravi> signal handlers. What exactly is a async-signal unsafe
Ravi> function.?
A function is async-signal-safe if you can interrupt it with a signal,
and successfully call it from within the signal handler without it
behaving incorrectly as a result.
The usual reasons why a function could be *unsafe* with respect to
signals are:
- the function potentially calls other functions which are not safe
(this is why the stdio library, fopen()/fclose()/fgetc() etc. are
unsafe, because virtually all the routines in it can call malloc()
or free())
- the function modifies static or global data structures (this is
why malloc() and free() are unsafe)
In some cases an unsafe function can be made safe by blocking signals
around the unsafe parts.
Signal safety is very closely related to the more general concept of
"reentrancy" (in fact the latter term was often used, but it is not
precisely correct).
Ravi> From what I understand, the function (like malloc(),
Ravi> pthread_*() ), that if reentered because of a signal
Ravi> (asynchronous) may cause deadlock or cause some corruption are
Ravi> async-signal unsafe. But then if I mask out the signals during
Ravi> the processing in my signal handler then does it make it
Ravi> async-signal safe .
No. You would need to mask out signals during the calls *in the main
program*, not in the signal handler.
It is specifically allowed to call unsafe functions from a signal
handler *if and only if* you can guarantee that the signal did not
itself interrupt an unsafe function.
--
Andrew.