How to Assign a Process to a Certain CPU Core in Ubuntu Linux

Supposing that you have a multi-core processor, whenever you run a process (program), depending on its requirements, it might be executed using all the available multi-cores (say that it’s a multimedia encoding utility which needs a lot of your CPU cycles). Or, if it doesn’t need a lot of CPU usages and a single core is more than capable of handling it, then still, while executing, it might be switched between the multiple CPU cores by your operating system.

The OS does this due to various reasons but mainly for achieving a balance between the system’s stability and the performance. And it’s performed by a lower level software utility (Kernel process) called “Scheduler” which decides how to interact a certain process with the available CPU cores, by using a value called the “CPU Affinity Mask”.

For most users, it’s almost always best for letting the OS handle it. But for the power users, having the ability to make a certain process, to be executed by only using a single CPU core (or a certain number of cores) has a slight benefit concerning the performance.

This is because, in a multi-core CPU environment, all the individual CPUs have a separate cache which is only accessible by that CPU core (the most lowest levels of CPU cache is known as “L1” and other “L2”, “L3” are usually shared between the cores).

CPU Core 1 Says: “Oy, that hurts!” 😉 …

So what happens when executing a process by switching it between various CPU cores is that, when switching a process to a new CPU core, the “L1” cache of the new core has to be updated and the previous core’s “L1” cache might needed to be deleted etc. This results in a somewhat unnecessary cache activity that ultimately reduces the performance.

So if we only used a certain number of CPU cores while executing that particular program, then we can avoid unnecessary processor cache activities (it’s like with a HDD, if it’s busy deleting/updating files etc, then it’ll be a bit slow while doing other things) thus improving the performance, a bit.

This can be beneficial, especially while running CPU hungry tasks such as encoding (not be a good idea if the encoder is multi-threaded) or Virtualization for example.

It all sounds good, but …

However, it is a bit more complicated than that, and is quite difficult to say when it will be beneficial or non-beneficial from the user’s point of view. For instance, let’s take the same example mentioned above. If while running that program (let’s called it process ‘A’), assigned to a certain core, it was ‘paused’ due to a request from another process with a higher priority, but shortly after, another core was available so that the ‘paused’ process ‘A’ can be run with a minimal delay, yet, since we have assigned it to be executed using a different core, it won’t get that chance.

So it’ll have to wait till that process with higher priority (on the same core where our process ‘A’ is assigned) is finished running or ‘paused’, and thus decreasing its performance. So …

Anyhow, without boring you any further with all these technical nonsense ;-), in modern GNU/Linux distributions, there’s a simple tool that lets us bypass the OS’s automated task execution functionality and binds a program (process) to a certain number of CPU cores with ease. It’s called “taskset”.

You can use this utility to start a process from scratch or it can even “re-map” an existing process to a given number of CPU cores as well.

I’m using Ubuntu (11.10 Oneiric Ocelot), and since it comes this tool pre-installed, all you gotta do is to open up the Terminal window and use the below commands which should get the job done.

Starting a new process ….

Let’s say that I wanted to use a program called “burnP6” (CPU stressing utility) that should only be run/executed using the first CPU core. Then I’ll use the below command in my Terminal window.

taskset -c 0 burnP6

The “-c” argument is what enables changing the “CPU Affinity” thus should be untouched.

The value “0” represents the first CPU Core  (“0” is for the first one, “1” indicates the second one, “2” for the third one etc) and the “burnP6” is the name of the program (process) that I wanted to run. So depending on your needs, you’ll have to replace these two values with yours.

Let’s say that I wanted to run the same program using the CPU Core number “2”, then I’ll use the below command.

taskset -c 1 burnP6

If I wanted to run it using multiple cores (first and the second cores) then I’ll use it in the below format.

taskset -c 0,1 burnP6

Binding an existing process …

As long as you know the “ID” (PID) of the process, as mentioned above, you can even bind an existing process to a certain CPU core (or Cores). For that, I’ll use “taskset” in the below format.

taskset -pc 0 7022

Note that we’ve added “p” which indicates PID (process ID).

Again, you’ll have to replace both “0” (CPU Core number) and “7022” (ID of the process). And I’m sure you know how to get the ID of a running process. If you don’t, then once the process is running, if you use the Unity desktop, then you can use the Gnome System Monitor and under the “Processes” tab, highlight the process and then look under the “ID” value, which is the PID of that particular process (as shown below).

If you don’t use a desktop environment, then enter the below command in your Terminal window and it should printout a list of all the running processes in your OS and from that list under the “PID” value, you can find a process and its ID as well.

ps -A

How do I know if it’s working ?

Well, if your process is CPU intensive (otherwise it doesn’t make a whole lot of sense to change the CPU Affinity value anyway) and if you use a desktop environment, then the built-in system monitor should be able to show you the CPU load for each individual CPU core, and if a particular Core (or Cores) have a higher CPU usage readings, then you know it’s working 😉 (as shown in the first screenshot).

But if you’d still prefer the command-line, then you can use another built in system monitor called “top” for that as well. Again, after running the process and changing its “Affinity mask”, use the below command in your Terminal.

top

Now when it’s running, press the key “1” of your Keyboard and the output should change and display the load for each individual cores as shown below.

That’s it!.

Again, for most users, this is almost always unnecessary. But if you’re some like a system administrator who wants a certain process to be executed by only using a certain number of CPU cores of your PC (in Ubuntu Linux), then I hope that this was a bit helpful. Enjoy!.

19 thoughts on “How to Assign a Process to a Certain CPU Core in Ubuntu Linux

  1. What am I supposed to do if I have a process that cannot be started via a command line, but I need it to only run on one core?

    • Quite simple Aaron. As I mentioned in the article, you don’t have to start a process from scratch with ‘taskset’ for it to work, you can ‘bind’ any existing processes (as long as you have the permission to do so) to a certain core (s) with it as well. The process is simple.

      1. First find out the process ID.

      2. Then use ‘taskset’ with the ‘-p’ argument, followed by the number of cores you want that process to be bound to, and then it too followed by the processes ID.

      For instance, if I wanted to bind a process ID called ‘789’ to my first and second cores, then I’d use ‘taskset’ in the below manner:

      taskset -p 01 789

      It’s that simple!. You can read all this in its manual or by reading the more simplified ‘help’ page, both of which can be read using the below commands, respectively:

      man taskset

      taskset --help

      Hope that helped.

  2. For me it seems to only partially work. I have 4 cores and 7 CPU intensive tasks. I made the affinity of tasks so the cores 0, 1 and 2 have 2 tasks but core 3 – just 1 task. Also I changed the priority of tasks so each core has no CPU intensive tasks with the same priority. In System Monitor in the column for CPU usage I expected to see 4 numbers close to 25. However, what I did see was 1 number fluctuating around 20, and 6 numbers approximately equal to 12.

  3. need help…..i have a filter program written in C…..i want to assign it to a perticular core….how would i do this??

  4. I have 8 cores and I want to assign a task on CPU7. Initially, it runs the task on CPU7, but after few seconds, it changes the affinity to CPU0 or CPU1. How can I prevent scheduler from changing the affinity of task?

Leave a Comment