About Linux FAQ

Browse More FAQs:

Linux taskset to retrieve or set a processess CPU affinity

Posted by Vivek Gite [Last updated: October 3, 2007]

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?

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

Subscribe to our free e-mail newsletter or RSS feed to get all updates. You can Email this page to a friend.

Related Other Helpful FAQs:

Discussion on This FAQ

  1. N D Hari Dass Says:

    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.

  2. dfsdfgsdfg Says:

    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.

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , , , , , ,

Copyright © 2006-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.