liveBook · Manning

Sig_atomic_t

As explained in Modern C

sig_atomic_t is an integer C type whose object evaluations and assignments are guaranteed to be uninterruptible with respect to signal handling.

Record a signal number

A signal handler can assign the received signal number to a sig_atomic_t object and return. The assignment remains uninterruptible with respect to signal handling. 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 default case assigns sig to interrupt before returning; the surrounding example uses sig_atomic_t for that object. section 17.6

TAKEAWAY: sig_atomic_t is an integer type whose object evaluations (loads) and assignments (stores) appear indivisible to signal handlers. “Uninterruptible” describes what can be observed, not necessarily how the operation is performed. Its width may be limited, though it is at least 8 bits. section 17.6

Do not use it as a counter: ++ can involve separate load, increment, and store steps, and the value can overflow. When counting signals, a lock-free atomic type is a distinct option where C11 atomics are available. section 17.6

How Modern C covers it

How deep it goes
Defines uninterruptibility and limits the guarantee to loads and stores, not increments.
Examples
Uses a handler that stores its received signal number and contrasts it with signal counting.
What you can do after
Evaluate/understand when sig_atomic_t supports a handler’s load or store and why it should not serve as a counter.
How it connects
Connects signal handlers, uninterruptible operations, atomic_flag, and lock-free atomic types.

Also explained in

Compare how 2 books explain sig_atomic_t →

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.