Friday, April 29, 2011

Spawned child exits with state = 127

Hi, 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));
  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?

From stackoverflow
  • 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.

    Gayan : The waitpid call returns with a value > 0 which means that there's a valid child.
  • 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.

    Gayan : I re-wrote the code using fork and execvp to get a more definite grasp of the error and it turned out that the actual error info is: errno = 14 (bad address) Some digging around revealed that this was because I wasn't ending my argument list with a final entry of "NULL". argv = new char[iSize + 1]; argv[iSize] = NULL; fixed the problem.


Post a Comment