What is async-signal safe function

Linux & Unix newsgroup archiveUNIX internals2001

Linuxmisc » UNIX internals

Andrew Giert #1 / 2 Mon, 20 Aug 2001 03:00:00 GMT

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.

This page is kept from the Linuxmisc archive of Linux and Unix newsgroups (snapshot 2022). Posts are shown as they were published, so commands, versions and addresses date from the original discussion. Linuxmisc.com now publishes a guide to online casinos in Canada.