按Ctrl + C中断事件处理的Linux [英] Ctrl + C interrupt event handling in Linux

查看:1501
本文介绍了按Ctrl + C中断事件处理的Linux的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用C ++和使用Linux的GNU C编译器编译的应用程序。不过,我想调用一个函数作为中断用户使用脚本<大骨节病>控制 <大骨节病> C 键。我该怎么办?任何答案将是非常美联社preciated。

I am developing an application that uses C++ and compiles using Linux GNU C Compiler. However, I want to invoke a function as the user interrupts the script using CtrlC keys. What should I do? Any answers would be much appreciated.

推荐答案

在preSS <大骨节病> CTR + C ,操作系统发送的信号的过程。有许多信号并且将它们中的一个是信号情报。在SIGINT(计划中断)是终止信号之一。

When you press Ctr + C, the operating system sends a signal to the process. There are many signals and one of them is SIGINT. The SIGINT ("program interrupt") is one of the Termination Signals.

有几个种类的终止信号,但关于SIGINT有趣的是,它可以通过你的程序进行处理(捕获)。 SIGINT的默认操作是程序终止。也就是说,如果你的程序没有专门处理这个信号,当你preSS <大骨节病> CTR + C 你的程序终止作为默认操作。

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 + C your program terminates as the default action.

要改变你必须注册才能被捕获的信号的信号的默认操作。要注册在C程序中(至少在POSIX系统)的信号有两个功能

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


  1. 信号(INT Signum的,sighandler_t处理);

  2. 的sigaction(INT Signum的,常量结构sigaction的*行为,
              结构的sigaction * oldact);

  1. signal(int signum, sighandler_t handler);
  2. sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);.

这些功能需要头 signal.h中要包含在C code。我有提供下面的信号函数的简单示例与评论。

These functions require the header signal.h to be included in your C code. I have provide a simple example of the signal function below with comments.

#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;
}  

请注意:你应该只调用安全/授权功能在信号处理程序。例如<一个href=\"http://stackoverflow.com/questions/16891019/how-to-avoid-using-printf-in-a-signal-handler/16891065#16891065\">avoid呼叫printf的信号处理。

Note: you should only call safe/authorized functions in signal handler. For example avoid calling printf in signal handler.

您可以编译这个code。与海湾合作委员会,并从shell执行它。有一个在code无限循环,它会运行,直到由pressing发送 SIGINT 信号<大骨节病> CTR + C

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.

这篇关于按Ctrl + C中断事件处理的Linux的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆