Answer
The code is re-installing the signal handlers so that subsequent SIGINTs and SIGALRMs invoke the same signal handler again.
Why? Historically, some implementations of signal()
reset the signal disposition to the default (SIG_DFL) upon invoking a user-defined signal handler, effectively making the signal handler a "one shot" affair. Some did not do this.
Thus, a common practice was to have user-defined signal handlers re-install themselves to make the handler effectively permanent. (On systems that did not reset disposition, this merely made the signal handler slightly less efficient by introducing a pointless syscall.)
sigaction()
, the successor to signal()
which was standardized in POSIX.1-1988, resolves this ambiguity by providing a flag, SA_RESETHAND, to control whether or not the handler is reset.
Aside
The commenters above are quite right: the code you are studying is a bad example.
Any use of signal()
is dubious in modern, production-quality code. That alone would not pass code review in many shops.
Even allowing signal()
, the fact that the handler is not re-installed right away, before the printf()
, is a bit odd.
printf()
is not async-signal-safe. In very simple toy programs it can be used without problems in a signal handler, but even this very example is not safe. What if the printf()
in the handler is called during the main loop's printf()
? What if that five second ALRM handler call interrupts an INT handler call?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…