"No child processes" and waitpif

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?

system() needs SIGCHLD since it creates and waits for processes to finish. You may wish to make your own implementation of it that is aware of your custom handlers, etc.

Hmm i'm sorry....what do you mean? Can you make an example?

Another question:i have read somewhere that it's better not to call certain functions from within a signal handler function.What are these functions?

Thanks again :slight_smile:

Since signal handlers can be interrupted (bumped to the interrupt stack from where it is)
you have to call system I/O functions (as examples: read, write) and not functions in the STDC library like fgets and fputs. This is because a system is generally unaffacted by most signals - although read and write do respond to SIGINT for example. You have to read the man page for any function you want to use. First.

So, system functions (API) are usually a safer bet. You should also read the sigvector man page for your system, man 2 signal page as well because some implmentations of these calls may cause problems for write() calls -- as an example.

Any function that is async-signal-safe is okay. Thread safe is not the same thing. Anything async-signal-safe has to be thread-safe as well. But the reverse is not always true.

async-signal-safe function calls
http://www.opengroup.org/onlinepubs/009695399/functions/xsh\_chap02\_04.html\#tag\_02\_04_04

Thanks a lot for your reply jim :slight_smile:

I couldn't work on the code in these days,so now it's time to face the problem :slight_smile:

After reading your reply and some internet resource i am pretty sure that i am not following the right approach.Though,i am not expert in unix programming,so i ask for some further advice....

I attach some code:

1) I install my signal handler with

void InstallNewSigChldHandler()
{        
    struct sigaction act;
    struct sigaction oldAct;
    extern MainWindow * _mainWindow;
    
    /* Assign sig_chld as our SIGCHLD handler */
    act.sa_handler = CallWaitChildProcess;
    act.sa_flags = SA_NOCLDSTOP;

    if (sigaction(SIGCHLD, &act, &oldAct) < 0) 
    {
        traceLog("%s.%d:ERROR INSTALLING SIGCHLD HANDLER:sigaction FAILED",__SNFILE__, __LINE__);
        return;
    }
    _mainWindow->SetSigChldInstalled(true);    
}

The handler function is:

void CallWaitChildProcess(int signo) 
{          
    int status, child_val, errStatus;
    extern MainWindow * _mainWindow;


    pid_t retPid;
    retPid=waitpid(g_ChildProcessPid, &status, WNOHANG);
    errStatus=errno;
    if (retPid < 0) 
    {
        traceLog("waitpid failed.Error cause:%s",strerror(errStatus));
        return;
    }

    if(retPid==0)
    {
        traceLog("waitpid returned 0");
        return;
    }


    if (WIFEXITED(status))                /* did child exit normally? */
    {
        child_val = WEXITSTATUS(status); /* get child's exit status */
        traceLog("process pid:%d --> ended normally with value %d\n", g_ChildProcessPid,child_val);
        
       if(true==_mainWindow->GetRecordingON())
       {
           _mainWindow->OnRecorderFinished(child_val);
           return;
       }
    }
}

The traceLog uses va_start,vsprintf and va_end functions,while in the OnRecorderFinished i call some Xt functions and send data to a pipe using the write system call.

Besides,i have read Motif Programming Manual (Volume 6A): 26 - Signal Handling,which explains how to safely handle signals in a motif application by adding a safe handler (i am working in X11 R7.1.1,so fortunately i can use the simpler X11R6 approach).

Now i ask:
1)I am thinking of moving all the "dirty" stuff to this safe handler function (with dirty i mean everything that's not allowed in a handler function).Do you think that's ok?

2)What do you think of the traceLog function?Can it be called from the signal handler?

Thanks again for your precious help!

I don't see anything special about that code at all. It's just setting up a signal handler, not going about it in any special "safe" way. I think they mean motif-compatible, not 'safe' in any general sense.

Is it safe to do non-signal-safe things in a signal handler? No.

Depends what it does and how it does it.