MAIN.C
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <
signal.h> int cnt = 0;
void sighandler_new (int signo, siginfo_t *psigninfo, void *preserved) {int count = ++cnt;
printf ("%d Enter, Signo:%d\n", count, Signo);
Sleep (3);
printf ("%d leave, Signo:%d\n", count, Signo);
} int main (void) {struct Sigaction act; Act.sa_flags = Sa_siginfo |
Sa_restart;
Act.sa_sigaction = sighandler_new;
If sigquit in the signal screening set, and the program is executing SIGINT signal processing function,//At this time a sigquit signal, then sigquit temporarily blocked,//sigint signal processing function after the execution of the Sigquit signal processing function.
Sigemptyset (&act.sa_mask);
Sigaddset (&act.sa_mask, sigquit);
Sigaction (SIGINT, &act, NULL);
Char Buf[8];
int iret;
do {iret = Read (Stdin_fileno, buf, sizeof (BUF)-1);
if ( -1 = = Iret) {perror ("read error");
Exit (-1);
} Buf[iret] = ' + ';
Puts (BUF);
}while (strcmp (buf, "quit\n"));
return 0; }
Compile Run
Comment out the main function
Sigemptyset (&act.sa_mask); Sigaddset (&act.sa_mask, sigquit);
After these two lines of code, the compile run results are as follows
Both figures are first to send a SIGINT signal, and then send a sigquit signal. The first figure blocks the sigquit signal, and the second one does not block the sigquit signal
The Man Handbook has:
int sigaction (int signum, const struct sigaction *act,
struct sigaction *oldact);
struct Sigaction {
void (*sa_handler) (int);
void (*sa_sigaction) (int, siginfo_t *, void *);
sigset_t Sa_mask;
int sa_flags;
void (*sa_restorer) (void);
};
Sa_restart: automatically re-initiates the system call that is interrupted by the signal.
If the program is blocking a system call in the event of a signal, such as calling the Read () function, the signal is processed and then returned from the blocked system. If you do not specify this parameter, the Read function reads failed after the interrupt processing is complete, as shown in the following figure.