Home > Net >  Why doensn't Ctrl C doesn't work in my terminal?
Why doensn't Ctrl C doesn't work in my terminal?

Time:11-05

I've the following problem:

I'm creating two threads in main. These threads never stop (they're always listening for messages).

So, in main, after creating this two threads, I've this snippet code to keep the program running:

for (;;)
{
    sleep(10);
}
return 0;

It works, but the problem is that when I execute the program, in the terminal, Ctrl C doesn't allow me to exit from the program. I've to exit with Ctrl \. I've this function (not made by me). Where is the problem?

void app_signal_handler(int sig_num)
{
    if (sig_num == SIGINT) {
        printf("SIGINT signal!\n");
    }
    if (sig_num == SIGTERM) {
        printf("SIGTERM signal!\n");
    }
    app_running = false;
}

char app_sigaltstack[SIGSTKSZ];
int app_setup_signals(void)
{
    stack_t sigstack;
    struct sigaction sa;
    int ret = -1;

    sigstack.ss_sp = app_sigaltstack;
    sigstack.ss_size = SIGSTKSZ;
    sigstack.ss_flags = 0;
    if (sigaltstack(&sigstack, NULL) == -1) {
        perror("signalstack()");
        goto END;
    }

    sa.sa_handler = app_signal_handler;
    sa.sa_flags = SA_ONSTACK;
    if (sigaction(SIGINT, &sa, 0) != 0) {
        perror("sigaction()");
        goto END;
    }
    if (sigaction(SIGTERM, &sa, 0) != 0) {
        perror("sigaction()");
        goto END;
    }

    ret = 0;
END:
    return ret;
}

CodePudding user response:

You never use app_running. Replace

for (;;)
{
    sleep(10);
}

with

while (app_running)
{
    sleep(10);
}
  • Related