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

查看:150
本文介绍了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利用率? (例如,双核CPU的利用率分别为80%和20%.在这种情况下,所有内核的利用率总和始终为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上运行其他应用程序,因此它们不会与第一个应用程序混合在一起,因此总体性能可能仍会有所提高.)

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

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天全站免登陆