SMP 内核、进程和线程如何精确地协同工作? [英] How do SMP cores, processes, and threads work together exactly?

查看:19
本文介绍了SMP 内核、进程和线程如何精确地协同工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

现在,在多核 CPU 上:

Now, on a multiple core CPU:

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

  • 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?

为单核创建的程序,在100%运行时,为什么每个核的CPU利用率是分布的?(例如,80% 和 20% 的双核 CPU.在这种情况下,所有内核的利用率总和为 100%.)内核是否通过运行每个线程、每个进程以某种方式相互帮助??

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?

推荐答案

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

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.

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

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) 操作系统通常允许您至少提示您要在哪个内核上执行某些线程.OpenMP 所做的是生成生成一定数量线程的代码,以在多个线程中分配来自程序循环的共享计算工作.它可以使用操作系统的提示机制(参见:线程关联)来执行此操作.但是,OpenMP 应用程序仍将与其他应用程序同时运行,因此操作系统可以自由地中断其中一个线程并在 CPU 上安排其他(可能不相关的)工作.实际上,根据您的具体情况,您可能希望应用许多不同的调度方案,但这是非常具体的,并且大多数时候您应该能够相信您的操作系统会为您做正确的事情.

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

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.

这篇关于SMP 内核、进程和线程如何精确地协同工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆