taskset is a Linux command-line utility that allows you to get or set the CPU affinity of a process or start a new process with a defined CPU affinity. The CPU affinity of a process determines which CPU cores are allowed to execute the process’s threads.
By default, a process can run on any CPU core that is available at the time of execution. However, setting CPU affinity can help you optimize the performance of your system. For example, if you have a multi-core processor and you want to dedicate a particular core to a specific process, you can use taskset to do this.
If you encounter the below error while running the command taskset:
taskset: command not found
you may try installing the below package as per your choice of distribution:
Distribution | Command |
---|---|
Debian | apt-get install util-linux |
Ubuntu | apt-get install util-linux |
Alpine | apk add util-linux |
Arch Linux | pacman -S util-linux |
Kali Linux | apt-get install util-linux |
CentOS | yum install util-linux |
Fedora | dnf install util-linux |
OS X | brew install util-linux |
Raspbian | apt-get install util-linux |
taskset Command Examples
1. Get a running process’ CPU affinity by PID:
# taskset --pid --cpu-list pid
2. Set a running process’ CPU affinity by PID:
# taskset --pid --cpu-list cpu_id pid
3. Start a new process with affinity for a single CPU:
# taskset --cpu-list cpu_id command
4. Start a new process with affinity for multiple non-sequential CPUs:
# taskset --cpu-list cpu_id_1,cpu_id_2,cpu_id_3
5. Start a new process with affinity for CPUs 1 through 4:
# taskset --cpu-list cpu_id_1-cpu_id_4
Summary
In summary, taskset is a useful tool for managing the CPU affinity of processes on a Linux system. It can help you optimize the performance of your system by dedicating specific CPU cores to specific processes. More information about the command can be found in the manual page by typing “man taskset” in the terminal.