初学者 Linux taskset 命令教程(附示例)
在此页
- Linux 任务集命令
- Q1。如何使用 taskset 检索进程的 CPU 亲和力?
- Q2。如何使用任务集更改 CPU 亲和力?
- Q3。如何在更改亲缘关系时分配一系列 CPU?
- Q4。如何启动具有预定义 CPU 亲和力的进程?
- 结论
听说过处理器亲和性这个术语吗?它的一项功能允许您将进程绑定或取消绑定到特定的中央处理器或一系列 CPU。是的,您可以告诉系统应该使用哪个 CPU 核心来运行特定进程。有关为什么存在处理器亲和性的理论详细信息,请访问此处。
在这里,在本教程中,我们将讨论一个实用程序 - 称为任务集 - 可让您实现处理器亲和性。但在我们这样做之前,值得一提的是,本教程中的所有示例都已经在 Ubuntu 22.04 LTS 机器和 Debian 11 上进行了测试。
Linux 任务集命令
taskset 命令允许您设置或检索进程 CPU 关联。以下是它的语法:
taskset [options] mask command [argument...]
taskset [options] -p [mask] pid
以下是工具手册页对其的解释:
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 practi?
cal for performance reasons. Therefore, forcing a specific CPU affin?
ity is useful only in certain applications.
The CPU affinity is represented as a bitmask, with the lowest order bit
corresponding to the first logical CPU and the highest order bit corre?
sponding 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
may be specified in hexadecimal (with or without a leading "0x"), or as
a CPU list with the --cpu-list option. For example,
0x00000001 is processor #0,
0x00000003 is processors #0 and #1,
0xFFFFFFFF is processors #0 through #31,
32 is processors #1, #4, and #5,
--cpu-list 0-2,6
is processors #0, #1, #2, and #6.
When taskset returns, it is guaranteed that the given program has been
scheduled to a legal CPU.
以下是一些 Q&A 风格的示例,可以让您更好地了解 taskset 命令的工作原理。
Q1。如何使用 taskset 检索进程的 CPU 亲和力?
如果你想让 taskset 显示一个已经运行的进程的 CPU 亲和力,请按以下方式使用命令:
taskset -p [PID]
只需将 PID 替换为您要获取其 CPU 关联性的进程的 ID。您可以获得进程的进程 ID,例如通过使用 ps 命令。
ps aux
进程 ID 在 PID 列中。

例如:
taskset -p 2363
上面的命令返回了以下输出:
pid 2363's current affinity mask: 3

所以这里的十六进制值 3 表示该进程可以在 4 个处理器内核中的任何一个上运行:0、1、2、3。
如果您希望输出以 CPU 范围为单位,您可以添加 -c 命令行选项。
taskset -cp 9726
以下是这种情况下的输出:
pid 9726's current affinity list: 0-3
Q2。如何使用任务集更改 CPU 亲和力?
要调整现有进程的 CPU 亲和力,您需要指定进程 ID(就像我们在上一节中所做的那样)以及定义新亲和力的十六进制掩码。
例如,Gedit 进程(PID:9726)当前的 CPU 亲和力为 f。

要将关联更改为 0x11,请使用以下命令:
taskset -p 0x11 9726
然后您可以使用以下命令再次检查新的亲和力:
taskset -p 9726
以下屏幕截图显示了在我的案例中这些命令的输出:

所以你可以看到亲和力发生了变化。
Q3.如何在更改亲缘关系时分配一系列 CPU?
这没什么大不了的。您所要做的就是将 -c 命令行选项添加到我们在上一节中使用的命令以及 CPU 内核范围作为输入。
这是一个例子:
任务集-cp 0,3 9726
以下是在这种情况下产生的输出:
pid 9726's current affinity list: 0
pid 9726's new affinity list: 0,3
Q4.如何启动具有预定义 CPU 亲和力的进程?
是的,您还可以启动具有设置 CPU 亲和力的进程。
例如,我启动了 CPU affinity 为 0xa 的文本 gedit 编辑器。
任务集 0xa gedit
结论
同意,taskset 命令不适用于普通命令行用户。它主要由服务器端专家用于多核环境中的流程优化。我们在这里讨论了工具基础知识。有关更多信息,请访问其手册页。