LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-31-2005, 04:05 AM   #1
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Rep: Reputation: 30
what happens if i create a thread and then fork the process?


hiho@ll

just an example:

a thread reads from a socket
it does accept(); , reads some bytes writes something back and if it's time it closes the socket

what happens if i fork the whole process after i created the thread?

what if the thread read something and NOW 2 PROCESSES and 2 threads read from the same socket at the same time

does this mean i have a big synchronization problem?
because if both threads read from the socket, then only one thread get's the data
so the whole stuff gets out of sync

or maybe this scenario isn't possible?

how would it work?
how could i synchronize such stuff?

thx@ll
 
Old 10-31-2005, 06:34 AM   #2
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
I assume you're trying to build something like a threaded server.

First create your server and start listening.
When a request comes in, accept it and ONLY THEN start a thread for communication with the client. Now you only have one socket in each thread.

But I'm not sure if that is what you're trying to achieve.
 
Old 10-31-2005, 08:52 AM   #3
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Original Poster
Rep: Reputation: 30
yeah it has something to do with a threaded server

but i know the concept you're talking about

but mostly i need an answer for my question
 
Old 10-31-2005, 09:19 AM   #4
naf
Member
 
Registered: Oct 2005
Location: Chicago, USA
Distribution: Slackware & Fedora
Posts: 66

Rep: Reputation: 15
Re: what happens if i create a thread and then fork the process?

Quote:
Originally posted by Thinking
what happens if i fork the whole process after i created the thread?
Then you have one process with two threads and a second process. The first two threads are sharing the data, and all three would share the file descriptors (including sockets). File locks are not inherited to the child process (from fork()). The important aspect here is that the new process inherits from the calling thread.


Quote:
Originally posted by Thinking
what if the thread read something and NOW 2 PROCESSES and 2 threads read from the same socket at the same time

does this mean i have a big synchronization problem?
because if both threads read from the socket, then only one thread get's the data
so the whole stuff gets out of sync

or maybe this scenario isn't possible?
You have a challenge. Perhaps you should follow Wim Sturkenboom's logic. You will have race conditions. You should close unused files descriptors when you fork. When you fork(), the child has its own copy of the file descriptor.

Quote:
Originally posted by Thinking
how would it work?
how could i synchronize such stuff?
Depends on what you want to do. You want all threads to have the same data read from the socket? More details are needed.
 
Old 10-31-2005, 09:52 AM   #5
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Original Poster
Rep: Reputation: 30
i'm not sure if i understood what you mean

Quote:
Then you have one process with two threads and a second process.
i don't realy understand this:
Process 1 (P1) creates a thread
P1 forks();

why should P1 have now 2 threads and P2 0 threads? (but i think i misunderstood)
i thought if i fork P1 with a thread, P2 will also have the same thread at exactly the same position as P1
so if Thread1 (T1) is waiting for data T2 will wait too, but only one thread will get the data, right?

Quote:
The first two threads are sharing the data, and all three would share the file descriptors (including sockets).
which FIRST two?
which THREE? (three processes, threads? why three?)

Quote:
File locks are not inherited to the child process (from fork()). The important aspect here is that the new process inherits from the calling thread.
i think it's more important to know what happens to the threads?
does P2 have a thread?

the problem with the synchronization will be, that i have to sync many processes with many threads
it's like a many to many synchronization
and that's the challenge, because how could i know if
many equals "everybody who needs it"
because maybe some thread does something different

what i want to do is create a high configurable server
the server forks childs (e.g. 5 childs)
and childs have some threads listening on a port

now i don't want to have 5 childs
i want 6
this means i have to fork a new child
but the main server itself will have some threads at this time
so i would fork a process with some threads

i know that one possibility is "just don't do it"
but i want to try
thx@ll
 
Old 10-31-2005, 10:16 AM   #6
naf
Member
 
Registered: Oct 2005
Location: Chicago, USA
Distribution: Slackware & Fedora
Posts: 66

Rep: Reputation: 15
Lets start with the main process. Call it P1.

P1 creates 4 threads. P1T1 (original thread of execution), P1T2, P1T2, P1T3.

Open three files. Call them F3, F4, F5. Consider F0, F1 and F2 as stdin, stdout, and stderr.
So now you have 6 descriptors open for all four threads.

Now thread P1T3 decides to fork. Call that new process P2. P2 has only one thread (threads are not copied).

Now P1T1, P1T2, P1T3, P1T4 and P2 all share the 6 file descriptors. Now what?

You want P2 to create 5 threads. Ok. Now you 10 threads in two processes working on the same file descriptors. Why do this? Most servers (even multi-threaded ones) close all other descriptors and reserves a single descriptor from the parent. Subsequently, the parent usually closes the file descriptor it opened for the child (whether it be a disk file, a pipe or a socket) to avoid these kinds of interactions.
 
Old 10-31-2005, 02:07 PM   #7
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Original Poster
Rep: Reputation: 30
ok

got it

Quote:
Now thread P1T3 decides to fork. Call that new process P2. P2 has only one thread (threads are not copied).
and what happens if P1 forks(); ?
what happens to P1T1-P1T4?

you said "threads are not copied"
and for your example i understand this perfectly

but what happens if P1 forks?
does P2 have P2T1-P2T4?

why i'm not sure?
because as far as i know fork(); just copies the whole process
this means it looks like the kernel does a copy of some RAM to another position in RAM (well i'm sure it's not that easy that the kernel does only memcpy() *lol* )

but if this is (simplified) true, this would mean P2 has 4 threads, because the whole process is copied and the Threads are part of the process, not?

thx@naf
 
Old 10-31-2005, 02:22 PM   #8
naf
Member
 
Registered: Oct 2005
Location: Chicago, USA
Distribution: Slackware & Fedora
Posts: 66

Rep: Reputation: 15
When you fork(), only the active thread creates the new process. So the new process is a copy of only that thread (not all threads). It may not be a wise decision to have processes and threads running on the same memory space. I woudl be uncomfortable doing that anyway and would consider doing fork only to exec a new program.

Check this site for more info:

http://www.opengroup.org/onlinepubs/009695399/functions/fork.html
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
fork vs thread/pthread powerplane Programming 17 08-11-2012 02:13 PM
Process and thread PIDs msriram_linux Programming 4 11-27-2004 11:43 AM
collecting data from fork process frostmagic Programming 5 12-28-2003 01:55 PM
how to fork a process? Punker99 Programming 2 07-28-2003 11:21 AM
fork() by Process 0 Manish Programming 0 02-23-2002 05:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration