title: using exec to execute a system command in a new process
body: <p>I am trying to spawn a process that executes a system command, while my own program still proceeds and two processes will run in parallel. I am working on linux. </p>
  
  <p>I looked up online and sounds like I should use exec() family. But it doesn't work quite as what I expected. For example, in the following code, I only see "before" being printed, ,but not "done". </p>
  
  <p>I am curious if I am issing anything? </p>
  
  <pre><code>#include &lt;unistd.h&gt;
  #include &lt;iostream&gt;
  
  using namespace std;
  
  main()
  {
     cout &lt;&lt; "before" &lt;&lt; endl;
     execl("/bin/ls", "/bin/ls", "-r", "-t", "-l", (char *) 0);
     cout &lt;&lt; "done" &lt;&lt; endl;
  }
  </code></pre>
  
  <p>[UPDATE]</p>
  
  <p>Thank you for your guys comments. Now my program looks like this. Everything works fine except at the end, I have to press enter to finish the program. I am not sure why I have to press the last enter? </p>
  
  <pre><code>#include &lt;unistd.h&gt;
  #include &lt;iostream&gt;
  
  using namespace std;
  
  main()
  {
     cout &lt;&lt; "before" &lt;&lt; endl;
     int pid = fork();
     cout &lt;&lt; pid &lt;&lt; endl;
     if (pid==0) {
        execl("/bin/ls", "ls", "-r", "-t", "-l", (char *) 0);
     }
     cout &lt;&lt; "done" &lt;&lt; endl;
  }
  </code></pre>