Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The question is all in the title: How can I set the processor affinity of a process on Linux?

share|improve this question
up vote 18 down vote accepted

I have used taskset for this. If you have taskset installed, something like:

taskset -c 1,3 -p 45678

would set the process with id 45678 to have an affinity to cpus 1 and 3.

share|improve this answer

Inside the process, the call would be sched_setaffinity(), or for pthreads stuff, pthread_setaffinity_np()

On a related note, if you're worrying about CPU affinity of your program, it may be worthwhile to pay attention to how it's doing memory allocation as well. Larger systems with memory attached to more than one controller (i.e. multiple CPU sockets, each with their own) will have variable latency and bandwidth between different CPU-memory pairs. You'll want to look into NUMA affinity as well, using the numactl command or the system calls that it works with. One program I worked on got a 10% performance improvement from this.

share|improve this answer

You need to install schedutils (Linux scheduler utilities). I have use it on my Ubuntu Desktop.

SF link

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.