如何捕获程序结束前使用 SIGINT 的次数?例如:在一个仅在使用 SIGQUIT 时结束的程序中,并告诉我们用户在结束前按了多少次 ctr-c(使用了 SIGINT)。
到目前为止,我已经做到了:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd>
void sigproc1(int var);
void sigproc2(int var);
int main()
{
signal(SIGINT, sigproc1) //SIGINT - interactive attention request sent to the program.
signal(SIGQUIT, sigproc2) //SIGQUIT - The SIGQUIT signal is similar to SIGINT, except that it's controlled by a different key—the QUIT character, usually C-\—and produces a core dump when it terminates the process, just like a program error signal. You can think of this as a program error condition “detected” by the user.
}
void sigproc1(int var)
{
signal(SIGINT, sigproc1);
signal(SIGINT, sigproc2);
printf("You have pressed ctrl-c\n");
//Save the number of times that it received the SIGINT signal
//Print the number of times that it received the SIGINT signal
}
void sigproc2(int var)
exit(0); //Normal exit status.
}