Pin Linux Processes to Specific CPUs with taskset

Quick Linux Tip #75:

Question: My high-priority process needs dedicated cores. How do I pin it to specific CPUs?

Try: taskset -cp 0,1 3456

Info: taskset sets or retrieves CPU affinity. The -c option specifies CPU numbers, while -p applies the affinity to an existing process by PID.

Examples:

  • $ taskset -c 0-3 ./program  # Launch a program on CPUs 0-3
  • $ taskset -c 4,5,6,7 -p 3456  # Reassign PID 3456 to CPUs 4-7
  • $ numactl --cpunodebind=0 ./program  # Bind a process to NUMA node 0

Note: CPU affinity limits a process to the selected CPUs, but does not by itself make those CPUs exclusively available to that process. For stronger CPU isolation, combine affinity with appropriate kernel CPU-isolation features such as isolcpus. Use numactl when CPU and memory locality across NUMA nodes matters.




LinuxTeck.com
linuxteck@ubuntu:~$ taskset -cp 0,1 3456
pid 3456's current affinity list: 0-7
pid 3456's new affinity list: 0,1

linuxteck@ubuntu:~$ taskset -c 2,3 ./cpu_intensive_task

linuxteck@ubuntu:~$ taskset -cp $(pgrep cpu_intensive)
pid 4567's current affinity list: 2,3
linuxteck@ubuntu:~$

PREVIOUS ARTICLE Quick Linux Tip #74: Compress Old Linux Logs and Keep 30 Days NEXT ARTICLE Quick Linux Tip #76: Generate a Self-Signed SSL Certificate with OpenSSL
About John Britto

John Britto Founder & Chief-Editor @LinuxTeck. A Computer Geek and Linux Intellectual having more than 20+ years of experience in Linux and Open Source technologies.

View all posts by John Britto →

Leave a Reply

Your email address will not be published.