Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clone-equivalent of fork?

Tags:

c

linux

fork

I'd like to use the namespacing features of the clone 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.

like image 595
Stéphan Kochen Avatar asked May 24 '10 16:05

Stéphan Kochen


People also ask

Is fork the same as clone?

A fork creates a completely independent copy of Git repository. In contrast to a fork, a Git clone creates a linked copy that will continue to synchronize with the target repository.

Does clone call fork?

5. clone() The clone() system call is an upgraded version of the fork call. It's powerful since it creates a child process and provides more precise control over the data shared between the parent and child processes.

Should I fork or clone a repo?

If you don't intend to make changes to code, clone but don't fork. Forking is intended to host the commits you make to code, while cloning is perfectly fine for copying the content and history of the project.


1 Answers

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.

like image 142
nategoose Avatar answered Oct 06 '22 20:10

nategoose