taskset Cheatsheet - CPU Affinity
All essential taskset commands organized by use case, with 37+ entries you can copy and run directly. Find the right command fast when you need it.
Back to SysOpsBasic Usage 6
taskset -p <pid>Show a process's CPU affinity mask
taskset -c -p <pid>Show affinity in CPU-list format
taskset -c 0,1 <command>Pin to CPUs 0 and 1 on launch
taskset -c 0-3 <command>Pin to CPUs 0-3 on launch
taskset -p <mask> <pid>Change a running process's CPU mask
taskset -cp 0,1 <pid>Repin a running process to CPUs 0,1
CPU Mask & List Format 7
0x00000001Hex mask: CPU 0
0x00000003Hex mask: CPUs 0 and 1
0x0000000fHex mask: CPUs 0-3
-c 0CPU-list format, more readable
-c 0,2,4Specify discrete CPUs
-c 0-3,8-11Mixed range list
taskset -c 0xff <command>Bind to CPUs 0-7 with a hex mask
Pin Running Processes 4
taskset -cp 0 $(pgrep nginx)Change the nginx master's affinity
for p in $(pgrep -f app); do taskset -cp 0-3 $p; doneBatch-repin matching processes
taskset -cp 0-3 <pid>Bind a PID to CPUs 0-3
taskset -ap 0-1 <pid>Set affinity for the process and all its threads
Affinity & System Info 5
cat /proc/<pid>/status | grep Cpus_allowedView a process's CPU mask
cat /sys/devices/system/cpu/onlineView online CPU numbers
nprocView the number of available CPU cores
lscpuView CPU architecture details (cores, cache, NUMA)
lscpu | grep NUMAView NUMA node information
NUMA Optimization 5
numactl --hardwareView NUMA topology and node layout
numactl -c 0 -m 0 ./appBind CPU and memory to NUMA node 0
numactl --cpunodebind=0 --membind=0 ./appFull NUMA binding (CPU + memory)
numastat -p <pid>View a process's memory distribution per NUMA node
numactl --interleave=all ./appInterleave memory across all nodes
Practical Scenarios 5
taskset -c 0-3 ./cpu_intensive_appLimit an app to specific cores
taskset -c 4-7 ./realtime_appIsolate cores for a real-time app
taskset -c 0 nginxPin Nginx workers to specific cores
taskset -c 1,2 mysqlPin MySQL to specific cores
isolcpus=2,3Kernel param: isolate CPUs 2,3 from scheduling at boot
Related Tools 5
htopInteractively view per-core usage
top -1View per-core load
mpstat -P ALL 1Monitor each core in real time (needs sysstat)
pidstat -t -p <pid> 1View per-thread CPU usage
cset shield -c 4-7cset creates a CPU shield (needs cset installed)
Tips
- CPU affinity reduces cache misses and boosts performance, but over-pinning can cause uneven load.
- Real-time apps (HFT, A/V processing) suit isolated cores.
- Under NUMA, binding CPU and memory nodes cuts cross-node access latency.
- taskset -c's CPU-list format is more readable than hex masks; use it daily.
- The isolcpus kernel param isolates CPUs at boot — more thorough than taskset but needs a reboot.
- Multi-threaded processes need -a to set all threads, or only the main thread is affected.
Official References
Commands are compiled from the official docs below. Click to verify the latest usage.
Maintained by LaoHand
Publicly updated on Jul 21, 2026, continuously proofread against official docs.
Contact Us
Wrong command or description? Send us corrections, business inquiries or product feedback by email.
Contact Us