如何控制进程在哪个核心上运行

如何控制进程在哪个核心上运行

本文介绍了如何控制进程在哪个核心上运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能理解如何编写一个使用多个进程或线程的程序:fork()一个新进程并使用IPC,或者创建多个线程并使用那些类型的通信机制.

I can understand how one can write a program that uses multiple processes or threads: fork() a new process and use IPC, or create multiple threads and use those sorts of communication mechanisms.

我也了解上下文切换.也就是说,只有一个CPU,操作系统才能为每个进程安排时间(并且有大量的调度算法),因此我们可以同时运行多个进程.

I also understand context switching. That is, with only once CPU, the operating system schedules time for each process (and there are tons of scheduling algorithms out there) and thereby we achieve running multiple processes simultaneously.

现在我们有了多核处理器(或多处理器计算机),我们可以在两个单独的核上同时运行两个进程.

And now that we have multi-core processors (or multi-processor computers), we could have two processes running simultaneously on two separate cores.

我的问题是关于最后一种情况:内核如何控制进程在哪个内核上运行?哪个系统调用(在Linux甚至Windows中)在特定内核上调度进程?

My question is about the last scenario: how does the kernel control which core a process runs on? Which system calls (in Linux, or even Windows) schedule a process on a specific core?

我问的原因:我正在为一个学校的项目工作,我们将在其中探索一个最新的计算主题-我选择了多核体系结构.关于如何在这种环境下进行编程(如何监视死锁或竞争条件),似乎有很多材料,但是关于控制各个内核本身的内容并不多.我希望能够编写一些演示程序并呈现一些汇编指令或C代码,以达到看,我在第二个内核上运行无限循环,查看的CPU利用率的峰值,特定核心".

The reason I'm asking: I'm working on a project for school where we are to explore a recent topic in computing - and I chose multi-core architectures. There seems to be a lot of material on how to program in that kind of environment (how to watch for deadlock or race conditions) but not much on controlling the individual cores themselves. I would love to be able to write a few demonstration programs and present some assembly instructions or C code to the effect of "See, I am running an infinite loop on the 2nd core, look at the spike in CPU utilization for that specific core".

任何代码示例?还是教程?

Any code examples? Or tutorials?

为澄清起见-许多人说这是操作系统的目的,应该让操作系统来解决这一问题.我完全同意!但是,我要问(或试图体会)的是操作系统实际上是做什么的.不是调度算法,而是更多的一旦选择了内核,必须执行什么指令才能使该内核开始获取指令?"

edit: For clarification - many people have said that this is the purpose of the OS, and that one should let the OS take care of this. I completely agree! But then what I'm asking (or trying to get a feel for) is what the operating system actually does to do this. Not the scheduling algorithm, but more "once a core is chosen, what instructions must be executed to have that core start fetching instructions?"

推荐答案

正如其他人所提到的,处理器关联性是特定于操作系统的.如果您想在操作系统范围之外执行此操作,那么您将获得很多乐趣,而我的意思是痛苦.

As others have mentioned, processor affinity is Operating System specific. If you want to do this outside the confines of the operating system, you're in for a lot of fun, and by that I mean pain.

也就是说,其他人提到了 SetProcessAffinityMask 用于Win32.没有人提及设置处理器亲和力的Linux内核方式,因此我会提到.您需要使用 sched_setaffinity(2) 系统调用.这是关于操作方法的一个不错的指南.

That said, others have mentioned SetProcessAffinityMask for Win32. Nobody has mentioned the Linux kernel way to set processor affinity, and so I shall. You need to use the sched_setaffinity(2) system call. Here's a nice tutorial on how.

此系统调用的命令行包装为 taskset(1) .例如
taskset -c 2,3 perf stat awk 'BEGIN{for(i=0;i<100000000;i++){}}'将busy-perf-stat的perf-stat限制为在内核2或3上运行(仍然允许它在内核之间迁移,但只能在这两个内核之间迁移).

The command-line wrapper for this system call is taskset(1). e.g.
taskset -c 2,3 perf stat awk 'BEGIN{for(i=0;i<100000000;i++){}}' restricts that perf-stat of a busy-loop to running on either of core 2 or 3 (still allowing it to migrate between cores, but only between those two).

这篇关于如何控制进程在哪个核心上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 05:34