title: execvp() - exit() return value in function
body: <p>I have been fighting with the following concept lately and I am still unable to understand it. I have this block of code</p>
  <pre><code>int foo(int f){
      int fail = 10;  //some random initialization value
      int status;
      pid_t child_pid;
      child_pid = fork();
  
      if(child_pid == 0) {
          /* This is done by the child process. */
  
          if (execvp(tokens2[0], tokens2)&lt;0){
              fail =1;
              perror(tokens2[0]); 
              exit(1);
          }
      }else if(child_pid&lt;0){
          perror(&quot;Fork failed&quot;);
          exit(1);
      } else {
          waitpid(child_pid, &amp;status, WUNTRACED);
      }
  
      //handle execvp error return value
      if (fail == 1){
          return -1;
      }else{
          return 0;
      }
      
  }
  </code></pre>
  <p>It does not make a lot of sense (don't mind <code>tokens2[]</code> array, it is declared properly and it works just fine) because it is on an abstract form in order for it to be audible and easy to understand. The question is: what is the output of function <code>foo</code>? This is what I have come to get so far:</p>
  <blockquote>
  <p>1)If <code>execvp</code> succeeds, it does not return any value. If it fails it returns <code>-1</code> (I think this return value does not have to do anything with my <code>foo</code> function).</p>
  <p>2)<code>exit(1)</code> kills the child process and returns the control flow to the parent.</p>
  </blockquote>
  <p>In my understanding, if <code>execvp</code> succeeds, the <code>foo</code> should return <code>-1</code>(according to variable <code>fail</code>), if it fails it should return <code>0</code>, but that is not what my <code>printf</code> checks on the main function show. Actually the outcome is always the opposite of what I expect.</p>
  <p>PS: I found something about <code>execvp</code> error handling using the good old <em>shelf-pipe</em> trick but it is too noisy for me. I want a simpler way to handle the error.</p>