C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-22-2008, 12:42 PM   #1
Registered User
 
Join Date: Oct 2006
Posts: 298
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?
Elkvis is offline   Reply With Quote
Old 02-22-2008, 04:10 PM   #2
Registered User
 
Join Date: Oct 2006
Posts: 298
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.
Elkvis is offline   Reply With Quote
Old 02-22-2008, 04:17 PM   #3
Registered User
 
Codeplug's Avatar
 
Join Date: Mar 2003
Posts: 3,898
http://www.opengroup.org/onlinepubs/...ions/fork.html

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

gg
Codeplug is offline   Reply With Quote
Old 02-22-2008, 09:08 PM   #4
Jack of many languages
 
Dino's Avatar
 
Join Date: Nov 2007
Location: Katy, Texas
Posts: 2,070
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.
Dino is offline   Reply With Quote
Old 02-22-2008, 09:30 PM   #5
Registered User
 
Codeplug's Avatar
 
Join Date: Mar 2003
Posts: 3,898
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
Codeplug is offline   Reply With Quote
Old 02-23-2008, 12:15 AM   #6
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,761
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.
brewbuck is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 06:45 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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