Linux CPU Scheduling

taskset — set or retrieve CPU affinity

taskset binds a process to specific CPU cores (CPU affinity). This is useful for performance tuning, workload isolation, and testing.

What taskset is good for

Improper CPU pinning can reduce performance or starve other processes. Use thoughtfully.

10 Practical Examples

1) Show CPU affinity for PID

taskset -p 1234

2) Set affinity to CPU 0

sudo taskset -p 0x1 1234

0x1 = binary 0001 (CPU 0)

3) Set affinity to CPU 0 and 1

sudo taskset -p 0x3 1234

0x3 = binary 0011 (CPU 0 & 1)

4) Launch program on specific CPU

taskset -c 2 myprogram

5) Launch program on multiple CPUs

taskset -c 0,2,4 myprogram

6) Use CPU range

taskset -c 0-3 myprogram

7) Combine with chrt

sudo chrt -f 50 taskset -c 1 myprogram

8) Check CPU count

nproc
lscpu

9) Verify affinity

ps -o pid,psr,cmd -p 1234

10) Combine with schedtool

taskset -c 0 sudo schedtool -F -p 80 myprogram