taskset
is a command-line utility in Linux that allows you to retrieve or set the CPU affinity of a process. CPU affinity dictates which CPU or set of CPUs a process is allowed to run on.
Browse / Taskset Command Cheatsheet
Taskset Command Cheatsheet
A quick reference guide for the Linux `taskset` command, used to set or retrieve the CPU affinity of a process.
Taskset Basics and Usage
What is taskset?
|
Setting CPU affinity can be useful for:
|
Processes inherit the CPU affinity mask from their parent process by default. |
The affinity mask is a bitmask representing the set of CPUs on which the process may run. Bit 0 corresponds to CPU 0, bit 1 to CPU 1, and so on. |
CPUs are numbered starting from 0. |
The affinity setting is only valid for the process and its children (unless explicitly changed by the children). |
The |
Syntax Overview
Retrieve affinity of an existing PID:
|
Launch a new command with a specific affinity:
|
Set affinity for an existing PID:
|
|
|
|
|
Representing CPU Masks/Lists
Hexadecimal Mask |
A bitmask where the N-th bit (starting from 0) is set if the process can run on CPU N.
|
CPU List (using |
A comma-separated list of CPU numbers or ranges.
|
Choosing Representation |
The CPU list ( |
Retrieving CPU Affinity (`-p`)
Syntax |
|
Example: Get affinity of PID 1 |
(This example output shows affinity for all CPUs up to 31 on a 32-bit system, assuming |
Using |
Use
(Example for a system with 32 CPUs) |
Finding a process PID |
Use commands like |
Example: Find PID and get affinity for ‘myprocess’ |
|
Setting Affinity and Examples
Setting Affinity for a New Command
Syntax (Hex Mask) |
|
Syntax (CPU List) |
|
Example: Run |
|
Example: Run |
|
Example: Run |
|
Example: Run |
|
Example: Run |
|
Verification |
You can verify the affinity of the newly launched process using |
Setting Affinity for a Running Process
Syntax (Hex Mask) |
|
Syntax (CPU List) |
|
Example: Restrict PID 1234 to CPU 0 (hex mask) |
|
Example: Restrict PID 1234 to CPU 0 (CPU list) |
|
Example: Allow PID 1234 to run on CPU 0, 2, and 4 (CPU list) |
|
Permissions |
You typically need sufficient privileges (e.g., root) to set the affinity of processes owned by other users. You can usually set the affinity for your own processes. |
Tips and Best Practices
Be Cautious with Critical Processes: Avoid restricting essential system processes unless you know exactly what you’re doing, as it could impact system stability. |
Check System CPU Count: Use |
Understand Hyper-threading: If your system uses hyper-threading, remember that logical CPUs are numbered sequentially (e.g., 0, 1, 2, 3 on a dual-core CPU with HT). Cores 0 and 1 might be logical threads on one physical core, while 2 and 3 are on another. Restricting a process to CPU 0 and 1 might keep it on a single physical core, which might not be the performance gain you expect. |
Verify Affinity: Always use |
Combine with |
Persistent Affinity: |
Checking which CPU a process is currently running on: Use |
Common Options
|
Affect all threads of the given PID. By default, |
|
Interpret MASK as a comma-separated list of CPU numbers and ranges, rather than a bitmask. |
|
Operate on the specified PID instead of launching a new command. |
|
Display help information. |
|
Display version information. |