Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux: Processes and Threads in a Multi-core CPU

Is it true that threads, compared to processes, are less likely to benefit from a multi-core processor? In other words, would the kernel make the decision of executing threads on a single core rather than on multiple cores?

I'm talking about threads belonging to the same process.

like image 702
someguy Avatar asked Aug 03 '10 10:08

someguy


People also ask

Can a threads be run on multiple CPUs?

The answer is: It depends. On a system with multiple processors or CPU cores (as is common with modern processors), multiple processes or threads can be executed in parallel. On a single processor, though, it is not possible to have processes or threads truly executing at the same time.

Is Linux multi threaded?

Yes, linux is fully multithreaded. On an SMP system you can even see kernel threads running concurrently on separate CPUs. As an aside, it makes more sense to call them kernel threads and not kernel processes because all of these share the same address space.

How do threads communicate on multicore processors?

In multi-core processors, a mechanism to exploit the cores' proximity and allow fast communications between cores is needed. At the hardware level, thread communications depend on cache coherence mechanisms, resulting in demand-based data transfers.


3 Answers

I don't know how the (various) Linux scheduler handle this, but inter-thread communication gets more expensive when threads are running on different Cores.

So the scheduler may decide to run threads of a process on the same CPU if there are other processes needing CPU time.

Eg with a Dual-Core CPU, if there are two processes with two threads and all are using all CPU time they get, it is better to run the two threads of the first process on the first Core and the two threads of the other process on the second core.

like image 58
IanH Avatar answered Oct 11 '22 07:10

IanH


That's news to me. Linux in particular makes little distinction between threads and processes. They are really just processes that share their address-space.

like image 43
Marcelo Cantos Avatar answered Oct 11 '22 05:10

Marcelo Cantos


Multiple single-threaded processes are more expensive to the system than single multi-threaded ones. But they will benefit from multicore CPU with same efficiency. Plus inter-thread communication is much cheaper then inter-process communication. If these threads really form single application i vote for multithreading.

like image 2
Andrey Avatar answered Oct 11 '22 05:10

Andrey