2

As far as I know if waitpid returns -1 then it is error condition. How it possible to get success (EXIT_SUCCUSS) from child process in WEXITSTATUS(childStatus)?

What is the difference between childStatus in waitpid & return value from WEXITSTATUS(childStatus)? Does it same?

pid_t returnValue = waitpid(Checksum_pid, &childStatus, WNOHANG);
printf("return value = %d", returnValue);
printf("return value = %d", childStatus);

if (WIFEXITED(childStatus))
        {
            printf("Exit Code: _ WEXITSTATUS(childStatus)") ;    
            //Proceed with other calculation.  
        }
kapilddit
  • 1,729
  • 4
  • 26
  • 51

2 Answers2

5

When using the option WNOHANG, I would expect that most of the time waitpid will return -1, with errno set to ECHILD.

In any case, whenever waitpid does return -1, you shouldn't be looking at childStatus, which (for all I know) could be just garbage. Instead, look at errno, and handle it appropriately.

Otherwise, your code seems to be ok, as far as it goes, and you should be able to extract a 0 or EXIT_SUCCESS from childStatus.

The man page for waitpid suggests the following sample code:

   if (WIFEXITED(status)) {
       printf("exited, status=%d\n", WEXITSTATUS(status));
   } else if (WIFSIGNALED(status)) {
       printf("killed by signal %d\n", WTERMSIG(status));
   } else if (WIFSTOPPED(status)) {
       printf("stopped by signal %d\n", WSTOPSIG(status));
   } else if (WIFCONTINUED(status)) {
       printf("continued\n");
   }

although it might be a good idea to add a final else printf("oops?\n") statement to this.

Joseph Quinsey
  • 9,553
  • 10
  • 54
  • 77
  • Is it okay to do waitpid after regular interval to make it succuss? I am getting -1 always... – kapilddit Apr 07 '14 at 08:51
  • I tried above mentioned as you suggested. I am getting -1 return value from waitpid all the time with errorno as ECHILD. but WIFEXITED(status) is returning 1 and WEXITSTATUS(status) returns 0. Can you please suggest me how can proceed in this condition? – kapilddit Apr 07 '14 at 09:13
  • @kapilddit: What happens if you replace `WNOHANG` with `0`? – Joseph Quinsey Apr 07 '14 at 17:26
  • I tried with 0 (instead of WNOHANG) but still problem is same. waitpid returns -1 with error ECHILD. I executed the same code in ubuntu it is giving child pid as return value from waitpid and Exit code as 0. I don't know why I am getting different behavior with same piece of code. – kapilddit Apr 07 '14 at 17:47
  • 1
    @kapilddit: One further suggestion: the man page says "if `SIGCHLD` is set to `SIG_IGN` ... then ... a call to `waitpid()` ... will block until all children have terminated, and then fail with `errno` set to `ECHILD`". – Joseph Quinsey Apr 07 '14 at 18:12
  • I initialize the child status with 0xFFFF & now I am getting print as"continued". What does it mean? – kapilddit Apr 10 '14 at 11:31
  • @kapilddit: To repeat, "whenever `waitpid` does return `-1`, you shouldn't be looking at `childStatus`, which (for all I know) could be just garbage" – Joseph Quinsey Apr 10 '14 at 19:42
2

WIFEXITED will read whatever value is stored in childStatus, which is just an integer, so it doesn't have to come from waitpid() - try eg

for(i = 0; i < 1234; i++)
        printf("WIFEXITED(%d) %s\n", i, WIFEXITED(i) ? "yes" : "no");

The difference between childSTatus and WIFEXITED(childStatus) is a bit tricky... Basically the exit status has been abused to tell the exit status or the signal that killed the process: you'd want something like

struct exitstatus {
        int status;
        int signalnumber;
        enum { exited, signaled };
};

but those informations have been squeezed into a single integer in some way (I'm not sure if the exact details are defined anywhere): eg on my computer the low 8 bits are used for the signal number (if any) and the bits 8-15 are used for the exit code. The important point anywhere is that you don't need to know how it happens, only how to get the results you want via WIFEXITED & friends.

loreb
  • 1,327
  • 1
  • 7
  • 6
  • In what case I will get -1 return value from waitpid? I am getting ECHILD as errono / error code. In waitpid do I need to pass the childpid for which I want to know the status? Only in parent process only I can request the status of child or in other process also I can request the status of the particular pid? – kapilddit Apr 04 '14 at 15:56
  • 1
    The reference for waitpid is at http://pubs.opengroup.org/onlinepubs/9699919799/functions/waitpid.html; waitpid can do both things, ie wait for any process/a specific process; yes, only in the parent process (unless you count reparenting) – loreb Apr 04 '14 at 16:01