what happens if

2012-03-19 Thread Erez D
what happens if i am running a multithreaded app ( 3 threads ) and one
thread calls fork() ?
___
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il


Re: what happens if

2012-03-19 Thread Baruch Siach
Hi Erez,

On Mon, Mar 19, 2012 at 10:47:20AM +0200, Erez D wrote:
 what happens if i am running a multithreaded app ( 3 threads ) and one
 thread calls fork() ?

See pthread_atfork(3).

baruch

-- 
 http://baruch.siach.name/blog/  ~. .~   Tk Open Systems
=}ooO--U--Ooo{=
   - bar...@tkos.co.il - tel: +972.2.679.5364, http://www.tkos.co.il -

___
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il


Re: what happens if

2012-03-19 Thread ronys
FTFM:
The  child  process  is  created with a single thread - the one
that called fork().


2012/3/19 Erez D erez0...@gmail.com

 what happens if i am running a multithreaded app ( 3 threads ) and one
 thread calls fork() ?

 ___
 Linux-il mailing list
 Linux-il@cs.huji.ac.il
 http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il




-- 
Ubi dubium, ibi libertas (where there is doubt, there is freedom)
___
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il


Re: what happens if

2012-03-19 Thread Nadav Har'El
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().


-- 
Nadav Har'El|Monday, Mar 19 2012, 
n...@math.technion.ac.il |-
Phone +972-523-790466, ICQ 13349191 |This space is for sale - inquire inside.
http://nadav.harel.org.il   |

___
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il


Re: what happens if

2012-03-19 Thread Erez D
On Mon, Mar 19, 2012 at 11:40 AM, Nadav Har'El n...@math.technion.ac.ilwrote:

 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,
 n...@math.technion.ac.il
 |-
 Phone +972-523-790466, ICQ 13349191 |This space is for sale - inquire
 inside.
 http://nadav.harel.org.il   |

___
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il


Re: what happens if

2012-03-19 Thread Nadav Har'El
On Mon, Mar 19, 2012, Erez D wrote about Re: what happens if:
 my reason for fork is only for exec() - to protect the first program from
 the other.

Then you should be all fine.

 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

If you're talking about the C language, you won't have any of these
problems. System calls like close() or dup() do not use any pthread
capabilities like mutexes.

If you're talking about a different programming language, I frankly
have no idea what its fork() does. Maybe you need to write some C
code that does fork()/exec() in one function call, and avoid this risk.

Did actually notice any problem with your code, or is this question
just theoretical?

-- 
Nadav Har'El|Monday, Mar 19 2012, 
n...@math.technion.ac.il |-
Phone +972-523-790466, ICQ 13349191 |If God had intended us to be vegetarians,
http://nadav.harel.org.il   |He wouldn't have made animals out of meat

___
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il


Re: what happens if

2012-03-19 Thread Erez D
On Mon, Mar 19, 2012 at 12:20 PM, Nadav Har'El n...@math.technion.ac.ilwrote:

 On Mon, Mar 19, 2012, Erez D wrote about Re: what happens if:
  my reason for fork is only for exec() - to protect the first program from
  the other.

 Then you should be all fine.

  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

 If you're talking about the C language, you won't have any of these
 problems. System calls like close() or dup() do not use any pthread
 capabilities like mutexes.

 If you're talking about a different programming language, I frankly
 have no idea what its fork() does. Maybe you need to write some C
 code that does fork()/exec() in one function call, and avoid this risk.

i will be using C++. are you sure this is safe ?


 Did actually notice any problem with your code, or is this question
 just theoretical?

theoretical


 --
 Nadav Har'El|Monday, Mar 19
 2012,
 n...@math.technion.ac.il
 |-
 Phone +972-523-790466, ICQ 13349191 |If God had intended us to be
 vegetarians,
 http://nadav.harel.org.il   |He wouldn't have made animals out of
 meat

___
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il


Re: what happens if

2012-03-19 Thread Nadav Har'El
On Mon, Mar 19, 2012, Erez D wrote about Re: what happens if:
  If you're talking about the C language, you won't have any of these
  problems. System calls like close() or dup() do not use any pthread
  capabilities like mutexes.
..
 i will be using C++. are you sure this is safe ?

I'm sure. But don't sue me if I'm wrong ;-)

-- 
Nadav Har'El|Monday, Mar 19 2012, 
n...@math.technion.ac.il |-
Phone +972-523-790466, ICQ 13349191 |Disclaimer: The opinions expressed above
http://nadav.harel.org.il   |are not my own.

___
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il


Re: what happens if

2012-03-19 Thread Erez D
On Mon, Mar 19, 2012 at 12:25 PM, Nadav Har'El n...@math.technion.ac.ilwrote:

 On Mon, Mar 19, 2012, Erez D wrote about Re: what happens if:
   If you're talking about the C language, you won't have any of these
   problems. System calls like close() or dup() do not use any pthread
   capabilities like mutexes.
 ..
  i will be using C++. are you sure this is safe ?

 I'm sure. But don't sue me if I'm wrong ;-)

I wont
thanks


 --
 Nadav Har'El|Monday, Mar 19
 2012,
 n...@math.technion.ac.il
 |-
 Phone +972-523-790466, ICQ 13349191 |Disclaimer: The opinions expressed
 above
 http://nadav.harel.org.il   |are not my own.

___
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il


Re: what happens if

2012-03-19 Thread Diego Iastrubni
On יום שני 19 מרץ 2012 12:22:15 Erez D wrote:
 On Mon, Mar 19, 2012 at 12:20 PM, Nadav Har'El 
n...@math.technion.ac.ilwrote:
  On Mon, Mar 19, 2012, Erez D wrote about Re: what happens if:
   my reason for fork is only for exec() - to protect the first program
   from the other.
  
  Then you should be all fine.

Another reason is for interacting with libraries which are singleton. I had 
to work with a library to talk to some special HW. The library was designed to 
work with one device per process, and I needed to support many.

I found myself forking and using pipe() to use the API from my main app. The 
problem is that that fork() was called from a thread. Read ahead.

  If you're talking about the C language, you won't have any of these
  problems. System calls like close() or dup() do not use any pthread
  capabilities like mutexes.
  
  If you're talking about a different programming language, I frankly
  have no idea what its fork() does. Maybe you need to write some C
  code that does fork()/exec() in one function call, and avoid this risk.
 
 i will be using C++. are you sure this is safe ?

All theory I read before implementing this said this was a bad idea. The 
theory says that libc may maintain some mutex inside malloc() which is called 
from printf() for example. This means that even trivial things may kill your 
app. The theory says that in multithreaded applications as soon as you clone() 
(the system called used by pthread_create()) you should execvp.

In my application (a lot of C++, running on linux 2.6.32, glibc 2.9 and glibc 
2.11.1 on ARM) erverything worked perfectly against the theory, your mileage 
may vary.

___
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il