防止 Ctrl-C 关闭程序

发布于 2024-12-07 08:25:42 字数 1376 浏览 0 评论 0原文

这是我之前问过的一个问题的延伸。 覆盖 Ctrl-C

我有这段代码正在处理 ctrl-c 的信号,而这有效一台机器上,我在另一台机器上运行它,现在按下 ctrl-c 后它再次退出程序。

当我问我的教授时,他告诉我,在处理程序中,我需要防止信号被进一步处理,但我不知道如何做到这一点。

#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h> 
#include <sys/types.h>
#include <signal.h> 
#include "Input.h"
#include "CircleBuff.h"

sig_atomic_t sigflag = 0;

void catch_int(int sig_num) {   
    sigflag = 1;
    //printf("to do: Print history");
}

void printHistory(CircleBuff hist) {
    cout << "Complete History:\n" << endl;
    hist.print();
    cout << endl;
}

int main(int argc, char** argv) {
    signal(SIGINT, catch_int);

    //my code here

    do {
        //more code here
        if (sigflag != 0) {
            printHistory(history);
            sigflag = 0;            
        }
    } while(report != 0); //which is assigned in the code

这是简化的代码:

#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <signal.h> 

sig_atomic_t sigflag = 0;

void catch_int(int sig_num) {   
    sigflag = 1;
    printf("to do: Print history");
}

int main(int argc, char** argv) {  
    signal(SIGINT, catch_int);
    do {
    } while(1);
}

This is an extension to a question I asked before.
Override Ctrl-C

I have this code that is handling the signal for ctrl-c, while this worked on one machine, I ran it on another and it now once again exits the program after ctrl-c is pressed.

When I asked my professor he told me that in the handler I will need to prevent the signal from being processed further, but I am not sure how to do this.

#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h> 
#include <sys/types.h>
#include <signal.h> 
#include "Input.h"
#include "CircleBuff.h"

sig_atomic_t sigflag = 0;

void catch_int(int sig_num) {   
    sigflag = 1;
    //printf("to do: Print history");
}

void printHistory(CircleBuff hist) {
    cout << "Complete History:\n" << endl;
    hist.print();
    cout << endl;
}

int main(int argc, char** argv) {
    signal(SIGINT, catch_int);

    //my code here

    do {
        //more code here
        if (sigflag != 0) {
            printHistory(history);
            sigflag = 0;            
        }
    } while(report != 0); //which is assigned in the code

Here is the reduced code:

#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <signal.h> 

sig_atomic_t sigflag = 0;

void catch_int(int sig_num) {   
    sigflag = 1;
    printf("to do: Print history");
}

int main(int argc, char** argv) {  
    signal(SIGINT, catch_int);
    do {
    } while(1);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街道布景 2024-12-14 08:25:42

您尝试过signal(SIGINT, SIG_IGN);吗?

我认为这会起作用


编辑:

我回去并稍微减少了你的程序,处理程序似乎可以工作
(每次调用都会增加标志),但实际的系统调用(在我的情况下为睡眠)被中止。

您能否重新测试并重申在这个减少的样本中对您不起作用的内容?

#include <stdio.h>
#include <unistd.h>
#include <signal.h>

volatile sig_atomic_t ctrlCcount = 0;

void catch_int(int sig_num){
  ctrlCcount++;
}

int main(int argc, char** argv){

  signal(SIGINT, catch_int);
  do{
     sleep(5);
     printf("Ctrl-C count=%d\n", ctrlCcount);
   }while(1);
   return 0;
}

Did you try signal(SIGINT, SIG_IGN);?

I assume that will work


Edit:

I went back and reduced your program just a bit and the handler seems to work
(every invokation increases the flag), yet the actual syscall (sleep in mycase) is aborted.

Can you retest and restate what is not working for you in this reduced sample?

#include <stdio.h>
#include <unistd.h>
#include <signal.h>

volatile sig_atomic_t ctrlCcount = 0;

void catch_int(int sig_num){
  ctrlCcount++;
}

int main(int argc, char** argv){

  signal(SIGINT, catch_int);
  do{
     sleep(5);
     printf("Ctrl-C count=%d\n", ctrlCcount);
   }while(1);
   return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文