Linux - running a process on multiple cores

I have a misunderstanding about how kernels, processes and threads work:

  • The process has a number of threads.
  • All these threads share the same section of memory
  • The kernel has its own cache memory and address space.

So when I start a process (which contains multiple threads) on Linux OS and check the "top -H" command, I see that the threads are spread across multiple cores.

So how does it work? (Are the threads of the same process that use the same process address space running on different kernels?)?

What am I missing here?

thank

+3


source to share


2 answers


The Linux kernel scheduler is all about scheduling tasks. See this answer for a nearly identical question that explains what the challenges are.

The task can be executed (at the moment) on any one core. The scheduler can move tasks from one core to another (but rarely does this because it takes time to warm up the core and its L1 cache).

A multithreaded process usually has multiple tasks (one per thread) that can usually run on multiple cores.



You should probably avoid a lot of threads per process. I would recommend no more than a dozen threads, especially if multiple are running (but the details are hardware and system dependent).

Read also about processor proximity

+4


source


Every time the operating system switches the CPU to a different thread, it sets all registers in the CPU for that thread. This includes the current stack, 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 configured to access that process memory.



When the operating system decides that one of the threads is using too much processor time, it suspends the thread and copies all processor registers into memory. Then it loads another thread into the processor core.

+1


source







All Articles