0

fork() forks a process, but it will not copy all of its threads; nor will it create a clone of all its fds: the fds are shared between parent and child.

I want a way of do a complete fork. So after I fork, the new process has the same process and threads as the old process, and all fds are the exact copy, for example normal files are exact copy (I hope even the file paths are the same, so if we want two processes to have two different files that are of the same path, they must be in different namespaces, right?), server sockets are of the same copy (two processes listening on the same port while no conflict happens must be in different namespaces (I only know docker)), and other fds are as seemless as possible. I also hope that the forking is copy-on-write, so both copying of file blocks and copying on memory are done only when writing on the page happens (considering a process have mmaped the whole address space (java does that!) and opened a 100G raw disk file (fdisk?), the demand makes sense); Also the new process forked should be able to be forked again.

As far as my narrow knowledge of linux, a complete copy of a process can only be possible (any operations of any process with its own fds will not interfere with the other), for example in different dockers.

Anyone has some ideas, or some implementations already exists? Currently I am thinking about making hack in libc to intercept syscalls, which is too hacky... alternatively one can use ptrace to intercept syscalls although ptrace seems to be slow on program syscalling much.

I guess the question does not have a simple answer as there are hundreds of various linux syscalls, and just considering a program calling open, mmap, lseek, fcntl, socket, setsockopt, ioctl, clone, shmat, ``..., it does not seem to be an easy task.

EDIT: the new process should have the same pids to avoid something breaking, so a docker, or pid namespaces are needed as well.

  • 1
    What is the end goal? That is, what are you trying to accomplish? – Fox Feb 21 '18 at 2:11
  • You might be interested in criu – Patrick Feb 21 '18 at 2:29
  • @Patrick Thank you! I am looking at it. It seems very promising. – Thiner Feb 21 '18 at 3:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.