Clone-equivalent of fork?
I'd like to use the namespacing features of the cl开发者_JS百科one
function. Reading the manpage, it seems like clone
has lots of intricate details I need to worry about.
Is there an equivalent clone
invocation to good ol' fork()
?
I'm already familiar with fork
, and believe that if I have a starting point in clone
, I can add flags and options from there.
I think that this will work, but I'm not entirely certain about some of the pointer arguments.
pid_t child = clone( child_f, child_stack,
/* int flags */ SIGCHLD,
/* argument to child_f */ NULL,
/* pid_t *pid */ NULL,
/* struct usr_desc * tls */ NULL,
/* pid_t *ctid */ NULL );
In the flags parameter the lower byte of it is used to specify which signal to send to notify the parent of the thread doing things like dying or stopping. I believe that all of the actual flags turn on switches which are different from fork
. Looking at the kernel code suggests this is the case.
If you really want to get something close to fork
you may want to call sys_clone
which does not take function pointer and instead returns twice like fork
.
You could fork a normal child process using fork(), then use unshare() to create a new namespace.
Namespaces are a bit weird, I can't see a lot of use-cases for them.
clone() is used to create a thread. The big difference between clone() and fork() is that clone() is meant to execute starting at a separate entry point - a function, whereas fork() just continues on down from the same point in the code from where was invoked. int (*fn)(void *) in the manpage definition is the function, which on exit returns an int, the exit status.
The closest call to clone is pthread_create() which is essentially a wrapper for clone(). This does not get you a way to get fork() behavior.
精彩评论