linux 内核线程是否在 cpu 硬件线程上映射 1:1 或 m:n?

Does linux kernel thread map 1:1 or m:n on cpu hardware threads?

例如,我在 linux 上有 4 个进程,每个进程包含 5 个 pthread 线程,我的 cpu 是 i7 4C8H。

问题

(1) For each process, does it restrict all its threads to consume just 1 cpu thread? Could one process use all the cpu Core/threads?

(2) Does linux kernel allocate time slice for each process equally(if they're all busy, and dont' specify and priority), or allocate time slice per their thread number, the more thread of a process, the more time slice?

1) 一个进程可以轻松地使用处理器上可用的所有 cores/hyperthreads,只需生成多个线程或进程并让它们执行任何类型的计算(例如,无限循环)。内核的调度程序通常会尝试将负载分散到所有可用资源上

2) 我不知道有一种机制可以为具有更多线程的进程分配更多时间。我相信每个线程都会被调度程序视为一个单独的可运行实体,并将参与标准时间切片舞蹈。特别是,Linux 的当前调度程序 (CFS - Completely Fair Scheduler) 非常努力地在所有 processes/threads

之间公平分配时间片

1) 一个进程的线程可以运行多个CPUs/cores并行。如果一次只有一个线程 运行 ,多线程就不会那么有用了!默认情况下,内核可以并且将会在任何可用的核心上安排一个线程。其他线程,即使是同一进程的一部分,运行在另一个核心上运行也没有区别。

有一个 cpuset 系统,您可以在其中将线程锁定到可用核心的某些子集。您可能需要某个进程的所有线程在某个核心上 运行。参见 pthread_getaffinity_np()

2)线程,而不是进程,是Linux中的基本调度实体。所以是的,具有更多线程的进程将获得更大份额的 CPU!你问的是 PTHREAD_SCOPE_SYSTEM vs PTHREAD_SCOPE_PROCESS 请参阅 pthread_attr_setscope()。 Linux threads (NPTL) 不支持后者。另见 pthread_setconcurrency(),它也不对 Linux 执行任何操作,因为 Linux 是 1:1。

但是,Linux 确实支持一个名为 "group scheduling." 的概念,将其视为一种在具有不同线程数的进程之间实现公平的方法。