liveBook · Manning

Asynchronous signal safe function

As explained in Modern C

An asynchronous signal safe function is a function that may be called from a handler for an asynchronous signal without violating the signal-handling rules.

A signal handler can run while the program is doing something else. An asynchronous signal safe function may be called from that handler without violating C’s signal-handling rules. The restriction matters because a handler must not access or change program state in an uncontrolled way. section 17.6

The C standard guarantees only a small set of calls in this context:

  • abort, _Exit, and quick_exit, which terminate the program. section 17.6
  • signal, when used for the same signal number that invoked the handler. section 17.6
  • Certain functions acting on atomic objects. section 17.6

TAKEAWAY: Unless specified otherwise, a C library function is not asynchronous signal safe. In particular, do not call exit or perform I/O from an asynchronous signal handler. A platform may permit more, but that does not make those calls portable. section 17.6

143   /**
144    ** @brief A minimal signal handler
145    **
146    ** After updating the signal count, for most signals this
147    ** simply stores the signal value in "interrupt" and returns.
148    **/
149   static void signal_handler(int sig) {
150     sh_count(sig);
151     switch (sig) {
152     case SIGTERM: quick_exit(EXIT_FAILURE);
153     case SIGABRT: _Exit(EXIT_FAILURE);
154   #ifdef SIGCONT
155       // continue normal operation
156     case SIGCONT: return;
157   #endif
158     default:
159       /* reset the handling to its default */
160       signal(sig, SIG_DFL);
161       interrupt = sig;
162       return;
163     }
164   }


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":3}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":3}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":3}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":3}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":3}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":3}],[{\"line\":6,\"ch\":0},{\"line\":6,\"ch\":3}],[{\"line\":6,\"ch\":6},{\"line\":6,\"ch\":17}],[{\"line\":6,\"ch\":18},{\"line\":6,\"ch\":32}],[{\"line\":4,\"ch\":45},{\"line\":4,\"ch\":48}],[{\"line\":6,\"ch\":33},{\"line\":6,\"ch\":36}],[{\"line\":18,\"ch\":10},{\"line\":18,\"ch\":13}],[{\"line\":7,\"ch\":0},{\"line\":7,\"ch\":3}],[{\"line\":7,\"ch\":8},{\"line\":7,\"ch\":16}],[{\"line\":8,\"ch\":0},{\"line\":8,\"ch\":3}],[{\"line\":8,\"ch\":8},{\"line\":8,\"ch\":14}],[{\"line\":9,\"ch\":0},{\"line\":9,\"ch\":3}],[{\"line\":9,\"ch\":8},{\"line\":9,\"ch\":12}],[{\"line\":10,\"ch\":8},{\"line\":10,\"ch\":12}],[{\"line\":13,\"ch\":8},{\"line\":13,\"ch\":12}],[{\"line\":9,\"ch\":13},{\"line\":9,\"ch\":20}],[{\"line\":9,\"ch\":22},{\"line\":9,\"ch\":32}],[{\"line\":9,\"ch\":33},{\"line\":9,\"ch\":45}],[{\"line\":10,\"ch\":28},{\"line\":10,\"ch\":40}],[{\"line\":10,\"ch\":0},{\"line\":10,\"ch\":3}],[{\"line\":9,\"ch\":8},{\"line\":9,\"ch\":12}],[{\"line\":10,\"ch\":8},{\"line\":10,\"ch\":12}],[{\"line\":13,\"ch\":8},{\"line\":13,\"ch\":12}],[{\"line\":10,\"ch\":13},{\"line\":10,\"ch\":20}],[{\"line\":10,\"ch\":22},{\"line\":10,\"ch\":27}],[{\"line\":9,\"ch\":33},{\"line\":9,\"ch\":45}],[{\"line\":10,\"ch\":28},{\"line\":10,\"ch\":40}],[{\"line\":11,\"ch\":0},{\"line\":11,\"ch\":3}],[{\"line\":11,\"ch\":6},{\"line\":11,\"ch\":12}],[{\"line\":12,\"ch\":0},{\"line\":12,\"ch\":3}],[{\"line\":13,\"ch\":0},{\"line\":13,\"ch\":3}],[{\"line\":9,\"ch\":8},{\"line\":9,\"ch\":12}],[{\"line\":10,\"ch\":8},{\"line\":10,\"ch\":12}],[{\"line\":13,\"ch\":8},{\"line\":13,\"ch\":12}],[{\"line\":4,\"ch\":60},{\"line\":4,\"ch\":66}],[{\"line\":13,\"ch\":22},{\"line\":13,\"ch\":28}],[{\"line\":19,\"ch\":10},{\"line\":19,\"ch\":16}],[{\"line\":14,\"ch\":0},{\"line\":14,\"ch\":3}],[{\"line\":14,\"ch\":6},{\"line\":14,\"ch\":12}],[{\"line\":15,\"ch\":0},{\"line\":15,\"ch\":3}],[{\"line\":15,\"ch\":8},{\"line\":15,\"ch\":15}],[{\"line\":16,\"ch\":39},{\"line\":16,\"ch\":46}],[{\"line\":16,\"ch\":0},{\"line\":16,\"ch\":3}],[{\"line\":17,\"ch\":0},{\"line\":17,\"ch\":3}],[{\"line\":1,\"ch\":27},{\"line\":1,\"ch\":33}],[{\"line\":3,\"ch\":29},{\"line\":3,\"ch\":35}],[{\"line\":4,\"ch\":28},{\"line\":4,\"ch\":34}],[{\"line\":6,\"ch\":18},{\"line\":6,\"ch\":24}],[{\"line\":17,\"ch\":10},{\"line\":17,\"ch\":16}],[{\"line\":17,\"ch\":22},{\"line\":17,\"ch\":29}],[{\"line\":18,\"ch\":0},{\"line\":18,\"ch\":3}],[{\"line\":19,\"ch\":0},{\"line\":19,\"ch\":3}],[{\"line\":4,\"ch\":60},{\"line\":4,\"ch\":66}],[{\"line\":13,\"ch\":22},{\"line\":13,\"ch\":28}],[{\"line\":19,\"ch\":10},{\"line\":19,\"ch\":16}],[{\"line\":20,\"ch\":0},{\"line\":20,\"ch\":3}],[{\"line\":21,\"ch\":0},{\"line\":21,\"ch\":3}]]"}
!@%STYLE%@!

The handler demonstrates permitted exits through quick_exit and _Exit, and resets handling with signal for the signal it received. Its call to sh_count relies on the separately qualified atomic-counting implementation. section 17.6

How Modern C covers it

How deep it goes
Defines the rule, lists C-standard guarantees, and distinguishes portable guarantees from broader platform allowances.
Examples
Uses a handler that calls quick_exit or _Exit and resets handling with signal.
What it assumes
Assumes familiarity with C functions and signal handlers; introduces asynchronous signal safety as a named restriction.
What you can do after
Evaluate/understand whether a proposed handler call has a C-standard safety guarantee rather than assuming ordinary library calls are safe.
How it connects
Connects signal handlers to atomic objects, program termination, and portable C programming.

Also explained in

Compare how 2 books explain asynchronous signal safe function →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.