gpt4 book ai didi

bash - 为进程分配 cpu 核心 - Linux

转载 作者:行者123 更新时间:2023-11-29 08:48:26 27 4
gpt4 key购买 nike

有什么方法可以强制具有特定 PID 的进程仅在服务器的一个 cpu 上执行和运行?我知道有这样的命令

taskset -cp <Cpu_Number> <Pid>

但是上面的命令在我的系统上不起作用。所以请让我知道是否有任何其他命令。

最佳答案

有两种方法可以将 cpu 内核分配给正在运行的进程。

第一种方法:

taskset -cp 0,4 9030

很清楚!将 cpu 核心 0 和 4 分配给 pid 9030。

第二种方法:

taskset -p 0x11 9030

这有点复杂。 -p 后面的十六进制数是位掩码。可以找到解释here ,摘录如下:

The CPU affinity is represented as a bitmask, with the lowest order bit corresponding to the first logical CPU and the highest order bit corresponding to the last logical CPU. Not all CPUs may exist on a given system but a mask may specify more CPUs than are present. A retrieved mask will reflect only the bits that correspond to CPUs physically on the system. If an invalid mask is given (i.e., one that corresponds to no valid CPUs on the current system) an error is returned. The masks are typically given in hexadecimal.

还在迷茫?请看下图:

enter image description here

我添加了与十六进制数对应的二进制文件,处理器从零开始从左开始计数。在第一个示例中,位掩码中有一个 one 对应于第 0 处理器,因此将为进程启用该处理器。位掩码中相应位置的 的所有处理器都将被禁用。事实上,这就是它被称为面具的原因。

综上所述,使用任务集更改处理器亲和性需要:

A user must possess CAP_SYS_NICE to change the CPU affinity of a process. Any user can retrieve the affinity mask.

请检查Capabalities Man Page .

您可能有兴趣查看此 SO Question处理 CAP_SYS_NICE。

我的资源

  1. TutorialsPoint

  2. XModulo

关于bash - 为进程分配 cpu 核心 - Linux,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33994983/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com