What happens a thread calls fork()?

This is a discussion on What happens a thread calls fork()? within the C++ Programming forums, part of the General Programming Boards category; I come across few questions: 1) A process has 3 threads, if process calls fork(), will the new process has ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    What happens a thread calls fork()?

    I come across few questions:

    1) A process has 3 threads, if process calls fork(), will the new process has the 3 threads as well?

    2) A process has 3 threads, if one thread calls fork(), will the new process has the 3 threads as well?

    3) Are there any shared data between the threads in different 2 processes in the above cases?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The new process will have ONE thread [a copy of the thread that called fork] according to this:
    http://www.opengroup.org/onlinepubs/...ions/fork.html

    1) and 2) are essentially the same - all processes have at least one thread - the main thread - in almost all aspects, the main thread is equal to other threads. The only real difference is that a child thread (aka non-main thread) does not cause the whole program to exit if it returns.

    3) Data and file descriptors are shared between the two processes. Data will be in copy-on-write state, so if either process writes to data [even if the data is not ACTUALLY changed, say you write 0 to a variable that was 0 before], it will be given its own copy of the data. So essentially the two processes will have their own data.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    Actually, it depends on the OS and threading model.

    If you're running Solaris and use the Solaris threads API (as opposed to the POSIX threads API), fork() will duplicate ALL threads in the process. If you only want to duplicate the calling thread in this situation, call fork1(). See the Solaris fork(2) man page.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,670
    Solaris 10 got it's act together and made Posix behavior the default. Before 10, you only got Posix behavior if linking with -lpthread.

    http://docs.sun.com/app/docs/doc/816-5167/fork-2?a=view

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multithreading in c
    By thebrighter in forum C Programming
    Replies: 8
    Last Post: 07-10-2007, 01:17 PM
  2. Replies: 2
    Last Post: 07-01-2007, 07:11 AM
  3. Thread Synchronization in Win32
    By passionate_guy in forum C Programming
    Replies: 0
    Last Post: 02-06-2006, 04:34 AM
  4. C++ Threading?
    By draggy in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2005, 12:16 PM
  5. Replies: 12
    Last Post: 05-17-2003, 05:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21