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...