Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it faster to run one process that spawns N threads or to run N processes?

I realize that this will largely depend on the processes in question, but is there a rule of thumb?

Say I have a multi-threaded program called progX that offers a command line switch (--cpu) controlling the number of CPUs it can use. Is it faster to launch 40 parallel instances using one CPU each (progX --cpu 1) or to launch a single instance, telling it to use 40 CPUs (progX --cpu 40)?

like image 546
terdon Avatar asked Jan 12 '13 18:01

terdon


1 Answers

it is faster to lauch a single instance by far. Threads are made for that purpose, and they are lighter that processes. The de-facto rule is: let the OS do the scheduling and memory management unless you need to do the dirty job by yourself. This way your code will be much simpler and cleaner. The OS has a bunch of lower level tools to handle processes and memory much more efficiently. Of course it will depend on the OS, but this is a general rule for modern OS, and at least the one i use (Linux).

like image 111
canolucas Avatar answered Nov 15 '22 09:11

canolucas