gpt4 book ai didi

c++ - 调用 system(3) 时忽略 SIGINT

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:54:33 25 4
gpt4 key购买 nike

我有一个带有线程的进程,例如在循环中调用:system("ping -qnc 1 192.168.1.1")
当我执行 CTRL +C 时,根据文档忽略 SIGINT:

During execution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT will be ignored, in the process that calls system() (these signals will be handled according to their defaults inside the child process that executes command).

这是为什么,我该怎么做才能使用 CTRL +C 退出我的进程?

最佳答案

您的进程已阻止这些信号,因此当用户按下 Ctrl-C 时 ping 将被终止,而您的程序也不会被终止。

如果您希望您的程序被杀死,您可以检查system() 的返回值并查看ping 是否由于以下原因而被杀死一个信号。如果是,您可以采取适当的措施。

int status = system("ping -qnc 1 192.168.1.1");

if (WIFEXITED(status)) {
std::cout << "ping exited with exit code " << WEXITSTATUS(status) << std::endl;
}
else if (WIFSIGNALED(status)) {
std::cerr << "ping killed by signal " << WTERMSIG(status) << std::endl;
exit(1);
}
else {
std::cerr << "ping exited for some other reason" << std::endl;
}

关于c++ - 调用 system(3) 时忽略 SIGINT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58530499/

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