Somehow it's easier to call fork
and then unshare
because many arguments are copied via fork
that would otherwise be manually wrapped to clone
. My question is, what is the difference between (1) calling clone
which forks a new process in separate namespaces and (2) fork+unshare
which forks a new process and then leaves parent's namespaces. Assume all namespace flags passed to clone
and unshare
are the same.
auto flag = CLONE_NEWUSER | CLONE_NEWUTS | CLONE_NEWIPC | CLONE_NEWPID | CLONE_NEWNS | CLONE_NEWNET | SIGCHLD;
So, with fork, its very easy for child to reuse data inherited from parents.
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);
}
For clone, we sometimes have to wrap data into another struct and pass it as void*
to the clone
system call.
int mydata;
clone(func, stack+stack_size, flag, &wrapper_of_data);
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 fork
saves me many efforts in the way that both can create a process in new namespaces.