taskset Command in Linux



The taskset command in Linux is used to set or retrieve the CPU affinity of a process. CPU affinity binds or restricts a process or thread to run on specific CPUs or cores within a multi-core system.

By setting CPU affinity, a process can be "bound" to certain processor cores, preventing the operating system from scheduling it to run on any available CPU. This helps reduce unnecessary context switching and cache misses.

Table of Contents

Here is a comprehensive guide to the options available with the taskset command −

Syntax of taskset Command

The syntax of the taskset command in Linux is as follows −

taskset [options] mask command [arguments…]

The syntax consists of optional flags [options], followed by a mask, which is a hexadecimal or binary value defining CPU affinity. For example, 0x1 binds the process to CPU 0. The command is the process to be executed with the specified CPU affinity, and [arguments...] is an optional field for the command being run.

taskset Command Options

The options for the taskset command are as follows −

Short OptionLong OptionDescription
-a--all-tasksSet or retrieve the CPU affinity of all the tasks (threads) for a given PID.
-c--cpu-listInterpret mask as a numerical list of processors instead of a bitmask. Numbers are separated by commas and may include ranges (e.g., 0,5,8-11).
-p--pidOperate on an existing PID and do not launch a new task.
-h--helpDisplay help text and exit.
-V--versionPrint version and exit.

Examples of taskset Command in Linux

This section explores how to use the taskset command in Linux with examples −

  • Setting CPU Affinity
  • Retrieving CPU Affinity
  • Setting CPU Affinity in Hexadecimal
  • Displaying Usage Help

Setting CPU Affinity

To set the CPU affinity, use the -c or --cpu-list option followed by the CPU cores and the command −

taskset -c 1,3 top

The above command binds the top command to CPUs 1 and 3.

Similarly, to set CPU affinity before executing the nginx process, use the taskset command in the following way −

taskset -c 0,1 nginx

To run a file, use the following command −

taskset -c 0,1 python3 myscript.py

Note that changing CPU affinity can impact system performance. It may improve execution on multi-core systems, but improper use can cause imbalances. Monitor performance and adjust settings as needed.

Retrieving CPU Affinity

To retrieve the CPU affinity, use the -p or --pid option followed by the process ID (PID) −

taskset -p 3854
taskset Command in Linux1

Setting CPU Affinity in Hexadecimal

To set CPU affinity using hexadecimal, the mask represents which CPUs the process can run on. Each bit in the hexadecimal mask corresponds to a CPU core: 1 means the process can run on that CPU, and 0 cannot.

Binary representation −

  • CPU 0 = 0
  • CPU 1 = 1
  • CPU 2 = 0
  • CPU 3 = 1

This gives the binary string 1010, which means CPUs 1 and 3 are enabled.

Now, convert binary to hexadecimal: 1010 in binary is 0xA in hexadecimal.

To set the CPU affinity to CPU cores 1 and 3, the binary string is 1010, which converts to 0xA in hexadecimal.

taskset 0xA top

To set the affinity of an already running process, use −

taskset -p 0x6 3943

The process with PID 3943 will be bound to CPUs 1 and 2. The hexadecimal mask 0x6 corresponds to the binary mask 00000110, which enables CPUs 1 and 2.

taskset Command in Linux2

The output pid 3943's current affinity mask: f means that the process with PID 3943 is currently set to run on CPUs 0, 1, 2, 3 (0xF in hexadecimal corresponds to CPUs 0 to 3). This shows the process is allowed to run on CPUs 0, 1, 2, and 3.

In the same way −

Bind toBinaryHexadecimal
CPU 000010x1
CPUs 0, 1, and 310110xB
All CPUs (if 8 CPUs)111111110xFF

Using hexadecimal allows for a compact representation of the CPU affinity mask, especially for systems with many CPU cores.

Displaying Usage Help

To display the usage help for the taskset command, use the -h or --help option −

taskset -h

Conclusion

The taskset command in Linux is a powerful tool for assigning or retrieving the CPU affinity of processes. By binding processes to specific CPU cores, it helps optimize performance by reducing unnecessary context switching and cache misses.

The taskset command supports various options, such as setting CPU affinity using numeric or hexadecimal masks, retrieving the affinity of a process, or adjusting the CPU cores a process is bound to.