bash 为进程分配 CPU 内核 - Linux
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33994983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Assigning a cpu core to a process - Linux
提问by Admia
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
有没有办法强制具有特定 PID 的进程仅在服务器的一个 CPU 上执行和运行?我知道有这样的命令
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.
但是上面的命令在我的系统上不起作用。所以请让我知道是否有任何其他命令。
回答by sjsam
There are two ways of assigning cpu core/cores to a running process.
有两种方法可以将 CPU 核心/核心分配给正在运行的进程。
First method:
第一种方法:
taskset -cp 0,4 9030
Pretty clear ! assigning cpu cores 0 and 4 to the pid 9030.
很清楚!将 CPU 内核 0 和 4 分配给 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 :
这有点复杂。后面的十六进制数-p
是位掩码。可以在此处找到解释,其中摘录如下:
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.
CPU 亲和性表示为位掩码,最低位对应第一个逻辑 CPU,最高位对应最后一个逻辑 CPU。并非所有 CPU 都可能存在于给定系统上,但掩码可能指定的 CPU 多于存在的 CPU。检索到的掩码将仅反映与系统上物理上的 CPU 相对应的位。如果给出了无效掩码(即,对应于当前系统上没有有效 CPU 的掩码),则返回错误。掩码通常以十六进制给出。
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.
我已经添加了对应于十六进制数的二进制文件,并且处理器从零开始从左开始计数。在第一个示例one
中,位掩码中有一个对应于第zero
th 个处理器,因此该处理器将为某个进程启用。所有zero
在位掩码中具有相应位置的处理器都将被禁用。事实上,这就是它被称为面具的原因。
Having said all these, using taskset to change the processor affinity requires that :
说了这么多,使用 taskset 来改变处理器亲和性需要:
A user must possess CAP_SYS_NICE to change the CPU affinity of a process. Any user can retrieve the affinity mask.
用户必须拥有 CAP_SYS_NICE 才能更改进程的 CPU 亲和性。任何用户都可以检索关联掩码。
Please check the Capabalities Man Page.
请查看能力手册页。
You might be interested to look at this SO Questionthat deals with CAP_SYS_NICE.
您可能有兴趣查看处理 CAP_SYS_NICE 的这个SO Question。
My Resources
我的资源