liveBook · Manning

Signal.h

As explained in Modern C

signal.h is the C standard header that declares signal-handling functions, constants, and related types.

A program can call signal(SIGINT, SIG_IGN) to request that an interactive attention signal be ignored. The C header signal.h defines the signal-handling interface, including signal numbers, dispositions, and functions such as signal and raise. section 17.6

A handled signal interrupts normal control flow at a point the application cannot foresee. When the handler returns, control resumes where it was interrupted. section 17.6

C specifies six signal numbers:

  • SIGFPE, SIGILL, and SIGSEGV indicate an erroneous arithmetic operation, an invalid instruction, and an invalid access to storage, respectively. section 17.6
  • SIGABRT indicates abnormal termination; SIGINT indicates an interactive attention signal; SIGTERM indicates a termination request. section 17.6

SIG_DFL restores the platform's default disposition for a signal; SIG_IGN requests that it be ignored. A program may also supply its own handler. TAKEAWAY: C's interface is minimal and suited to elementary situations; platform-specific signals and rules reduce portability. section 17.6

 92   /**
 93    ** @ brief Enables a signal handler and catches the errors
 94    **/
 95   sh_handler* sh_enable(int sig, sh_handler* hnd) {
 96     sh_handler* ret = signal(sig, hnd);
 97     if (ret == SIG_ERR) {
 98       SH_PRINT(stderr, sig, "failed");
 99       errno = 0;
100     } else if (ret == SIG_IGN) {
101       SH_PRINT(stderr, sig, "previously ignored");
102     } else if (ret && ret != SIG_DFL) {
103       SH_PRINT(stderr, sig, "previously set otherwise");
104     } else {
105         SH_PRINT(stderr, sig, "ok");
106     }
107     return ret;
108   }


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":1},{\"line\":0,\"ch\":3}],[{\"line\":1,\"ch\":1},{\"line\":1,\"ch\":3}],[{\"line\":2,\"ch\":1},{\"line\":2,\"ch\":3}],[{\"line\":3,\"ch\":1},{\"line\":3,\"ch\":3}],[{\"line\":3,\"ch\":18},{\"line\":3,\"ch\":27}],[{\"line\":3,\"ch\":28},{\"line\":3,\"ch\":31}],[{\"line\":4,\"ch\":1},{\"line\":4,\"ch\":3}],[{\"line\":1,\"ch\":28},{\"line\":1,\"ch\":34}],[{\"line\":4,\"ch\":26},{\"line\":4,\"ch\":32}],[{\"line\":5,\"ch\":1},{\"line\":5,\"ch\":3}],[{\"line\":5,\"ch\":8},{\"line\":5,\"ch\":10}],[{\"line\":8,\"ch\":15},{\"line\":8,\"ch\":17}],[{\"line\":10,\"ch\":15},{\"line\":10,\"ch\":17}],[{\"line\":5,\"ch\":19},{\"line\":5,\"ch\":26}],[{\"line\":6,\"ch\":1},{\"line\":6,\"ch\":3}],[{\"line\":6,\"ch\":19},{\"line\":6,\"ch\":25}],[{\"line\":9,\"ch\":19},{\"line\":9,\"ch\":25}],[{\"line\":11,\"ch\":19},{\"line\":11,\"ch\":25}],[{\"line\":13,\"ch\":21},{\"line\":13,\"ch\":27}],[{\"line\":7,\"ch\":1},{\"line\":7,\"ch\":3}],[{\"line\":7,\"ch\":10},{\"line\":7,\"ch\":15}],[{\"line\":8,\"ch\":0},{\"line\":8,\"ch\":3}],[{\"line\":8,\"ch\":10},{\"line\":8,\"ch\":17}],[{\"line\":10,\"ch\":10},{\"line\":10,\"ch\":17}],[{\"line\":8,\"ch\":26},{\"line\":8,\"ch\":33}],[{\"line\":9,\"ch\":0},{\"line\":9,\"ch\":3}],[{\"line\":6,\"ch\":19},{\"line\":6,\"ch\":25}],[{\"line\":9,\"ch\":19},{\"line\":9,\"ch\":25}],[{\"line\":11,\"ch\":19},{\"line\":11,\"ch\":25}],[{\"line\":13,\"ch\":21},{\"line\":13,\"ch\":27}],[{\"line\":10,\"ch\":0},{\"line\":10,\"ch\":3}],[{\"line\":8,\"ch\":10},{\"line\":8,\"ch\":17}],[{\"line\":10,\"ch\":10},{\"line\":10,\"ch\":17}],[{\"line\":10,\"ch\":33},{\"line\":10,\"ch\":40}],[{\"line\":11,\"ch\":0},{\"line\":11,\"ch\":3}],[{\"line\":6,\"ch\":19},{\"line\":6,\"ch\":25}],[{\"line\":9,\"ch\":19},{\"line\":9,\"ch\":25}],[{\"line\":11,\"ch\":19},{\"line\":11,\"ch\":25}],[{\"line\":13,\"ch\":21},{\"line\":13,\"ch\":27}],[{\"line\":12,\"ch\":0},{\"line\":12,\"ch\":3}],[{\"line\":8,\"ch\":10},{\"line\":8,\"ch\":14}],[{\"line\":10,\"ch\":10},{\"line\":10,\"ch\":14}],[{\"line\":12,\"ch\":10},{\"line\":12,\"ch\":14}],[{\"line\":13,\"ch\":0},{\"line\":13,\"ch\":3}],[{\"line\":6,\"ch\":19},{\"line\":6,\"ch\":25}],[{\"line\":9,\"ch\":19},{\"line\":9,\"ch\":25}],[{\"line\":11,\"ch\":19},{\"line\":11,\"ch\":25}],[{\"line\":13,\"ch\":21},{\"line\":13,\"ch\":27}],[{\"line\":1,\"ch\":28},{\"line\":1,\"ch\":31}],[{\"line\":3,\"ch\":32},{\"line\":3,\"ch\":35}],[{\"line\":4,\"ch\":26},{\"line\":4,\"ch\":29}],[{\"line\":6,\"ch\":27},{\"line\":6,\"ch\":30}],[{\"line\":9,\"ch\":27},{\"line\":9,\"ch\":30}],[{\"line\":11,\"ch\":27},{\"line\":11,\"ch\":30}],[{\"line\":13,\"ch\":29},{\"line\":13,\"ch\":32}],[{\"line\":14,\"ch\":0},{\"line\":14,\"ch\":3}],[{\"line\":15,\"ch\":0},{\"line\":15,\"ch\":3}],[{\"line\":15,\"ch\":8},{\"line\":15,\"ch\":14}],[{\"line\":16,\"ch\":0},{\"line\":16,\"ch\":3}]]"}
!@%STYLE%@!

This wrapper calls signal to enable a handler and checks for an error or a previous disposition. section 17.6

How Modern C covers it

How deep it goes
Explains standard signal numbers, dispositions, handler registration, and portability limits, while noting platform-specific extensions.
Examples
Uses a parser's signal handler and a registration wrapper that checks signal's return value.
What you can do after
Apply the C signal interface to elementary handler registration and interpret standard dispositions.
How it connects
Connects signal handling with control flow, computational exceptions, abnormal termination, and portability.

Also explained in

Compare how 2 books explain signal.h →

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.