Linux Setting processor affinity for a certain task or process

When you are using SMP (Symmetric MultiProcessing) you might want to override the kernel’s process scheduling and bind a certain process to a specific CPU(s).

Adblock detected 😱

My website is made possible by displaying online advertisements to my visitors. I get it! Ads are annoying but they help keep this website running. It is hard to keep the site running and producing new content when so many people block ads. Please consider donating money to the nixCraft via PayPal/Bitcoin, or become a supporter using Patreon.

But what is CPU affinity?

CPU affinity is nothing but a scheduler property that “bonds” a process to a given set of CPUs on the SMP 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. For example, application such as Oracle (ERP apps) use # of cpusÃ‚ per instance licensed. You can bound Oracle to specific CPU to avoid license problem. This is a really useful on large server having 4 or 8 CPUS

Setting processor affinity for a certain task or process using taskset command

taskset is used to set or retrieve the CPU affinity of a running process given its PID or to launch a new COMMAND with a given CPU affinity. However taskset is not installed by default. You need to install schedutils (Linux scheduler utilities) package.

The CPU affinity is represented as a bitmask, with the lowest order bit corresponding to the first logical CPU and the highest order bit corresponding to the last logical CPU. For example:

0x00000001 is processor #0 (1st processor)

0x00000003 is processors #0 and #1

0x00000004 is processors #2 (3rd processor)

To set the processor affinity of process 13545 to processor #0 (1st processor) type following command:# taskset 0x00000001 -p 13545 If you find a bitmask hard to use, then you can specify a numerical list of processors instead of a bitmask using -c flag:# taskset -c 1 -p 13545 # taskset -c 3,4 -p 13545 Where,

-p : Operate on an existing PID and not launch a new task (default is to launch a new task)

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Your support makes a big difference:

I have a small favor to ask. More people are reading the nixCraft. Many of you block advertising which is your right, and advertising revenues are not sufficient to cover my operating costs. So you can see why I need to ask for your help. The nixCraft takes a lot of my time and hard work to produce. If everyone who reads nixCraft, who likes it, helps fund it, my future would be more secure. You can donate as little as $1 to support nixCraft:

Hiya, thanks for this very interesting subject. I was looking for something like this, to be able to schedule my VMware Workstation 6.0 to run on my HP xw9400 Workstation Dual-Core Opteron (3&4) under RHEL5 (x86-64).

After loading ‘top’ press 1 (one) to switch to SMP view, which will show each CPU usage.

Try assigning CPU intensive program to one of your CPU, and see (in ‘top’) whether the affinity is set or not.

In my system, the program taskset definitely do it’s job. If I don’t use the taskset, the CPU that work on my program is vary from 1 to 4 (I have quad core), when I assign the program to one specific CPU, then only that CPU that has high usage.

this is great – i have a java wrapper on a engine written in C++ that crashes whenever the engine is run on more than 1 processor. since all the newer processors are multi-core, i have been running the OS in uniprocessor mode and basically wasting the other cores! ~Ellis http://www.lightspoke.com

Does the taskset do its job on already running processes. I tried but it didn’t work. First I ran 2 identical processes without taskset. They were homogeneously running on all 8 cpus I had. Now, when I tried assigning 2 cpus to 1 and 6 to another, it didn’t work. I killed the process where I assigned 6 cpus. Still the other was using all 8 cpus.

hi guys, I tried using taskset and schedtool interchangeably, and I noticed that when I’m running virtualbox, which is a cpu intensive process, then I change proc affinity which doesn’t seem to have that big an effect in ‘top’. I change from one proc to another and the output of ‘top’ seems unchanged, while the process is already running. However, if I begin a new cpu intensive process and assign it an affinity before running it, then I can see changes in the output of ‘top’. any clues to this? perhaps my already running virtualbox is too ‘attached’ to its original proc that the linux kernel decides to override/not pay attention to my schedtool/taskset commands?

Actually, it evidently works especially when I set a process’ affinity before running the process. I noticed that while running a cpu or memory intensive task such as Virtual box or VMware, alternating/changing processor affinity doesn’t seem as evident as my previous example. Anyone noticed this? Perhaps it has something to do with the Linux kernel overriding the manual affinity changing since the kernel knows how to better handle processes and it probably knows that there will be performance hits when moving processes such as VMware/Virtualbox from one proc to another (e.g. cache hit rate etc) ?

How you can unbind a process from the selected processor affinity? I mean, if you don’t want keep the affinity on a process all time, how can you unbind the established affinity between the processor and the process? Something like: taskset -c -p Idprocess? or taskset -c * -p Idprocess? or taskset -c 0,1,2,3 -p Idprocess?

What are the key guts needed to make this work? I want to put this into a busybox-based embedded system… where apt-get won’t help me at all. I need to statically get it compiled onto the never-changing initrd…

This was a very helpful post. It solved part of my problems. But I still have another problem: How do I know in each processor a current process is running into. For instance I have a quadcore machine and want to know if the process with pid 999 is running in the cpu 0, 1, 2 or 3. top command shows only the processor average utilization by this pid but don’t show each cpu is this. Taskset let me bound a task to a CPU, but yet i don’t know each cpu this process was previously running into.

kool stuff – it helped me a lot .thankx in advance i am in need of running a application wit the same cofnfig file , but in different processor . even though i run both pairs in different proccessor (as per ur taskset commands) . both are crashin in the sense if i change something in 1 pair it gets reflected in other..can t they be prevented.

Hi Tony,I have a CentOS 5.7 w/ cPanel 11.30 + ngxcinp plugin. I want to move over to CloudLinux + LiteSpeed. What would you suggest me:a) setup a new server with CentOS 6 -> Install cPanel -> Move customers over -> Upgrade to CloudLinux 6 orb) stay with CentOS 5.7 and upgrade to CloudLinux directly from there?I’ve read that theres no upgrade path between RHEL5 -> RHEL6 and as such nor for CentOS or CloudLinux. Which version do you run on your servers? 5.x or 6.x?Thanks in advance

I have two programs. I want to execute both program on different core. I did this by setting the affinity (using taskset command). But the core is changing during next execution. How to permanently assign a specific core to a running program.