我正在尝试使用sigaction来忽略ctrl-c。代码到底有助于理解。问题是当我按ctrl-C, ^ Cctrl-c 出现而不是 ^ C 时,它不会打印立即提示。只有在我点击 Enter 之后,它才会开始打印提示。像这样:
> // run the program sigAction
$./sigAction
sigAction>^Cctrl-C
// it is waiting,waiting . Nothing happens until I hit Enter, next prompt comes up.
sigAction>
我正在寻找的结果是当我点击ctrl-C而没有按Enter键时,下一个提示应该立即出现。问题如下:
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <signal.h>
4 #include <string.h>
5 // implement ignoring ctrl-C
6 void sigIntHandler(int signum) {
7 fprintf(stderr, "ctrl-C\n" );
8
9 }
10
11 main()
12 {
13
14 struct sigaction signalAction;
15 signalAction.sa_handler = sigIntHandler;
16 sigemptyset(&signalAction.sa_mask);
17 signalAction.sa_flags = SA_RESTART;
18 sigaction(SIGINT, &signalAction, NULL);
19 while(1) {
20
21 char block[4096];
22
23 printf("sigAction>");
24 fflush(stdout);
25
26 fgets(block, 4096, stdin);
27
28 if (!strcmp(block, "exit\n")) {
29 exit(1);
30 }
31 }
32 }
当我使用gdb查看内部时,它给出了以下信息:
(gdb) n
Single stepping until exit from function main,
which has no line number information.
sigAction>^C
Program received signal SIGINT, Interrupt.
0x00007ffff7b15d10 in __read_nocancel () from /lib64/libc.so.6
(gdb) n
Single stepping until exit from function __read_nocancel,
which has no line number information
任何想法都有帮助!提前谢谢!