linux - cpu 亲和性,只允许进程在特定的 cpu 上运行

标签 linux bash affinity

我想通过以下方式将进程绑定(bind)到特定的核心#0(cpu 亲和性)

taskset -c 0 ./run_prog

当它自己的程序在核心 #0 上运行时,操作系统可能会决定将其他后台和事件进程运行到核心 #0。换句话说,该命令将 run_prog 严格限制在核心 #0 但不会阻止其他进程在核心 #0 上运行,这很糟糕!

有什么方法可以编写 bash 脚本来首先阻止事件进程在核心 #0 上运行,然后再运行该任务集命令?

最佳答案

我已经在对原始问题的一系列评论中讨论了这个问题,但我认为这是“对潜在问题的答案”,而不是对您的具体问题的具体答案,所以我们开始吧:

I've heard this question a couple of times, and it was always being asked out of a misunderstanding of how simultaneous multiprocessing works. First of all: Why do you need your process on core #0? Do you have a reason?

typically, the linux kernel is quite efficient at scheduling tasks to processors in a manner that minimizes the negative effects that either process migration or a single-core-bottleneck would bring. In fact, it's very rare to see someone actually gain performance by manually setting affinities, and that usually only happens when the userland process is closely communicating with a kernel module which for some hardware or implementation reason is inherently single-threaded and can't easily be migrated.

Your question itself shows a minor misunderstanding: Affinity means that the kernel knows that it should schedule that process on the given core. Hence, other processes will automatically be migrated away from that core and only be running there if your desired task leaves a lot of that core unused. To change the "priority" your process has in CPU allocation, just change its nice value.

The reason is performance measurement by isolating the running process. Regarding the second comment, that mean we have to rely on OS scheduler because it will not run a background process on a core which is currently utilized 100% while there are idle cores.

您要衡量的不是流程的绩效 孤立地,但是进程,绑定(bind)到单个 CPU!为一个 单线程进程没问题,但想象一下你的进程可能 有多个线程——这些通常都在不同的线程上运行 核心,整体性能会高得多。一般来说,试试 只是最小化机器上的非进程工作负载(即运行时不 正在运行的窗口管理器/ session 管理器,停止所有非必要的 服务)并使用一个非常小的 nice 值——该度量可能是 比较准确。

另外,time 命令可以让你知道一个进程用了多少时间 完全花费(包括等待),作为用户态进程占用 CPU, 并在系统调用中占用 CPU——我认为这可能符合您的需要 够好了:)

关于linux - cpu 亲和性,只允许进程在特定的 cpu 上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30691578/

相关文章:

linux - touch 命令在一个目录下创建多个文件(不同名称)

bash - 使用 bash 脚本为 Sublime Text 2 构建系统

ubuntu - 脚本在while循环中运行时计算机不会关闭

c++ - Windows Phone 中的进程关联

用于设置核心亲和性的 C++ 风格?

c++ - 在 Windows 与 Linux 中运行 C++ 程序

python - 从后台启动的 python 程序打印消息

node.js - Node worker_threads + 集群 + 亲和性

java - Linux 和 java : File is being created but text is not written

linux - 如何将 "command -i interval | tail"输出写入文件