How to Use Taskset Command in Linux

In Linux, the “taskset” is a Command-Line utility that sets or retrieves the affinity of CPUs regarding the process. CPU affinity refers to the ability of a process or thread to run on a specific CPU or set of CPUs. It is useful for performance tuning or isolating the process from different processes executing on the same device. 

This article will briefly explain the “taskset” command with examples.

How Does the taskset Command Work in Linux?

The “taskset” command binds a process or thread to a particular or set of CPUs. The basic syntax of the command is as follows:

$ taskset [options] mask command

Where “mask” is a hexadecimal bitmask representing the CPUs on which the process or thread should be allowed to run, and “command” is the script that you want to run with the specified CPU affinity.

The options and detailed usage can be viewed from the command:

$ taskset --help

Here are a few examples of how the taskset command can be used in Linux.

Example 1: Assign the CPU Affinity to a Process

To assign the CPU affinity to a particular process, utilize the “c” option by specifying the process name. In our case, assign the CPU 0 and CPU 1 to the process “top”:

$ taskset -c 0,1 top

The output shows that CPU 0 and CPU 1 have been set to the “top” process.

Example 2: Extract the CPU Affinity of an Executing Process

To retrieve the CPU affinity of a running process with PID 1234, users can execute the following command with the “p” option:

$ taskset -p 1234

The output returns the CPU affinity 3 of the process having id 1234.

Example 3: Display the List of Current CPU Affinity of the Process 

To display the list of current CPU affinities of the particular process, specify the process id with the “cp” option. In our case, the process id “3141” is specified to display the current CPU affinity: 

$ taskset -cp 3141

The output retrieves the CPU affinity 0 and 1 of the specified process.

Example 4: Re-Assign the CPU Affinity to a Process

Users can re-assign the CPUs for changing affinity. For this, run a process with PID 1234 to CPU 1 that is already bound with the CPU 0:

$ taskset -cp 1 1234

The output confirms the current affinity has been changed.

Example 5: Launch a Process by Assigning Process ID

To launch a new process with the specific process id, execute the “taskset” command by specifying the process name as below:

$ taskset 3242 gedit

The above command will launch the “gedit” application with the “3242” process in the Linux system.

Example 6: Assign a Range of CPUs to a Process

The “taskset” command can assign a specific CPU or range of CPUs to a process. You can also assign a process to multiple CPUs by specifying a range of CPU IDs. For instance, assign the process to CPUs 0-2 to the “2213” existing process:

$ taskset -cp 0-2 2213

The output shows that a range of CPUs from 0 to 2 are assigned to the specific process id “1234”. 

Conclusion

Linux offers the “taskset” command to set or retrieve the CPU affinity of a running and new process or thread in the operating system. Using the command, users can display the list of current CPU affinity of the process, re-assign the CPU affinity to a process, launch a process by assigning process ID, and many more. This article briefly explained the “taskset” command and all possible examples.