Multithreaded fork
Asked Answered
T

4

26

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?

Thrasonical answered 19/5, 2011 at 10:0 Comment(2)
Have you seen this question? Or this one? Basically only the fork()ing thread survives in the child process. What are you trying to achieve?Vesicle
Actually I was trying to create a replicated process for reliable execution, where the replicated process would verify the outputs from primary process by executing the same code.Thrasonical
H
25

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 ?.

Heyduck answered 19/5, 2011 at 10:6 Comment(0)
S
18

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.

Scorcher answered 19/5, 2011 at 10:5 Comment(0)
E
0

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.

Edva answered 19/5, 2011 at 10:6 Comment(0)
N
0

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

Negativism answered 29/6, 2022 at 21:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.