views:

202

answers:

4

Possible Duplicate:
What are the thread limitations when working on Linux compared to processes for network/IO-bound apps?

What is meant by context switches in threads or processes ? Also which is better : Threads or Processes ? I mean which is more space efficient and which is more time efficient ?

+2  A: 

Take a look at this answer.

Matt Joiner
It's not my fault your answers are ill-informed and airy. Make yourselves known.
Matt Joiner
A: 

I don't think you can say one is better than the other - it depends what you are doing.

There is a detailed overview of context switches here: http://en.wikipedia.org/wiki/Context_switch

In the most basic sense a context switch is when the processor pauses execution of a task to do another task.

Adam Butler
A: 

Also, you have to remember that threads may be your only choice if your OS is embedded and doesn't support multiple processes ;)

BigSandwich
A: 

Not sure I like any of the other answers so I'll add my $0.02.

Context switch for threads is when the process interrupts the current thread so that another can run. It has the store the current thread's context to memory and then switch to the other thread -- loading in its context and running it. For processes, it is the OS which is interrupting the process so another process can get some cycles. Threads and processes are very blurred under most modern operating systems. For example, under Linux, each thread has a separate process-id although they have overlapping memory space.

In terms of which one is better is a more complicated question. In general, threads are "better" than processes. Context switching out an entire process usually requires a lot more work as opposed to a thread. Threads usually have much smaller memory footprints that needs to be stored/restored when the switch happens as opposed to processes. Although in the old days, the only way to get parallel operation was to run multiple processes, modern thread implementations are very efficient at scheduling threads across processors.

This said, there are still places where you might want to run jobs in separate processes depending on the architecture and workload.

Gray