1

I want to send SIGINT to all processes by name so I am using killall -SIGINT which was working fine. Now I introduced system() call in my c code to run a shell script or shell command which take around 10 seconds to complete. I see that in this case signal handler is not called when I send signal.

When I remove or run it in background that system call it again start working.

Please anyone can suggest me to manage it.

closed as unclear what you're asking by G-Man, GAD3R, Tomasz, Rui F Ribeiro, Eric Renouf Dec 27 '16 at 13:00

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Without seeing the actual code it is hard to come with suggestions. – maulinglawns Dec 27 '16 at 7:33
  • Sample code as follow – Awadhesh Maurya Dec 27 '16 at 11:07
3

from the man page of system(3) -

system()  executes a command specified in command by calling /bin/sh -c command, and returns after the command has been completed.  During execution of the
       command, SIGCHLD will be blocked, and SIGINT and SIGQUIT will be ignored.

Even though the signal is sent to all processes (specified by name), the parent here ignores SIGINT during the system() call. However, it should respond once the call is completed (during sleep() in your case) - have you tried increasing the sleep window ?

  • Thanks I have tested with signal SIGUSR1 its working fine and process is not ignoring while executing system call. – Awadhesh Maurya Dec 27 '16 at 12:23
0
#include<stdio.h>
#include <signal.h>
 void signalHandler(int);
int main()
{
    struct sigaction sa;
    sa.sa_flags = 0;
    // Setup the signal handler
    sa.sa_handler = signalHandler;
    // Block every signal during the handler
    sigfillset(&sa.sa_mask);  
    if (sigaction(SIGINT, &sa, NULL) == -1) {
    printf("Error: cannot handle SIGINT\n"); 
}

    while(1)
    {
        printf("Start to ping google.com");
        system("ping www.google.com -c 1");
        printf("Stop to ping google.com\n");
        sleep(1);
    }

}

void signalHandler(int signal)
{
    printf("signalHandler: signal = %d\n", signal); 
}
  • As you see I just try to ping www.google.com for once it is not printing CsignalHandler: signal = 2 as in signalHandler function. If I comment line of system() function it start receiving signal by killall -SIGINT sigint – Awadhesh Maurya Dec 27 '16 at 11:17
  • I cannot put my code on comment so put it as answer as its size too long to fit in comment – Awadhesh Maurya Dec 27 '16 at 11:48
  • You should edit your question to include the code, this isn't an answer and shouldn't go in a comment even if you could put it in one. – Eric Renouf Dec 27 '16 at 12:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.