SMP 内核、进程和线程究竟如何协同工作?

发布于 2024-09-04 16:55:35 字数 513 浏览 3 评论 0原文

在单核 CPU 上,每个进程都在操作系统中运行,CPU 从一个进程跳转到另一个进程以充分利用自身。一个进程可以有许多线程,在这种情况下,CPU 在各自的进程上运行时会通过这些线程运行。

现在,在多核 CPU 上:

  • 内核是否在每个进程中一起运行,或者内核可以在某个特定时间点在不同进程中单独运行吗?例如,您有程序 A 运行两个线程。双核CPU可以运行这个程序的两个线程吗?我认为如果我们使用像 OpenMP 这样的东西,答案应该是肯定的。但是,当内核在此 OpenMP 嵌入式进程中运行时,其中一个内核是否可以简单地切换到其他进程?

  • 对于单核创建的程序,在100%运行时,为什么每个核的CPU利用率是分布的? (例如,双核 CPU 为 80% 和 20%。在这种情况下,所有核心的利用率始终加起来为 100%。)核心是否尝试通过以某种方式运行每个线程、每个进程来互相帮助?

On a single core CPU, each process runs in the OS, and the CPU jumps around from one process to another to best utilize itself. A process can have many threads, in which case the CPU runs through these threads when it is running on the respective process.

Now, on a multiple core CPU:

  • Do the cores run in every process together, or can the cores run separately in different processes at one particular point of time? For instance, you have program A running two threads. Can a dual core CPU run both threads of this program? I think the answer should be yes if we are using something like OpenMP. But while the cores are running in this OpenMP-embedded process, can one of the cores simply switch to other process?

  • For programs that are created for single core, when running at 100%, why is the CPU utilization of each core distributed? (e.g. A dual core CPU of 80% and 20%. The utilization percentage of all cores always add up to 100% for this case.) Do the cores try to help each other by running each thread, of each process, in some ways?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

灵芸 2024-09-11 16:55:35

核心(或 CPU)是计算机执行代码的物理元素。通常,每个内核都具有执行计算、寄存器文件、中断线等所需的所有元素。

大多数操作系统将应用程序表示为进程。这意味着应用程序有自己的地址空间(==内存视图),操作系统确保该视图及其内容与其他应用程序隔离。

进程由一个或多个线程组成,它们通过在CPU上执行机器代码来执行应用程序的实际工作。操作系统决定哪个线程在哪个 CPU 上执行(通过使用巧妙的启发式方法来改善负载平衡、能耗等)。如果您的应用程序仅包含一个线程,那么您的整个多 CPU 系统不会对您有太大帮助,因为它仍然只为您的应用程序使用一个 CPU。 (但是,整体性能仍可能会提高,因为操作系统将在其他 CPU 上运行其他应用程序,因此它们不会与第一个 CPU 混合)。

现在回答您的具体问题:

1)操作系统通常允许您至少给出有关您想要在哪个核心上执行某些线程的提示。 OpenMP 的作用是生成代码,生成一定数量的线程,以将程序循环中的共享计算工作分配到多个线程中。它可以使用操作系统的提示机制(请参阅:线程关联)来执行此操作。
然而,OpenMP 应用程序仍将与其他应用程序同时运行,因此操作系统可以自由地中断其中一个线程并在 CPU 上安排其他(可能不相关)工作。
实际上,根据您的情况,您可能需要应用许多不同的调度方案,但这是非常具体的,大多数时候您应该能够相信您的操作系统会为您做正确的事情。

2) 即使您在多核 CPU 上运行单线程应用程序,您也会注意到其他 CPU 也在工作。这来自 a) 来自同时执行其工作的操作系统和 b) 来自您的应用程序永远不会单独运行的事实 - 每个正在运行的系统都由一大堆并发执行的任务组成。检查 Windows 的任务管理器(或 Linux 上的 ps/top)来检查正在运行的内容。

Cores (or CPUs) are the physical elements of your computer that execute code. Usually, each core has all necessary elements to perform computations, register files, interrupt lines etc.

Most operating systems represent applications as processes. This means that the application has its own address space (== view of memory), where the OS makes sure that this view and its content are isolated from other applications.

A process consists of one or more threads, which carry out the real work of an application by executing machine code on a CPU. The operating system determines, which thread executes on which CPU (by using clever heuristics to improve load balance, energy consumption etc.). If your application consists only of a single thread, then your whole multi-CPU-system won't help you much as it will still only use one CPU for your application. (However, overall performance may still improve as the OS will run other applications on the other CPUs so they don't intermingle with the first one).

Now to your specific questions:

1) The OS usually allows you to at least give hints about on which core you want to execute certain threads. What OpenMP does is to generate code that spawns a certain amount of threads to distribute shared computational work from loops of your program in multiple threads. It can use the OS's hint mechanism (see: thread affinity) to do so.
However, OpenMP applications will still run concurrently to others and thus the OS is free to interrupt one of the threads and schedule other (potentially unrelated) work on a CPU.
In reality, there are many different scheduling schemes you might want to apply depending on your situation, but this is highly specific and most of the time you should be able to trust your OS doing the right thing for you.

2) Even if you are running a single-threaded application on a multi-core CPU, you notice other CPUs doing work as well. This comes a) from the OS doing its job in the meantime and b) from the fact that your application is never running alone -- each running system consists of a whole bunch of concurrently executing tasks. Check Windows' task manager (or ps/top on Linux) to check what is running.

东走西顾 2024-09-11 16:55:35

另请注意,操作系统不太关心线程来自哪个进程。它通常会将线程调度到处理器/内核,无论线程来自哪个进程。这可能会导致一个进程的四个线程同时运行,就像四个进程的一个线程同时运行一样容易。

Note also that the OS doesn't much care which process the threads are from. It will usually schedule threads to processors / cores regardless of which process the thread is from. This could lead to four threads from one process running at the same time, as easily as one thread from four processes running at the same time.

無處可尋 2024-09-11 16:55:35

@BjoernD,你提到过..

.. 如果您的应用程序仅包含单个线程,那么您的
整个多CPU系统不会对你有太大帮助,因为它仍然只会使用
一个 CPU 适合您的应用程序...

我认为即使它是一个单线程应用程序,该应用程序线程在其生命周期内也可能在不同的内核上执行。在每次抢占和稍后分配给 CPU 时,可能会为该线程分配不同的核心。

@BjoernD, you mentioned that..

.. If your application consists only of a single thread, then your
whole multi-CPU-system won't help you much as it will still only use
one CPU for your application...

I think even if its a single threaded application, that application thread may be executed on different cores during its lifetime. On each preemption and later assignment to a CPU, a different core may get assigned to that thread.

零度℉ 2024-09-11 16:55:35

是的,线程和进程可以在多核 CPU 上同时运行,因此这正如您所描述的那样工作(无论您如何创建这些线程和进程、OpenMP 或其他方式)。单个进程或线程一次仅在单个核心上运行。如果请求 CPU 时间的线程多于可用内核数(通常是这种情况),操作系统调度程序将根据需要将线程移入或移出内核。

单线程进程在多个 CPU 或内核上运行的原因与您的操作系统有关,而不是特定于硬件的任何功能。一些操作系统没有“线程亲和力”的意识 - 它们不关心线程正在哪个处理器上运行 - 因此,当需要重新评估正在使用的资源时(至少每秒几次),它们'会将线程/进程从一个核心/CPU 移动到另一个核心/CPU。除了导致缓存未命中之外,这通常不会影响进程的性能。

Yes, threads and processes can run concurrently on multi-core CPUs, so this works as you describe (regardless of how you create those threads and processes, OpenMP or otherwise). A single process or thread only runs on a single core at a time. If there are more threads requesting CPU time than available cores (generally the case), the operating system scheduler will move threads on and off cores as needed.

The reason why single-threaded processes run on more than one CPU or core is related to your operating system, and not specifically any feature of the hardware. Some operating systems have no sense of "thread affinity" - they don't care what processor a thread is running on - so when time comes to re-evaluate what resources are being used (several times a second, at least), they'll move a thread/process from one core/CPU to another. Other than causing cache misses, this generally doesn't affect the performance of your process.

小霸王臭丫头 2024-09-11 16:55:35

如果有一个线程应用程序有 10 个线程,最初它将在同一个 CPU/核心上启动。在一段时间内,由于 Linux 中的负载平衡器,多个线程将分配到其他核心/CPU。如果有多个这样的线程应用程序,我认为所有应用程序线程大多在同一个核心/CPU 上运行,因为线程的本地/全局变量在它们运行的​​核心的 l1/l2 缓存中很容易获得。它们离开核心比执行时间更耗时。如果线程需要在不同的核心中运行。我认为必须向线程提供亲和力信息。

If there is one thread application which has say 10 threads, initially it will start on the same CPU/core.over a period of time the multiple threads will be distributed to other cores/cpus due to the load balancer in Linux. If there are multiple such thread applications are there,I think all the application threads mostly run on the same core/cpu as the locals/globals of the threads are readily available in l1/l2 cache of the core in which they were running.Moving them out of the core is time consuming than their execution time.If the threads need be run in a different core.I think one has to supply the affinity info to the thread.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文