Multithreading or Light-weight processes ?
Light-weight processes are far more secure than threads in the sense they don't share memory and thus avoid a whole host of problems associated with it.
IMO, they are also easier to work with (while programming); I find the message-passing IPC model simpler and more manageable.
Additionally when it comes to parallel computing; even there light-weight processes are a win-win scenario. There's no need for complex algorithms that manage shared memory between CPUs when each CPU can be assigned 1/more L.W.-processes and they all interact by message passing.
I think on a well designed OS, L.W.-procs should be as efficient as threads.
Some applications like Google Chrome already use L.W.-procs (for each tab the user opens a seperate process is launched). It surprises me that a lot more people don't use it already, given its many advantages.
Which model of multitasking do you think is better? (especially in terms of programmer efficiency)
19 comments
[ 2.7 ms ] story [ 47.3 ms ] threadThings like chrome make an interesting case for using LWP in an environment that usually uses threads, but it still does not necessarily mean it is a better solution.
ps -eLf
To see what LWPs are
Avoiding terminology like process/thread that varies from system to system; my main point is memory sharing between multiple threads of execution should be avoided.
http://www.eecs.berkeley.edu/Pubs/TechRpts/2006/EECS-2006-1....
Threads are a seemingly straightforward adaptation of the dominant sequential model of computation to concurrent systems. Languages require little or no syntactic changes to support threads, and operating systems and architectures have evolved to efficiently support them. Many technologists are pushing for increased use of multithreading in software in order to take advantage of the predicted increases in parallelism in computer architectures. In this paper, I argue that this is not a good idea. Although threads seem to be a small step from sequential computation, in fact, they represent a huge step. They discard the most essential and appealing properties of sequential computation: understandability, predictability, and determinism. Threads, as a model of computation, are wildly nondeterministic, and the job of the programmer becomes one of pruning that nondeterminism. Although many research techniques improve the model by offering more effective pruning, I argue that this is approaching the problem backwards. Rather than pruning nondeterminism, we should build from essentially deterministic, composable components. Nondeterminism should be explicitly and judiciously introduced where needed, rather than removed where not needed. The consequences of this principle are profound. I argue for the development of concurrent coordination languages based on sound, composable formalisms. I believe that such languages will yield much more reliable, and more concurrent programs.
Sometimes you need a multi-threaded model...i think a GUI library is a good example of that. Other times, a multi-process model would be easier to set up...a "just run in the background" sort of thing.
I also don't quite understand why suddenly everybody is afraid of writing multi-threaded applications. Just make sure you understand how the threading model works in your specific technology and what abstractions are provided. If your application is complex enough to require a "complex algorithm to manage shared memory", then i think you need to really take your time to understand what you're trying to do.
Having said that; I acknowledge that chasing down bugs caused by threads is not fun at all. Especially race conditions since they are seemingly un-predictable. But i think it is getting easier with newer platforms and tools.
Threads make intercommunication cheap but at the price of exposing to problems like deadlocks or data access synchronization overhead.
My rule of thumb is to use process and move to threads only when there is a significant added value.
That's what threads are for - parallel paths that need to share memory. Processes are for parallel paths that rarely need to access the same data or to synchronize between them.
Don't forget also that when your CPU shares time, it divides up the time equally among the processes. So if your application creates 20 processes, it takes a disproportionate amount of CPU time (I believe, someone correct me if I'm wrong).
It depends on the OS. In most cases, threads are the only schedulable entity (think of a classic process as containing one thread), so there is no difference between 20 single-threaded processes and one process with 20 threads.
An additional benefit is security. Your model can rely on some processes having the capability to access confidential data. These act as proxies for accessing that data and protects it. The OpenBSD operating system has used this "privilege separation" trick for years.
Everything with shared memory will die in the long run. The hardware can't keep on fooling us with a big memory space shared among all processes anyway.