In section 8.13, we show the implementation of a system function. However, that version did not process the signal. POSIX.1 requires system to ignore SIGINT and Sigquit and block sigchld. Before showing the correct handling of these signals, let's see why we need to worry about the processing of these signals.
The following code uses the system version of section 8.13 to invoke the Ed editor. (This editor has been part of the UNIX system for a long time.) We use it here because it is an interactive program that captures interrupt and exit signals. If we call a shell and enter a break, it captures the interrupt and prints a question mark. The ED program also sets the exit signal to the layout so that it is ignored. )
<span style= "FONT-SIZE:18PX;" ><strong> #include <signal.h>
static void
sig_int (int signo)
{
printf ("Caught SIGINT \ n ");
}
static void
sig_chld (int signo)
{
printf ("Caught sigchld\n");
}
int
main (void)
{
if (signal (SIGINT, sig_int) = = Sig_err) {
printf ("Signal (SIGINT) error\n");
Exit (1);
}
if (Signal (SIGCHLD, sig_chld) = = Sig_err) {
printf ("Signal (SIGCHLD) error\n");
Exit (1);
}
if (System ("/bin/ed") < 0) {
printf ("System () error");
Exit (1);
}
Exit (0);
} </strong></span>
The code above captures both SIGINT and SIGCHLD. The results of the operation are:
$./a.out
A (add text command)
This is one line of text
. (terminating add mode)
1, $p (printing starting from the first line)
This is one line of text
W Temp.foo (write buffer to file)
25 (25 bytes written)
Q (Exit)
Caught SIGCHLD