我应该重载CtrlC信号并用它来打印信息。它不应该结束程序。重写Ctrl-C
到目前为止,会发生什么事是,当按CtrlÇ按下它打印的消息,但最终的方案。
当我问我的教授时,他告诉我这样做: 您需要让信号处理程序不要继续处理信号。现在信号正在被您的代码处理,然后继续处理父处理程序。
有没有一种方法,我应该添加或我需要将信号安装程序移到某个地方?
这是到目前为止我的代码:
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <signal.h>
#include "Input.h"
#include "CircleBuff.h"
//void handler_function(int signal_id);
void catch_int(int sig_num){
//reset the signal handler again to catch_int, for next time
signal(SIGINT, catch_int);
//print message
printf("Print History");
fflush(stdout);
}
void printHistory(CircleBuff hist){
cout << "Complete History:\n" << endl;
hist.print();
cout << endl;
}
int main(int argc, char** argv){
struct sigaction signal_action; /* define table */
signal_action.sa_handler = catch_int; /* insert handler function */
signal_action.sa_flags = 0; /* init the flags field */
sigemptyset(&signal_action.sa_mask); /* are no masked interrupts */
sigaction(SIGINT, &signal_action, NULL); /* install the signal_action */
do{
//My code: where the value report will be assigned within.
} while(report != 1)
}
您不应该在信号处理函数中调用'printf',因为'printf'不是异步安全的。有关异步安全功能的列表,请参见man 7 signal。更简单的'write'系统调用是异步安全的,但是没有一个是''功能。 –