title: wait(status), WEXITSTATUS(status) always returns 0
body: <p>I'm working on a c program that uses fork() exec() wait().
  The first process has this code:</p>
  
  <pre><code>int main(int argc, const char * argv[]) {
  // insert code here...
  
  int pid = fork();
  
  if(pid == 0){
      //we are the child process.
      execlp("/Users/matthewdunn/Library/Developer/Xcode/DerivedData/PA2-hekivoaqvsegnggdvcfguasystig/Build/Products/Debug/Checker", "Checker", "1", "5", (char*) NULL);
  }else{
      //we are the parent process.
      printf("Coordinator: forked process with ID ");
      printf("%d\n", pid);
  
      printf("Coordinator: waiting for process [");
      printf("%d", pid);
      printf("]\n");
  
      int status;
      waitpid(pid, &amp;status, 0);
      int returned = WEXITSTATUS(status);
  
      printf("Coordinator: child process ");
      printf("%d", pid);
      printf(" returned ");
      printf("%d", returned);
      printf(".\n");
  }
  
  
  
    return 0;
  }
  </code></pre>
  
  <p>The second process has the code: </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;unistd.h&gt;
  
  void printProcessInfo(){
      printf("Checker process [");
      printf("%d", getpid());
      printf("]:  ");
  }
  
  int main(int argc, const char * argv[]) {
      // insert code here...
  
      printProcessInfo();
      printf("Starting.\n");
  
      int arg1;
      int arg2;
  
      arg1 = atoi(argv[1]);
      arg2 = atoi(argv[2]);
  
      if((arg2 % arg1) &gt; 0){
          printProcessInfo();
          printf("%d", arg2);
          printf(" *IS NOT* divisible by ");
          printf("%d\n", arg1);
  
          printProcessInfo();
          printf("Returning 0.\n");
          return 0;
      }
      else{
          printProcessInfo();
          printf("%d", arg2);
          printf(" *IS* divisible by ");
          printf("%d\n", arg1);
  
          printProcessInfo();
          printf("Returning 1.\n");
          return 1;
      }
  
  }
  </code></pre>
  
  <p>For a reason that I cannot figure out, the parent function always reads a return value of 0, even though I get the falling output:</p>
  
  <pre><code>Coordinator: forked process with ID 2865 
  Coordinator: waiting for process [2865] 
  Checker process [2865]:  Starting. 
  Checker process [2865]:  5 *IS* divisible by 1 
  Checker process [2865]:  Returning 1.
  Coordinator: child process 2865 returned 0. 
  Program ended with exit code: 0
  </code></pre>
  
  <p>The second process is clearly returning 1, yet I'm still getting a 0 for the status. I have no idea what could be causing this. I'm writing this code using Xcode. Is there a reason why WEXITSTATUS(status) always returns 0?</p>