|
Home > Archive > Unix Programming > November 2006 > threads or fork
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Bruintje Beer 2006-11-17, 1:25 am |
| Hello,
I do not understand why you use threads if you can fork a process. Can
somebody tell me what is better to use : threads or fork a process.
Johan
| |
| Pascal Bourguignon 2006-11-17, 7:30 am |
| "Bruintje Beer" <me@knoware.nl> writes:
> I do not understand why you use threads if you can fork a process. Can
> somebody tell me what is better to use : threads or fork a process.
Both are better.
They don't do the same thing.
fork _duplicates_ the process, the whole process state, including the
memory. The resulting processes are independant.
threads work inside the same process, only they're allocated a
specific stack, and they get their own processor register set. They
share the memory and all the process state.
When you kill a process, only this process is killed, but the whole
process is killed including all its threads. When you terminate a
thread, only this thread is terminated, and the other threads and its
process still run.
All the threads of a process share the same memory space, so they can
"easily" work on the same dataset. On the other hand, the processes
having distinct memory spaces cannot easily work on the same
data. They must go thru IPC (Inter Process Communication) mechanisms
to pass data to and fro. (They can share some block of memory, but not
the whole space).
+-------------------+
|process memory s |
| t |
| +------+ a |
| |thread|---+ c |
| +------+ +--->k |
| | | |
| | +---->data |
| v |
| program |
+-------------------+
after a fork:
+-------------------+ +-------------------+
|process memory s | |process memory s |
| fork res=cpid t | | fork res=0 t |
| +------+ a | | +------+ a |
| |thread|---+ c | | |thread|---+ c |
| +------+ +--->k | | +------+ +--->k |
| | | | | | | |
| | +---->data | | | +---->data |
| v | | v |
| program | | program |
+-------------------+ +-------------------+
after creation of a new thread:
+-------------------+
|process memory s |
| t |
| +------+ +>a |
| |thread|---+ | c |
| +------+ +--->k |
| | | | |
| | +-+>data | |
| v | ^ | s |
| program| | +-+ t |
| ^ +--|-|-->a |
| | +------+ c |
| +---|thread|->k |
| +------+ |
+-------------------+
--
__Pascal Bourguignon__ http://www.informatimago.com/
"You question the worthiness of my code? I should kill you where you
stand!"
| |
| sjdevnull@yahoo.com 2006-11-17, 7:30 am |
| Bruintje Beer wrote:
> Hello,
>
> I do not understand why you use threads if you can fork a process. Can
> somebody tell me what is better to use : threads or fork a process.
>
Threads share their memory, which can alternately be stated as threads
do not have memory protection from each other.
Barring some odd platform-specific corner case, use threads if you
cannot isolate which memory you want to share--normally this means that
you're sharing a lot of complex data structures. Use processes
otherwise.
Using processes + IPC sometimes seems more complex at the outset than
just using threads, but often turns out to be easier to maintain and
less buggy in the long-term. It requires thinking about your
programming desing and being clear on what's shared, which is normally
a very good thing that will make your life easier. But sometimes
threads are really a better solution.
Until you develop a feel for what the differences are, err toward
process based solutions if possible.
| |
| raxitsheth2000@yahoo.co.in 2006-11-20, 7:28 am |
| do a Web search for "thread vs process"
raxit
|
|
|
|
|