I have a misunderstanding about how cores, process and threads works:
So, when I'm running a process (which contains several threads) on Linux OS, and check the "top -H" command, I can see that the threads are distributed on multiple cores.
So How can it works ? (Threads of same process, which share same process address space, run on different cores ?) ?
What I miss here ?
Thanks
The Linux kernel scheduler is scheduling tasks. See this answer to a nearly identical question which explains what tasks are.
A task may run (at a given moment) on some single core. The scheduler may move tasks from one core to another (but rarely do so, since it takes time to warm up a core and its L1 cache).
A multi-threaded process usually have several tasks (one per thread) which usually can run on several cores.
You probably should avoid having a big lot of threads per process. I would recommend at most a dozen threads, especially if several of them are runnable (but details vary with hardware and system)
Read also about processor affinity
Every time that the operating system switches a CPU to another thread, it sets all the registers on the CPU for that thread. This includes the current stack, the memory access permissions, all of that.
So when two threads of the same process run on two different CPU cores, each of those cores is set to access that process memory.
When the operating system decides that one of the threads has used too much CPU time, it suspends the thread and copies all the CPU registers into memory. It then loads another thread into the CPU core.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With