pthreads + fork()

This is a discussion on pthreads + fork() within the Linux Programming forums, part of the Platform Specific Boards category; If I start a pthread at the beginning of my program (a multi-process network server app that calls fork() each ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    2,590

    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
    2,590
    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,702
    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
    Katy, Texas
    Posts
    2,309
    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.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,702
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    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, 10: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, 03: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, 03:53 PM

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