Question

I use posix_spawnp to execute different processes and I check the status (with waitpid) to make sure the child was created properly

    int iRet = posix_spawnp(&iPID, zPath, NULL, NULL, argv, environ);       

    if (iRet != 0)
    {
        return false;
    }

    int iState;
    waitpid(static_cast<pid_t>(iPID), &iState, WNOHANG);
    cout << "Wait: PID " << iPID << " | State " << iState << endl;

    if (WIFEXITED(iState)) {
        printf("Child exited with RC=%d\n",WEXITSTATUS(iState));
    }
    else if (WIFSIGNALED(iState)) {
        printf("Child exited via signal %d\n",WTERMSIG(iState));
    }
    else
    {
        printf("Child is NORMAL");
    }

At first this executes properly and I get the following message:

Wait: PID 15911 | State 0 Child exited with RC=0

After executing the same process several times, the child process starts to exit with status 127.

Wait: PID 15947 | State 32512 Child exited with RC=127

After this happens, I could not get the child to spawn again. I enclosed the section of code given above in a for loop but it wouldn't spawn properly. If I restart the parent process, it works for a while but the same problem crops up again after a while.

What am I doing wrong here?

Was it helpful?

Solution

Check this link.

For example:

EINVAL The value specified by file_actions or attrp is invalid.

The error codes for the posix_spawn and posix_spawnp subroutines are affected by the following conditions: If this error occurs after the calling process successfully returns from the posix_spawn or posix_spawnp function, the child process might exit with exit status 127.

It looks as if it might exit with 127 for a whole host of reasons.

OTHER TIPS

Check the return code from waitpid() to be sure that it isn't having problems.

The way the code reads suggests that you are only spawning one child process at a time (otherwise there'd be no need to call waitpid() within the loop). However in that case I wouldn't expect to use WNOHANG.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top