为进程分配CPU核心-Linux [英] Assigning a cpu core to a process - Linux
问题描述
有没有办法强制执行具有特定PID的进程,使其仅在服务器的一个CPU上执行并运行?我知道有这样的命令
taskset -cp< Cpu_Number> < Pid>
但以上命令在我的系统上不起作用。因此,请告诉我是否还有其他命令。
有两种方法将cpu核心分配给正在运行的进程。
第一种方法:
任务集-cp 0,4 9030
很清楚!将CPU核心0和4分配给pid 9030。
第二种方法:
任务集-p 0x11 9030
这有点复杂。 -p
之后的十六进制数字是位掩码。可以在
我已添加十六进制数和处理器对应的二进制数从零开始从左开始计数。在第一个示例中,位掩码中的 one
对应于第个零
处理器,因此将启用该处理器一个过程。所有在位掩码中的相应位置零
的处理器都将被禁用。实际上,这就是为什么它被称为掩码的原因。
所有这些,使用任务集更改处理器关联性要求:
用户必须拥有CAP_SYS_NICE才能更改
进程的CPU关联性。任何用户都可以检索相似性掩码。
请检查功能手册页。
您可能有兴趣查看此 SO问题,涉及CAP_SYS_NICE。
我的资源
Is there any way to force a process with specific PID, to be executed and run on only one of the cpu s of a server? I know that there is a command like this
taskset -cp <Cpu_Number> <Pid>
but the above command does not work on my system. So please let me know if there is any other command.
There are two ways of assigning cpu core/cores to a running process.
First method:
taskset -cp 0,4 9030
Pretty clear ! assigning cpu cores 0 and 4 to the pid 9030.
Second Method:
taskset -p 0x11 9030
This is a bit more complex. The hexadecimal number that follows -p
is a bitmask. An explanation can be found here, an excerpt of which is given below :
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.
Still confused? Look at the image below :
I have added the binaries corresponding to the hexadecimal number and the processors are counted from left starting from zero. In the first example there is a one
in the bitmask corresponding to the zero
th processor, so that processor will be enabled for a process. All the processors which have zero
to their corresponding position in the bitmask will be disabled. In fact this is the reason why it is called a mask.
Having said all these, using taskset to change the processor affinity requires that :
A user must possess CAP_SYS_NICE to change the CPU affinity of a process. Any user can retrieve the affinity mask.
Please check the Capabalities Man Page.
You might be interested to look at this SO Question that deals with CAP_SYS_NICE.
My Resources
这篇关于为进程分配CPU核心-Linux的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!