Multithreaded fork

2020-01-29 08:19发布

Can fork() function be used to replicate a multithreaded process. And if so, will all threads be exactly the same and if not, why not. If replication can't be done through fork, is there any other function which can do it for me?

3条回答
等我变得足够好
2楼-- · 2020-01-29 09:09

No, the child will only have one thread. Forking a threaded process is not trivial. (See this article Threads and fork(): think twice before mixing them for a good rundown).

I don't know of any way of cloning a process and all its threads, I don't think that's possible on Linux.

查看更多
萌系小妹纸
3楼-- · 2020-01-29 09:21

After a fork, only one thread is running in the child. This is a POSIX standard requirement. See the top answer to the question fork and existing threads ?.

查看更多
迷人小祖宗
4楼-- · 2020-01-29 09:21

No.

A fork creates a new process with his own thread(s), copies the file descriptor and the virtual memory.

A child process does NOT share the same memory with his father. So this is absolutely not the same.

查看更多
登录 后发表回答