Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Process vs Threads

Tags:

How to decide whether to use threads or create separate process altogether in your application to achieve parallelism.

like image 272
Nitin Avatar asked Sep 15 '08 13:09

Nitin


People also ask

What is the difference between a process and a thread?

A process is a program under execution i.e an active program. A thread is a lightweight process that can be managed independently by a scheduler. Processes require more time for context switching as they are more heavy. Threads require less time for context switching as they are lighter than processes.

Is it better to use threads or processes?

You'd prefer multiple threads over multiple processes for two reasons: Inter-thread communication (sharing data etc.) is significantly simpler to program than inter-process communication. Context switches between threads are faster than between processes.


2 Answers

Threads are more light weight, and for the making several "workers" just to utilize all availabe CPUs or cores, you're better of with threads.

When you need the workers to be better isolated and more robust, like with most servers, go with sockets. When one thread crashes badly, it usually takes down the entire process, including other threads working in that process. If a process turns sour and dies, it doesn't touch any other process, so they can happily go on with their bussiness as if nothing happened.

like image 102
wvdschel Avatar answered Sep 18 '22 13:09

wvdschel


Processes have more isolated memory. This is important for a number of reasons:

  • It is harder for a single task to crash the other tasks.
  • More memory will be available per process. This is important for large, high-performance applications like Apache or database servers, like Postgres. This is important for both allocated memory and memory mapped files.
like image 29
Adam Tegen Avatar answered Sep 21 '22 13:09

Adam Tegen