Linux taskset to retrieve or set a processess CPU affinity

Q. How do I use Linux (RHEL) taskset command to set or get the CPU affinity of a running process given its PID or to launch a new COMMAND with a given CPU affinity?


nixCraft: Privacy First, Reader Supported
  • nixCraft is a one-person operation. I create all the content myself, with no help from AI or ML. I keep the content accurate and up-to-date.
  • Your privacy is my top priority. I don’t track you, show you ads, or spam you with emails. Just pure content in the true spirit of Linux and FLOSS.
  • Fast and clean browsing experience. nixCraft is designed to be fast and easy to use. You won’t have to deal with pop-ups, ads, cookie banners, or other distractions.
  • Support independent content creators. nixCraft is a labor of love, and it’s only possible thanks to the support of our readers. If you enjoy the content, please support us on Patreon or share this page on social media or your blog. Every bit helps.

Join Patreon βž”

A. You need to use taskset command. CPU affinity is a scheduler property that “bonds” a process to a given set of CPUs on the system. The Linux scheduler will honor the given CPU affinity and the process will not run on any other CPUs. Note that the Linux scheduler also supports natural CPU affinity: the scheduler attempts to keep processes on the same CPU as long as practical for performance reasons. Therefore, forcing a specific CPU affinity is useful only in certain applications.

Retrieve the CPU affinity of an existing task

The general syntax is as follows:
taskset -p [pid] To retrieve the CPU affinity of an existing task (PID 12345), enter:
# taskset -p 12345

Use taskset command to CPU affinity

The general syntax is as follows:
taskset -c [0,1,2,3..N] [pid] Where,

  • -c 0,1,2,..N : The masks are typically given in hexadecimal. For example, 0x00000001 is processor #0, 0x00000003 is processors #0 and #1 etc. However -c option allows you to specifiy a numerical list of processors instead of a bitmask. The list may contain multiple items, separated by comma, and ranges. For example, 0,5,7,9-11.
  • pid : Process / program ID

For example set PID 12345 on 2-3 CPU, enter:
# taskset -c 2,3 12345

Did you notice? 🧐

nixCraft is ad-free to protect your privacy and security. We rely on reader support to keep the site running. Please consider subscribing to us on Patreon or supporting us with a one-time support through PayPal or purchase official merchandise. Your support will help us cover the costs of hosting, CDN, DNS, and tutorial creation.

About the author: Vivek Gite is the founder of nixCraft, the oldest running blog about Linux and open source. He wrote more than 7k+ posts and helped numerous readers to master IT topics. Join the nixCraft community via RSS Feed or Email Newsletter.

πŸ₯Ί Was this helpful? Please add a comment to show your appreciation or feedback.


3 comments… add one
  • N D Hari Dass May 24, 2008 @ 5:52

    I wanted to use taskset to attach cpu cores to specific processes.
    I followed your example. But it does not work and returns the
    error

    taskset -c 5 29885
    execvp: no such file or dir
    failed to execute 29885

    Also trying to execute taskset at the mpi command line like
    mpirun -np 2 taskset -c 4,5 test output &
    simply does not work.

    Any help most greatfully appreciated.

  • dfsdfgsdfg Jun 1, 2008 @ 7:13

    taskset sets the processor affinity of a NEW process — one that you specify as the rest of the parameters on the command line. like the ‘nice’ program. it is giving you an error because you are telling to to run the command 29885. to modify the affinity of an existing process, use the -p option. ex: taskset -p 1 29885.

  • VIKAS Dec 5, 2011 @ 14:05

    Correct Syntax is as below

    [root@vikasbox ~]# taskset -c -p 1 31777
    pid 31777’s current affinity list: 0
    pid 31777’s new affinity list: 1
    [root@vikasbox ~]#

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.