title: Difference between clone and fork+unshare
body: <p>Somehow it's easier to call <code>fork</code> and then <code>unshare</code> because many arguments are copied via <code>fork</code> that would otherwise be manually wrapped to <code>clone</code>. My question is, what is the difference between (1) calling <code>clone</code> which forks a new process in separate namespaces and (2) <code>fork+unshare</code> which forks a new process and then leaves parent's namespaces. Assume all namespace flags passed to <code>clone</code> and <code>unshare</code> are the same.</p>
  
  <pre><code>auto flag = CLONE_NEWUSER | CLONE_NEWUTS | CLONE_NEWIPC | CLONE_NEWPID | CLONE_NEWNS | CLONE_NEWNET | SIGCHLD;
  </code></pre>
  
  <p>So, with fork, its very easy for child to reuse data inherited from parents.</p>
  
  <pre><code>int mydata;  // this could be even more complicated, class, struct, etc.
  auto pid = fork();
  if(pid == 0) {
    // reuse inherited data in a copy-on-write manner
    unshare(flag);
  }
  </code></pre>
  
  <p>For clone, we sometimes have to wrap data into another struct and pass it as <code>void*</code> to the <code>clone</code> system call. </p>
  
  <pre><code>int mydata;
  clone(func, stack+stack_size, flag, &amp;wrapper_of_data);
  </code></pre>
  
  <p>It seems to me in the above example the only difference is the performance hit, where fork can be a bit more expensive. However, the nature of <code>fork</code> saves me many efforts in the way that both can create a process in new namespaces. </p>