title: How to properly fork() a process
body: <p>I'm trying to understand how to properly used <a href="http://linux.die.net/man/2/fork" rel="nofollow">fork()</a> and <a href="http://linux.die.net/man/3/execvp" rel="nofollow">execvp()</a> to execute a command. So far I have the following code:</p>
  
  <p>When I run <code>./test vim myFile.c</code> it correctly opens <code>myFile.c</code> but I get strange behavior. It seems as though there are two processes running because whenever I enter anything it seems to happen twice. Why is this?</p>
  
  <pre><code>int main (int argc, char* argv[]) {
     int fdin, pid, w, status; 
     fdin = 0;                                                                   
     if ((pid = fork()) &lt; 0)                                                     
         errorExit (EXIT_FAILURE);                                                                                                                              
     execvp(argv[0],argv);  
  
     do {
        w = waitpid(cpid, &amp;status, WUNTRACED | WCONTINUED);
        if (w == -1) {
            perror("waitpid");
            exit(EXIT_FAILURE);
        }
  
       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");
        }
    } while (!WIFEXITED(status) &amp;&amp; !WIFSIGNALED(status))
  }
  </code></pre>