F
Francois Grieu
Hello,
in C99, the signal handler for a SIGTERM signal is subject
to restrictions, including 7.14.1.1#5
If the signal occurs other than as the result of calling
the abort or raise function, the behavior is undefined
if the signal handler refers to any object with static
storage duration other than by assigning a value to an
object declared as volatile sig_atomic_t, or the signal
handler calls any function in the standard library other
than the abort function, the _Exit function, or the
signal function with the first argument equal to the
signal number corresponding to the signal that caused the
invocation of the handler.
Clearly, it is legal that the the signal handler for a
SIGTERM signal sets a sig_atomic_t flag, and poll that
flag in the normal program flow to terminate gracefully.
Is another option possible, allowing more freedom, e.g.
calling printf to output the best result found so far,
with no test for that in the normal program flow?
I'm thinking of also defining at startup a signal handler
for SIGABRT, and calling abort in the signal handler for
SIGTERM; it seems that the signal handler for SIGABRT will
be called, and it seems less restrictions apply on this one
as it occurs as the result of calling the abort function.
Is that correct?
If yes, I get that we need a mechanism to insure consistency
of data such as "the best result found so far". Do we also
need a mechanism preventing reentrant use of <stdio.h>
functions, or is it just fine to printf to a known open file
in a/this SIGABRT signal handler?
Anyone has a known-correct illustration of directly acting
on SIGTERM rather than by polling in the normal program
flow?
Off topic: what is there in the Linux/Posix/unamit specs
justifying the common use of printf in a SIGTERM/SIGABRT
signal handler?
TIA,
Francois Grieu
in C99, the signal handler for a SIGTERM signal is subject
to restrictions, including 7.14.1.1#5
If the signal occurs other than as the result of calling
the abort or raise function, the behavior is undefined
if the signal handler refers to any object with static
storage duration other than by assigning a value to an
object declared as volatile sig_atomic_t, or the signal
handler calls any function in the standard library other
than the abort function, the _Exit function, or the
signal function with the first argument equal to the
signal number corresponding to the signal that caused the
invocation of the handler.
Clearly, it is legal that the the signal handler for a
SIGTERM signal sets a sig_atomic_t flag, and poll that
flag in the normal program flow to terminate gracefully.
Is another option possible, allowing more freedom, e.g.
calling printf to output the best result found so far,
with no test for that in the normal program flow?
I'm thinking of also defining at startup a signal handler
for SIGABRT, and calling abort in the signal handler for
SIGTERM; it seems that the signal handler for SIGABRT will
be called, and it seems less restrictions apply on this one
as it occurs as the result of calling the abort function.
Is that correct?
If yes, I get that we need a mechanism to insure consistency
of data such as "the best result found so far". Do we also
need a mechanism preventing reentrant use of <stdio.h>
functions, or is it just fine to printf to a known open file
in a/this SIGABRT signal handler?
Anyone has a known-correct illustration of directly acting
on SIGTERM rather than by polling in the normal program
flow?
Off topic: what is there in the Linux/Posix/unamit specs
justifying the common use of printf in a SIGTERM/SIGABRT
signal handler?
TIA,
Francois Grieu