In the below code, when system function get executed (in Linux and compiled with g++), it will send the SIGCHLD signal to process (from which it is called) which doesn’t happen in Solaris code. I mean, if I run the same code in Solaris, the system function doesn’t send any signal to the process from which it is called. As a result, my process in Linux hangs.
#include <iostream>
using namespace std;
#include <cstdlib>
#include <signal.h>
void sigHandler(int sgnNbr)
{
cout <<"Signal " << sgnNbr << "caught" << endl;
system("echo $HOSTNAME");
exit(1);
}
int main()
{
signal(SIGCHLD,sigHandler);
system("echo $HOSTNAME");
return 0;
}
My questions are:
- Why does the process hang in the above case?
- why does the system function not send any signal in Solaris?
… and my last question, which is related to my real application:
Actually, in my real application (in which single signal handler is there for some four signals), some other process sending the SIGABRT signal to my process and in signal handler I am killing the one process using system function as a result it got hanged(which is similar to the above scenario). Is there any way to overcome this?
From http://pubs.opengroup.org/onlinepubs/009604499/functions/system.html:
Solaris is correct; Linux is incorrect.
The most likely reason that your program is hanging is that it is not safe to call
systemwithin a signal handler. See http://pubs.opengroup.org/onlinepubs/009604499/functions/xsh_chap02_04.html#tag_02_04 for the list of async-signal-safe functions.To kill another process, you can use
kill, which is async-signal-safe.