多线程分叉
fork() 函数可以用于复制多线程进程吗?如果是这样,所有线程都会完全相同,如果不是,为什么不呢?如果无法通过 fork 完成复制,是否有其他函数可以为我完成复制?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
分叉后,子进程中只有一个线程在运行。这是 POSIX 标准要求。请参阅问题的最佳答案分叉和现有线程?。
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 ?.
不,孩子只会有一个线程。分叉线程进程并非易事。 (请参阅这篇文章线程和 fork():在混合它们之前三思而后行以获得良好的概述)。
我不知道有什么方法可以克隆进程及其所有线程,我认为这在 Linux 上是不可能的。
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.
不会
。fork 使用自己的线程创建一个新进程,复制文件描述符和虚拟内存。
子进程不与其父亲共享相同的内存。所以这绝对不一样。
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.
假设其他线程之一(除了执行 fork() 的线程之外的任何线程)负责从您的支票帐户中扣除资金。
POSIX 定义了存在线程时 fork() 的行为,以仅传播分叉线程。
如果另一个线程锁定了互斥锁,则该互斥锁将在子进程中被锁定,但锁的所有者将不存在来解锁它。因此,受锁保护的资源将永久不可用。
http://www.doublersolutions.com/docs/ dce/osfdocs/htmls/develop/appdev/Appde193.htm
Suppose that one of the other threads (any thread other than the one doing the fork( )) has the job of deducting money from your checking account.
POSIX defined the behavior of fork( ) in the presence of threads to propagate only the forking thread.
If the other thread has a mutex locked, the mutex will be locked in the child process, but the lock owner will not exist to unlock it. Therefore, the resource protected by the lock will be permanently unavailable.
http://www.doublersolutions.com/docs/dce/osfdocs/htmls/develop/appdev/Appde193.htm