0

I'm trying to make a C program that can continue running also after a CTRL+C.

I wrote this:

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

void acceptCommands();

void sighandle_int(int sign)
{
    //system("^C;./a.out");   *out:* ^Csh: ^C: command not found

// how to protect here the app from being killed?

}

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

    acceptCommands();   

    return 0;
}

how can i do? Thank you

  • what is your intended functionality of the line `system("^C");`? – Christian Gibbons Apr 17 '18 at 17:12
  • that line is only for test, the purpose was to execute CTRL+C and after open again the application – Davide Ambrosi Apr 17 '18 at 17:18
  • 1
    In a Unix-like environment, job control commands like control-c are interpreted by the shell; they are not commands you can pass directly to system(), so "execute CTRL-C" doesn't really make sense in that context. – Jim Lewis Apr 17 '18 at 17:24
  • That doesn't really make sense. This is a function to handle SIGINT, and you want it to send a SIGINT? If you did manage to send a SIGINT out it would be handled by your handler function and you could be stuck in an eternal loop of handling SIGINT. If you just want your program to keep running, you don't need to really do anything in your handler. By handling SIGINT, it overrides the default action of killing the process. – Christian Gibbons Apr 17 '18 at 17:24
  • oh yeah I didn't realize that, thanks. Let me modify the question – Davide Ambrosi Apr 17 '18 at 17:26

1 Answers1

0

I'm trying to make a C program that can continue running also after a CTRL+C. ? when the process receives CTRL+C you set the handler for that using sigaction() and in that handler you can specify whether to continue or ignore or whatever you want.

May be you want like this

void sighandle_int(int sign) {
            /*when process receives SIGINT this isr will be called,
            here you can specify whether you want to continue or ignore,
            by signal handler again */
            signal(SIGINT,SIG_IGN);
            //or
            signal(SIGINT,sighandle_int);
    }

Meanwhile use sigaction() instead of signal() as told here What is the difference between sigaction and signal?

Achal
  • 11,821
  • 2
  • 15
  • 37