2

So, basically, all I want my program to do is have my program stop whatever it is running when control C is doing (aka, kill the child process), while still running the main parent process. I'm attempting to do this with sigaction below:

void handler(int signum)
{
    kill(pidKillTracker, SIGINT);
    if(signum== SIGINT)
    {
            printf("\nChild PID is terminated: %s\n", pidKillTracker);
            fflush(stdout);
    }
}

int main(int argc, char ** argv)
{
    struct sigaction sa;
    memset(&sa, 0, sizeof(sa));
    sa.sa_handler= handler;
    //signal(SIGINT, SIG_IGN);
    if(sigaction(SIGINT, &sa, NULL)==-1)
    {
            printf("SIGACTION");
            fflush(stdout);
    }
//REST OF CODE BELOW...

Basically, I am making sigaction struct called sa, setting handler to handler and trying to send the thing to the handler if CTRL-C is hit while the program is running.

Then, once it is in the handler, it kills off the child process, which the ID is stored in pidKillTracker.

YET, when I actually do this in my program, my program does a segmentation fault(core dumped).

I appear to be following everything the tutorials I look up says to to do, but I am not clear what is going on. I attempted to memset sa, because I read somewhere that can prevent this issue, but it does nothing.

Even if I comment out "kill(pidKillTracker, SIGINT)", the program still segmentation faults.

Below is an example of me calling sleep 5 in my program and then hitting ctrl-c. The same issue occurs:

-bash-4.1$ programrun
:sleep 5
^C
Segmentation fault (core dumped)
-bash-4.1$

I would appreciate any guidance as I have read as much as I can on this, and seem to be getting no where at this point.

EDIT, here is what I did without the printf and still get segmentation fault:

void handler(int signum)
{
    //kill(pidKillTracker, SIGINT);
    if(signum== SIGINT)
    {
            kill(pidKillTracker, SIGINT);
            sigINTTracker=1;
    }
}

int main(int argc, char ** argv)
{
    struct sigaction sa;
    memset(&sa, 0, sizeof(sa));
    sa.sa_handler= handler;
    //signal(SIGINT, SIG_IGN);
    if(sigaction(SIGINT, &sa, NULL)==-1)
    {
            printf("SIGACTION");
            fflush(stdout);
    }
//REST OF CODE BELOW...
user7977797
  • 73
  • 1
  • 1
  • 10
  • 2
    You shouldn't call `printf()` inside a signal handler. You can only call functions that are documented to be async-signal safe. – Barmar May 26 '17 at 01:36
  • Try running under [valgrind](http://valgrind.org). There's a possibility you are reading/writing memory you shouldn't be, and if so this tool will tell you. – dbush May 26 '17 at 01:40
  • So, I removed that printf(), however it basically segfaults if ANYTHING is in there. If it is empty, it will run. Fixed in edit. – user7977797 May 26 '17 at 01:43
  • yeah, generally speaking, you shouldn't do any more than set a flag – ikegami May 26 '17 at 03:47
  • There are a number of functions you can call safely under POSIX rules — see [What is the difference between `sigaction()` and `signal()`?](https://stackoverflow.com/questions/231912/what-is-the-difference-between-sigaction-and-signal) and more particularly [How to avoid calling `printf()` in a signal handler?](http://stackoverflow.com/questions/16891019/) The `kill()` function is one of those that it is safe to call under POSIX rules. Standard C rules are very restrictive indeed; they are catering to a lowest common denominator. – Jonathan Leffler May 26 '17 at 04:40
  • 1
    You need to produce an MCVE ([MCVE]) that reproduces your problem on your machine. We really can't guess what you're doing in the code we can't see that is probably what is causing the crash. – Jonathan Leffler May 26 '17 at 04:42

0 Answers0