Signalcatch or ignore interrupts
#include <signal.h>
typedef int (*sighandler_t)(int);
sighandler_t signalint interruptsighandler_t addressDescription
This call is a comprehensive method of catching or ignoring
signals sent to the current process. Notice that "kill()" does
the sending of signals, and "signal()" does the catching.
Normally, a signal sent to a process causes it to terminate
with the status of the signal. If, in advance of the
anticipated signal, this system call is used, the program has
the choice of ignoring the signal or designating a function to
be executed when it is received. Different functions may be
designated for different signals.
The values for "address" have the following meanings:
0reset to the default i.e. abort when received.1ignore; this will apply until reset to another value.Otherwisetaken to be the address of a C function which
is to be executed on receipt of the signal.
If the latter case is chosen, when the signal is received by
the process the "address" is reset to 0, the default, before
the function is executed. This means that if the next signal
received should be caught then another call to "signal()"
should be made immediately. This is normally the first action
taken by the "interrupt" function. The function may access the
signal number which caused its execution by looking at its
argument. On completion of this function the program resumes
at the point at which is was "interrupted" by the signal.
An example should help to clarify all this. Suppose a program
needs to create a temporary file which should be deleted before
exiting. The body of the program might contain fragments like
this:
pn = creat("temp",3); /* create a temporary file */
signal(2,intrupt); /* ensure tidying up */
signal(3,intrupt);
write(pn,string,count);
close(pn); /* finished writing */
unlink("temp"); /* delete it */
exit(0); /* normal exit */
The call to "signal()" will ensure that if a keyboard or quit
signal is received then the function "intrupt()" will be
executed and this might be written:
intrupt(sig)
{
close(pn); /* close it if open */
unlink("temp"); /* delete it */
exit(sig); /* received signal er exit status */
}
In this case, as the function will be exiting before another
signal is received, it is unnecessary to call "signal()" again
to reset its pointer. Note that either the function
"intrupt()" should appear in the source code before the call to
"signal()", or it should be pre-declared.
The signals used by OS-9 are defined in the header file as
follows:
/* OS-9 signals */
#define SIGKILL 0 /* system abort (cannot be caught or ignored)*/
#define SIGWAKE 1 /* wake up */
#define SIGQUIT 2 /* keyboard abort */
#define SIGINT 3 /* keyboard interrupt */
/* special addresses */
#define SIG_DFL 0 /* reset to default */
#define SIG_IGN 1 /* ignore */
Please note that there is another method of trapping signals,
namely "intercept()" (q.v.). However, since "signal()" and
"intercept()" are mutually incompatible, calls to both of them
must not appear in the same program. The link-loader will
preven the creation of an executable program in which both are
called by aborting with an "entry name clash" error for
"_sigint".
See Also
intercept(),
OS-9 shell command "kill",
kill()