I am trying to use sigaction to ignore ctrl-c. The code is in the end to help understanding.The problem is when I press ctrl-C, ^Cctrl-c comes up instead of ^C, and it does not print next prompt instantly. Only until I hit Enter, it starts printing the prompt. Like this:
> // run the program sigAction
$./sigAction
sigAction>^Cctrl-C
// it is waiting,waiting . Nothing happens until I hit Enter, next prompt comes up.
sigAction>
The result I am looking for is when I hit ctrl-C without hitting Enter, next prompt should come up instantly. The problem is below:
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 }
When I use gdb to look inside, it gives me information below:
(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
Any idea helps! Thanks in advance!