0

Good day! I have a process which was created via posix_spawn function. How can I get his exit code?

I know about waitpid() function but it returns status for child process. Also if process still a live and I will call waitpid() function then I will receive right information what this process isn't closed yet.

Here is my current code:

int GetExitCode()
{
  int status;
  int rtn = waitpid(pid, &status, WNOHANG);
  if (rtn > 0) // still live
  {
    return -1;
  }

  rtn = waitpid(pid, &status, WUNTRACED);
  if (rtn != -1 || errno != ECHILD) 
  {
     // Here I got rtn = -1 and errno #10
  }

  if (WIFEXITED(status))
  {
    return EXIT_SUCCESS;
  }

  return EXIT_FAILURE;
}

But how I can check exit status for non-child process? Thanks!

Updated. My new code:

int GetExitCode()
{
      int status = 0;

      int rtn = kill(pid, 0);
      if (rtn == -1 && errno == ESRCH)
      {
          return 0;
      }

      rtn = waitpid(pid, &status, WNOHANG | WUNTRACED | WCONTINUED);

      if (rtn == 0) // still live
      {
        return 0;
      }

      std::cout << "Probably success. Errno: " << errno << ". StrError: " << strerror(errno) << std::endl;
      if (WIFEXITED(status))
      {
        return 1;
      }

      return 0;
}
user922871
  • 435
  • 2
  • 6
  • 17
  • 1
    The `posix_spawn` function returns a process ID just as `fork` does, which means you can use the normal `wait` family of functions. In fact, the created process *is* a child of your process, just like when you create a process with `fork`. – Some programmer dude Sep 17 '14 at 14:10
  • @JoachimPileborg: I think that should be an answer. The whole basis of this question seems to be that OP is confused about what "child process" means. – R.. GitHub STOP HELPING ICE Sep 17 '14 at 14:17
  • If that is true then why waitpid returns Error code 10 "No child processes" ? I expected to see termination status... where I made mistake? Thanks – user922871 Sep 17 '14 at 14:20
  • Can you please add some code to your question to show what you're doing? A [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) would be perfect. – Some programmer dude Sep 17 '14 at 14:22
  • Sorry, first post updated – user922871 Sep 17 '14 at 14:28

2 Answers2

0

Remove the second call to waitpid, the first got the status (you can wait for a process only once).

AProgrammer
  • 51,233
  • 8
  • 91
  • 143
  • So, if I want to check if process still a live I must check it by kill function? As this described in that question? http://stackoverflow.com/questions/1157700/how-to-wait-for-exit-of-non-children-processes – user922871 Sep 17 '14 at 14:33
0

The problem is two-fold: First your check if the process is still alive or not is wrong. From the waitpid manual page about the return value:

if WNOHANG was specified and one or more child(ren) specified by pid exist, but have not yet changed state, then 0 is returned.

Since you check for larger than zero, that means you return -1 when the process has exited.

The second problem is that you call waitpid twice. If the process has exited, then you already "reaped" it with the first call which is why the second call gives you an error, since the child process no longer exists.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621