Thread: pthreads + fork()

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445

    pthreads + fork()

    If I start a pthread at the beginning of my program (a multi-process network server app that calls fork() each time a new client connection is received) will that thread start to run in the child process after calling fork(), or will the child process act as if that thread was not started?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    I discovered the answer for myself, experimentally.

    for those interested, the thread only executes in the parent process, unless it is started again in the child.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    http://www.opengroup.org/onlinepubs/...ions/fork.html

    Correct - only the thread calling fork() is duplicated in the child.

    gg

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I'm learning about these now myself. This is how I was going to respond, but I wasn't sure of myself.

    Is this all correct?

    It's my understanding that fork() starts processing the newly spawned process (the "child") at the point of the fork(). The child is running in it's own process (aka address space) and while the stack and other info is copied into the new process (address space), you do not have access to the pthread that was started prior to the fork() - it's not in the child address space.
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Something like that. As I understand it, the thread that calls fork is the only thread that is copied to the new process.

    The Rationale section of the pthread_atfork() documentation gives good insight on the kind things to be aware of when calling fork() in a multi-threaded application.

    gg

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elkvis View Post
    If I start a pthread at the beginning of my program (a multi-process network server app that calls fork() each time a new client connection is received) will that thread start to run in the child process after calling fork(), or will the child process act as if that thread was not started?
    When a thread forks it changes back into a "regular" process, separate from the parent and any thread it contains.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is fork() a resource hog?
    By HalNineThousand in forum Linux Programming
    Replies: 8
    Last Post: 04-15-2008, 09:07 AM
  2. Fork(), pause(), and storing PID's in a linked list
    By vital101 in forum C Programming
    Replies: 10
    Last Post: 09-28-2007, 02:16 AM
  3. Fork - unpredictable?
    By fredkwok in forum Linux Programming
    Replies: 4
    Last Post: 03-26-2006, 02:49 PM
  4. fork(), exit() - few questions!
    By s3t3c in forum C Programming
    Replies: 10
    Last Post: 11-30-2004, 06:58 AM
  5. Daemon programming: allocated memory vs. fork()
    By twisgabak in forum Linux Programming
    Replies: 2
    Last Post: 09-25-2003, 02:53 PM