Skip to content
Advertisement

What’s the intention of waitpid

The sample code below is from linux man page waitpid function. Can the last else if be replaced with else? When I write code, I would write like:if, else if, and end with else. So I think it is strange in the sample code.

   #include <sys/wait.h>
   #include <stdlib.h>
   #include <unistd.h>
   #include <stdio.h>

   int
   main(int argc, char *argv[])
   {
       pid_t cpid, w;
       int status;

       cpid = fork();
       if (cpid == -1) {
           perror("fork");
           exit(EXIT_FAILURE);
       }

       if (cpid == 0) {            /* Code executed by child */
           printf("Child PID is %ldn", (long) getpid());
           if (argc == 1)
               pause();                    /* Wait for signals */
           _exit(atoi(argv[1]));

       } else {                    /* Code executed by parent */
           do {
               w = waitpid(cpid, &status, WUNTRACED | WCONTINUED);
               if (w == -1) {
                   perror("waitpid");
                   exit(EXIT_FAILURE);
               }

               if (WIFEXITED(status)) {
                   printf("exited, status=%dn", WEXITSTATUS(status));
               } else if (WIFSIGNALED(status)) {
                   printf("killed by signal %dn", WTERMSIG(status));
               } else if (WIFSTOPPED(status)) {
                   printf("stopped by signal %dn", WSTOPSIG(status));
               } else if (WIFCONTINUED(status)) { /* can this be 
                                                   *replaced with else ???
                                                   */
                   printf("continuedn");
               }
           } while (!WIFEXITED(status) && !WIFSIGNALED(status));
           exit(EXIT_SUCCESS);
       }
   }

Advertisement

Answer

Here’s the question I think you are asking:

Is it possible for waitpid to return a status such that none of WIFEXITED(status), WIFSIGNALED(status), WIFSTOPPED(status), or WIFCONTINUED(status) returns nonzero?

The answer is almost certainly “no”, and the Linux man page implies as much without (unfortunately) explicitly saying it.

The UNIX standard says more here, and specifically does guarantee that one of those macros will return nonzero in certain cases. But Linux is not (necessarily) compliant to this standard.

But I think the best solution in code would be to have an else clause after those four options, and (depending on your application) take this to mean that the status is unknown. This could happen even if it’s not the case now – maybe in some future version of Linux there is another kind of exit condition not covered here, and you don’t want your code to crash in such a case.

The standard does seem to have evolved over time. For example, earlier versions didn’t have the WIFCONTINUED case, and I also found some references online to a WIFCORED in some other systems. So it would be good to make your code flexible if you’re concerned about it.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement