taskset for CPU affinity

SMP operating systems have choices when it comes to scheduling processes: a new or newly rescheduled process can run on any available cpu. However, while it shouldn't matter where a new process runs, an existing process should go back to the same cpu it was running on simply because the cpu may still be caching data that belongs to that process. This is particularly apt to be true if the process is a thread: the other threads in the same program are very likely to have cpu cache of interest to their brethren (though obviously this also diminishes the performance gain that might be seen from multithreading) . For these reasons, scheduling algorithms pay attention to cpu affinity and try to keep it constant.

It is possible to force a process to run only on a certain cpu. There are Linux system calls (sched_setaffinity and sched_getaffinity) and a command line "taskset".

Most of us probably don't need to bother with being heavy handed here. The Linux scheduler probably does a better job assigning processes to cpu's than we would. There may be conditions where you want to effectively dedicate a cpu to a certain process. Usually that means forcing all other processes to another cpu to prevent them from migrating to the cpu you want to keep dedicate. That's not too hard if we are only dealing with two cpu's because processes inherit affinity from their parants. So if you forced "init" to bind to cpu 0, all of its children would inherit that restriction and wouldn't use cpu one unless specifically set. You'd then set your "special" process to use cpu 1. This only gets a little more complicated with more than two processors; available cpu's are just a bit mask, so you would bind init to use more than one cpu but not the one you want to keep clear.

Got something to add? Send me email.

(OLDER) <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> taskset for CPU affinity


Increase ad revenue 50-250% with Ezoic

More Articles by

Find me on Google+

© Tony Lawrence

Tue Feb 16 19:42:30 2010: 8095   RichardJose


I am running a Xen Virtual Machine on an 8-core machine. I want to bind guest vm's on different cores. Can you tell me is it possible to do that using taskset or cpuset or any of the other things you mentioned about affinity here?

Tue Feb 16 19:46:53 2010: 8096   TonyLawrence


I don't know why not - did you try?

Fri Jul 2 10:33:08 2010: 8772   anonymous


If a process is assigned a CPU to run on, do all child processes spawned by it inherit the cpu affinity?

Fri Jul 2 12:25:19 2010: 8773   TonyLawrence


' So if you forced "init" to bind to cpu 0, all of its children would inherit that restriction and wouldn't use cpu one unless specifically set.'

Mon Jan 9 23:43:45 2012: 10456   anonymous


In qemu virtual machine, I am using taskset -c 0 process1 & taskset -c 1 process2 & taskset -c 2 process3 & taskset -c 3 process4; to simultaneously issue 4 processes and bind them to four cores. However, I find that in-between, some cores (say 1 and 2) do not execute those processes but either idle or do something else. Can you suggest, what could be the reason or way of improvement (making sure, processes don't migrate)?

Tue Jan 10 02:03:16 2012: 10457   TonyLawrence


Are you sure they are migrating? I would expect them to bind, but of course Qemu is also involved here, so maybe it is screwing it up. Beyond my knowledge, sorry.

Kerio Samepage

Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us

Legend has it that every new technology is first used for something related to sex or *redacted*. That seems to be the way of humankind. (Tim Berners-Lee)

Microsoft isn't evil, they just make really crappy operating systems. (Linus Torvalds)

This post tagged: