Centos7 进程与CPU绑定
可以在命令行中使用 taskset 命令指定进程运行在特定CPU上。
其在 man 中描述如下:
taskset is used to set or retrieve the CPU affinity of a running process given its PID or to launch a new COMMAND with a given CPU affinity. CPU affinity is a scheduler property that "bonds" a process to a given set of CPUs on the system. The Linux scheduler will honor the given CPU affinity and the process will not run on any other CPUs. Note that the Linux scheduler also supports natural CPU affinity: the scheduler attempts to keep processes on the same CPU as long as practical for performance reasons. Therefore, forcing a specific CPU affinity is useful only in certain applications.
简单说就是这个命令可以查看或者设置进程能够在哪些 CPU 上运行。
语法:
taskset [options] mask command [arg]...
示例:
1.列出指定进程关联的 CPU:
taskset -p 27502
pid 27502's current affinity mask: 3
mask 值对应于二进制格式的0000 0011
,这意味着 27502 进程可以在2个不同的 CPU 核心(从0到1)中的任何一个上运行。
其中还有另外一种表现形式:
taskset -cp 27502
pid 27502's current affinity list: 0,1
这种方式直接以列表的形式显示。
2.运行程序时指定关联的 CPU:
taskset 1 ./server
taskset -p 27509
pid 27509's current affinity mask: 1
也可以用列表形式指定:
taskset -c 1 ./server
taskset -cp 27514
pid 27514's current affinity list: 1
3.设置运行中进程关联的 CPU:
taskset -cp 0 27514
pid 27514's current affinity list: 1
pid 27514's new affinity list: 0