in reply to Re: Re: Why use threads over processes, or why use processes over threads?
in thread Why use threads over processes, or why use processes over threads?
I'm not familiar with linux or most other unixes, but there would have to be at least the replication of filesystem objects -- duping of open file handles, sockets etc. and associated state.
I would imagine that it also requires the creation of handles to the existing memory objects in order to handle COW etc.
In addition, every write, which on the evidence of Abigail above, can frequently mean a perl-level read, will result in a memory copy operation (though I'm not sure what the granularity is). There will also be some amount of overhead associated with detecting writes to shred memory segments. Whether this is a software or hardware interupt, the effect upon L1 and L2 caches etc. can be expensive too.
It's unclear to me how forking handles other shared handles like DB connections, hardware connections to tape drives, serial ports and the like, but I think that it is probably down to the user to handle this rather than fork.
None of these things is individually expensive, but the convenience of spawning a thread, without requiring any of this is considerable. The greatest use, and the greatest benefit from threads is for performing asynchronous reads (from whatever). This use is simply not possible with forks. The select model just doesn't compare for usability, and event-driven models require you to throw away even standard structured programming techniques, never mind object-oriented models and revert to relying upon global state.
Finally, the benefits of co-routines are totally absent from the forking model, but are almost trivial to implement using threads.
I don't see threads and processes as an either/or proposition. In an ideal world, the programmer would have both spanners in his toolkit, and would be free to choose whichever is appropriate for the task at hand. For some tasks one is appropriate, for others, the other. In some cases a mixture of the two makes perfect sense, if the underlying system supports both efficiently. The best choice will sometimes be dictated by the underlying system.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Re: Why use threads over processes, or why use processes over threads?
by Anonymous Monk on Nov 11, 2003 at 21:15 UTC | |
by BrowserUk (Patriarch) on Nov 11, 2003 at 22:02 UTC | |
by Aristotle (Chancellor) on Nov 12, 2003 at 03:33 UTC | |
by BrowserUk (Patriarch) on Nov 12, 2003 at 08:07 UTC | |
by Anonymous Monk on Nov 11, 2003 at 23:40 UTC | |
by BrowserUk (Patriarch) on Nov 12, 2003 at 00:50 UTC |