Server Fault is a question and answer site for system and network administrators. 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

I know that taskset can be used to force a process to run on a particular cpu.

How can I force tomcat to run on a particular CPU?

I then want to run apache bench on a seperate CPU.


I appreciated your suggestions on NOT to do this, but I'm really looking on HOW to do it :)

This post shows how:

Just a little confused on how you know which CPU, it is hex based?

share|improve this question
They will by default run as parallel as possible. You will not get any performance benefit by assigning affinity manually. You may see performance penalties depending on the specifics of your circumstances. – Chris S Dec 21 '11 at 16:12
We had a very similar question to this in the past - I went on a link hunt and couldn't find it… – voretaq7 Dec 21 '11 at 16:36
maybe not, see:… – codecompleting Dec 21 '11 at 16:49
@codecompleting "The server has a novel internal architecture to me that seeks to never voluntarily block threads or cause context switches" reads to me as "The server is like a teenager: It never wants to get off the phone^WCPU" -- A very special case where taskseting it to bind it to a single CPU may provide a performance boost. – voretaq7 Dec 21 '11 at 16:53

David showed you where the bazooka is, but if you use it you will almost certainly be shooting yourself in the foot (see my comment).

As a general rule you should accept that you are not smarter than the task scheduler -- A lot of very bright people worked on it, and they did a really good job of making sure it crams the maximum number of computations into the minimum amount of time. Unless you have very specific and well-defined reasons for messing with it (as in "I know exactly what is going on inside the kernel, and why, and I want it to do this instead") you should trust the scheduler and let the it do its thing.

share|improve this answer
agreed, but I'm just experiementing as there are cases when things don't work as you would have expected, see:… – codecompleting Dec 21 '11 at 16:50
you keep posting that one source, any other supporting evidence whatsoever? seems like an old wives tale kind of tweak to me – JamesRyan Dec 21 '11 at 17:08

See the taskset command. Note that this will almost always have significant negative effects on performance.

share|improve this answer
Note that unless I misunderstand its purpose taskset forces a specific process to run on a specific CPU -- It does not force other processes to stay off that CPU (so if the scheduler sees you're not using it and gives it to someone else for a few quick calculations you'll have to wait until they're done before you can have it back - Almost certainly not what you want) – voretaq7 Dec 21 '11 at 16:33
Almost certainly what you do want. The scheduler's not stupid. It won't do things unless it has a good reason. – David Schwartz Dec 21 '11 at 17:44
Every time I've seen/heard this question the intent has been "Do not let other processes run on CPU1, reserve it exclusively for me." - @codecompleting seems to be asking for that behavior, and AFAIK taskset doesn't do that. If you can show me how to do that with taskset I'll be happy to be proven wrong (and I'd find that functionality useful myself!) – voretaq7 Dec 21 '11 at 18:03
Processor affinity is inherited. If you set the processor affinity for all current tasks to exclude a particular core, new tasks will inherit the previous affinity. And I see nothing in the question that suggests he's asking for this kind of behavior, and the vast majority of times, that's not what you want. (Why take away an option the scheduler would only use if it had a good reason to?) – David Schwartz Dec 21 '11 at 18:31
hmm, excluding the core for all non-permitted tasks is certainly an option for the "just for me" behavior. Ugly, but workable. I'll let the OP clarify their intent if they wish to, but the purpose of "reserving" a core is exactly that: Ensuring it is always available for the target application. This is sometimes done in hypervisors to ensure that a specific VM always has CPU available without having to wait while others are evicted. (I'm not saying it's something you SHOULD do on a unix system - I think it's boneheaded. I'm just giving the typical argument for doing it) – voretaq7 Dec 21 '11 at 18:43

It appears that unless you plan on doing one of the following:

Making your own task scheduler. having apache use one primarily will everyone else uses it in turn as well.

I would suggest just getting a dedicated server for it. short of doing super in depth changes to the kernel or a headache of bypassing the scheduler for all the system tasks with taskset.

share|improve this answer

According to the manual page for taskset, the CPUs are specified as a bitmask. So CPU0 is 0x01, CPU1 is 0x02, CPU2 is 0x04, CPU3 is 0x08. You can add them together if you want a process to run on more than one cpu. (e.g. 0x03 is CPU0 and CPU1)

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.