Linux 中的 Ctrl + C 中断事件处理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17766550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ctrl + C interrupt event handling in Linux
提问by mozcelikors
I am developing an application that uses C++ and compiles using Linux GNU C Compiler.
我正在开发一个使用 C++ 并使用 Linux GNU C 编译器编译的应用程序。
I want to invoke a function as the user interrupts the script using Ctrl+ Ckeys.
我想在用户使用Ctrl+C键中断脚本时调用一个函数。
What should I do? Any answers would be much appreciated.
我该怎么办?任何答案将不胜感激。
采纳答案by Grijesh Chauhan
When you press Ctr + C, the operating system sends a signalto the process. There are many signals and one of them is SIGINT. The SIGINT ("program interrupt") is one of the Termination Signals.
当您按下 时Ctr + C,操作系统会向该进程发送一个信号。有许多信号,其中之一是 SIGINT。SIGINT(“程序中断”)是终止信号之一。
There are a few more kinds of Termination Signals, but the interesting thing about SIGINT is that it can be handled (caught) by your program. The default action of SIGINT is program termination. That is, if your program doesn't specifically handle this signal, when you press Ctr + Cyour program terminates as the default action.
还有更多种类的终止信号,但关于 SIGINT 的有趣之处在于它可以由您的程序处理(捕获)。SIGINT 的默认操作是程序终止。也就是说,如果你的程序没有专门处理这个信号,当你按下Ctr + C你的程序时,你的程序将作为默认操作终止。
To change the default action of a signal you have to register the signal to be caught. To register a signal in a C program (at least under POSIX systems) there are two functions
要更改信号的默认操作,您必须注册要捕获的信号。要在 C 程序中注册信号(至少在 POSIX 系统下),有两个函数
- signal(int signum, sighandler_t handler);
- sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);.
- 信号(int signum,sighandler_t 处理程序);
- sigaction(int signum, const struct sigaction *act, struct sigaction *oldact); .
These functions require the header signal.hto be included in your C code. I have provide a simple example of the signal
function below with comments.
这些函数要求在 C 代码中包含头文件signal.h。我提供了signal
下面带有注释的函数的简单示例。
#include <stdio.h>
#include <stdlib.h>
#include <signal.h> // our new library
volatile sig_atomic_t flag = 0;
void my_function(int sig){ // can be called asynchronously
flag = 1; // set flag
}
int main(){
// Register signals
signal(SIGINT, my_function);
// ^ ^
// Which-Signal |-- which user defined function registered
while(1)
if(flag){ // my action when signal set it 1
printf("\n Signal caught!\n");
printf("\n default action it not termination!\n");
flag = 0;
}
return 0;
}
Note: you should only call safe/authorized functionsin signal handler. For example avoid calling printf in signal handler.
注意:您应该只在信号处理程序中调用安全/授权的函数。例如避免在信号处理程序中调用 printf。
You can compile this code with gcc and execute it from the shell. There is an infinite loop in the code and it will run until you send a SIGINT
signal by pressing Ctr + C.
您可以使用 gcc 编译此代码并从 shell 执行它。代码中有一个无限循环,它会一直运行,直到您SIGINT
按发送信号Ctr + C。
回答by Carl Norum
Typing CtrlCnormally causes the shell to send SIGINT
to your program. Add a handler for that signal (via signal(2)
or sigaction(2)
), and you can do what you like when CtrlCis pressed.
键入CtrlC通常会导致 shell 发送SIGINT
到您的程序。为该信号添加一个处理程序(通过signal(2)
或sigaction(2)
),您可以在CtrlC按下时执行您喜欢的操作。
Alternately, if you only care about doing cleanup before your program exits, setting up an exit handler via atexit(3)
might be more appropriate.
或者,如果您只关心在程序退出之前进行清理,则设置退出处理程序 viaatexit(3)
可能更合适。
回答by phyrrus9
You can use the signal macro.
您可以使用信号宏。
Here is an example of how to deal with it:
以下是如何处理它的示例:
#include <signal.h>
#include <stdio.h>
void sigint(int a)
{
printf("^C caught\n");
}
int main()
{
signal(SIGINT, sigint);
for (;;) {}
}
Sample output:
示例输出:
Ethans-MacBook-Pro:~ phyrrus9$ ./a.out
^C^C caught
^C^C caught
^C^C caught
^C^C caught
^C^C caught