Browse Docs

๐ŸŽฏ CPU Affinity

On this page

Source : http://www.glennklockwood.com/hpc-howtos/process-affinity.html

Taskset

1sudo apt-get install util-linux   # or: yum install util-linux
2taskset -c 1 script.sh          # run script.sh on CPU number 1  (-c: CPU, -p: PID)
3taskset -c 1,2,3 script.sh      # give it several CPUs.

Numactl

1numactl --cpunodebind=0 simulation.x
2numactl --cpunodebind=0 --membind=0 simulation.x
3numactl -C 0 -N 0 simulation.x
4numactl -C +0,1,2,3 simulation.x    # similar to taskset
5numactl -H                          # see which memory corresponds to a CPU

Note: with numactl, unlike taskset, you cannot change the CPU affinity of a process on the fly.

Verification

1# ps -eo pid,tid,class,rtprio,ni,pri,psr,pcpu,stat,wchan:14,comm
2  PID   TID CLS RTPRIO  NI PRI PSR %CPU STAT WCHAN          COMMAND
3    1     1 TS       -   0  19   4  0.0 Ss   -              init
4    2     2 TS       -   0  19   3  0.0 S    kthreadd       kthreadd
5    3     3 TS       -   0  19   0  0.0 S    run_ksoftirqd  ksoftirqd/0
6    6     6 FF      99   - 139   0  0.0 S    cpu_stopper_th migration/0
7    7     7 FF      99   - 139   0  0.0 S    watchdog       watchdog/0
1# taskset -c -p 4806
2pid 4806's current affinity list: 0-3
Sunday, October 4, 2026 Sunday, August 13, 2023