gpt4 book ai didi

c - 使用 sigaction 实现 ctr-c

转载 作者:太空宇宙 更新时间:2023-11-04 03:28:48 25 4
gpt4 key购买 nike

我正在尝试使用 sigaction 来忽略 ctrl-c。代码最后是为了帮助理解。问题是当我按 ctrl-C 时,^Cctrl-c 出现而不是 ^C,并且它不打印next 立即提示。直到我按下 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

任何想法都有帮助!提前致谢!

最佳答案

正如我在评论中指出的那样,您并不是真的想使用 SA_RESTART。这意味着当 read() 获得中断时,读取在中断后恢复,这排除了您的主程序打印新提示的机会。

这段代码似乎很可能接近你想要的:

#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static void sigIntHandler(int signum)
{
fprintf(stderr, "Ctrl-C (%d)\n", signum);
}

int main(void)
{
struct sigaction signalAction;
signalAction.sa_handler = sigIntHandler;
sigemptyset(&signalAction.sa_mask);
signalAction.sa_flags = 0; // SA_RESTART;
sigaction(SIGINT, &signalAction, NULL);

while (1)
{
char block[4096];

printf("sigAction>");
fflush(stdout);

if (fgets(block, 4096, stdin) == 0)
{
if (errno == EINTR)
{
errno = 0;
clearerr(stdin);
fprintf(stderr, "Interrupted!\n");
continue;
}
else
{
fprintf(stderr, "EOF spotted\n");
break;
}
}

if (!strcmp(block, "exit\n"))
{
fprintf(stderr, "Exiting\n");
exit(1);
}
printf("Read: [%.*s]\n", (int)(strlen(block) - 1), block);
}
}

请注意,正式地,您不应该在信号处理程序中使用 printf() 系列函数 — 参见 How to avoid using printf() in a signal handler?

运行示例(名为 sigact 的程序):

$ ./sigact
sigAction>Ordinary input
Read: [Ordinary input]
sigAction>Some typing, then an interrupt! ^CCtrl-C (2)
Interrupted!
sigAction>More input?
Read: [More input?]
sigAction>Yes, more input.
Read: [Yes, more input.]
sigAction>And why you type control-D?
Read: [And why you type control-D?]
sigAction>EOF spotted
$ ./sigact
sigAction>Also typing exit works, of course.
Read: [Also typing exit works, of course.]
sigAction>exit
Exiting
$

^C 来自终端驱动程序(在 Mac OS X 10.11.6 上)。 lflags 第一行末尾的 echoctl 是这样做的:

$ stty -a
speed 9600 baud; 50 rows; 141 columns;
lflags: icanon isig iexten echo echoe -echok echoke -echonl echoctl
-echoprt -altwerase -noflsh -tostop -flusho pendin -nokerninfo
-extproc
iflags: -istrip icrnl -inlcr -igncr ixon -ixoff ixany imaxbel iutf8
-ignbrk brkint -inpck -ignpar -parmrk
oflags: opost onlcr -oxtabs -onocr -onlret
cflags: cread cs8 -parenb -parodd hupcl -clocal -cstopb -crtscts -dsrflow
-dtrflow -mdmbuf
cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = <undef>;
eol2 = <undef>; erase = ^?; intr = ^C; kill = ^X; lnext = ^V;
min = 1; quit = ^\; reprint = ^R; start = ^Q; status = ^T;
stop = ^S; susp = ^Z; time = 0; werase = ^W;
$

关于c - 使用 sigaction 实现 ctr-c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38959797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com