what happens if
Erez D
erez0001 at gmail.com
Mon Mar 19 12:13:23 IST 2012
On Mon, Mar 19, 2012 at 11:40 AM, Nadav Har'El <nyh at math.technion.ac.il>wrote:
> On Mon, Mar 19, 2012, Erez D wrote about "what happens if":
> > what happens if i am running a multithreaded app ( 3 threads ) and one
> > thread calls fork() ?
>
> On Linux, the new process will run ONLY a copy of the thread doing the
> fork(). The other threads are *not* copied to the child process.
>
> There is no way in Linux to ask to duplicate all of the process's
> threads, as in Solaris's forkall(2) system call. Even if such a
> system call existed, its usefulness would be dubious as explained
> in Posix's fork(2) manual:
> http://pubs.opengroup.org/onlinepubs/009695399/functions/fork.html
> (look for the section starting in "The addition of the forkall()
> function to the standard was considered and rejected.")
>
> All this makes fork() more or less useless for multithreaded
> applications except for one idiom: fork() followed by exec().
>
my reason for fork is only for exec() - to protect the first program from
the other.
the problem that may arise in forking a multithreaded is that one thread
may lock a mutex and then another call for fork, which means that the child
inherits the locked mutex.
i do not know internaly what library function use this mutex, e.g. will
close() need this mutex ? will dup() will ? in this case, trying to close a
side of a pipe, duping it or even closing open fds (for security issues)
may hang the child while the library waits for the mutex , and no other
thread exists in the child to release it
a only way to resolve it, is to fork to another process before generating
the other threads, the second process will be left single threaded and so
may fork() with no problems, but this way i can't use pipe(), and need to
revert to named pipe which is ugly
any pther ideas ? things i missed ?
>
>
> --
> Nadav Har'El | Monday, Mar 19
> 2012,
> nyh at math.technion.ac.il
> |-----------------------------------------
> Phone +972-523-790466, ICQ 13349191 |This space is for sale - inquire
> inside.
> http://nadav.harel.org.il |
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.cs.huji.ac.il/pipermail/linux-il/attachments/20120319/1f698075/attachment-0001.html>
More information about the Linux-il
mailing list