Hi everybody,
i'm using a signal handler for the SIGCHLD signal.
void InstallNewSigChldHandler()
{
struct sigaction act;
struct sigaction oldAct;
act.sa_handler = CallWaitChildProcess;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_NOCLDSTOP;
if (sigaction(SIGCHLD, &act, &oldAct) < 0)
{
cout<<"error in sigaction";
}
}
The actual signal handler code is:
void CallWaitChildProcess(int signo)
{
int status, child_val, errStatus;
pid_t retPid;
retPid=waitpid(g_ChildProcessPid, &status, WNOHANG);
errStatus=errno;
if (retPid < 0)
{
printf("waitpid failed.Error cause:%s",strerror(errStatus));
return;
}
if(retPid==0)
{
printf("waitpid returned 0");
return;
}
if (WIFEXITED(status)) /* did child exit normally? */
{
child_val = WEXITSTATUS(status); /* get child's exit status */
printf("process pid:%d --> terminated normally with status %d\n", g_ChildProcessPid,child_val);
}
}
I'm doing this cause i want to run two external programs from my main application,and catch their return code.So,i have the g_ChildProcessPid variable which holds the pid for the external program.
I expected that every other child would cause waitpid to return 0,since i specify the WNOHANG flag,but in my code i have some calls to the "system" function that cause a "No child processes" error.
For example,in a function i do:
sprintf(command,"rm %s/%s",getenv("LOG_DIR"),"test.log");
system(command);
This causes waitpid to fail,with the following error:
waitpid failed.Error cause:No child processes
Why?