Search code examples
c++spawnwaitpid

Spawned child exits with state = 127


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?


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.