Applying the EINTR Error Code
Except when the sigsuspend(2) technique is used, a signal can be caught by a signal handler at any time. This restricts the choice of available functions to those that are re-entrant. Consequently, when non–re-entrant functions must be called, a different technique must be used.
A signal handler can post a result to a global flag variable, which is later polled by the application. Using this technique, no re-entrancy issues arise because the event is synchronous (polled) instead of being asynchronous. The following example shows a signal handler that posts a true result to the flag variable gotSIGINT:
static int gotSIGINT = 0; /* True when SIGINT arrives */ static void catch_SIGINT(int signo) { gotSIGINT = 1; /* ...
Get Advanced UNIX Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.