How to Assign a Process to a Specific CPU

Nearly all modern servers come with multiple CPUs or at least a CPU with more than one core. Operating systems see more than one CPU, and the advantage is that you can dedicate a CPU to a particular process, freeing up the other CPUs for less intense work. Done correctly, it will make your server faster and more efficient.

For example, let’s suppose you have a server with a dual core Xeon processor that supports Hyperthreading technology. In total, Linux will recognize four CPUs. If you have a process that is particularly intense and do not want it spreading across all the CPUs, you can use a program called schedutils to assign it to a single CPU.

First, you will need to know the process’ ID (or PID). You can find it by typing:

ps aux | grep process-name

Next, use the command taskset to assign a CPU to the process:

taskset -c 1 -p 83432

This tells CPU one to run the process with the PID 83432.

Keep in mind that when a process changes its PID or is stopped and started again, you will need to set the CPU assignment again. You could even assign processes to the first three CPU and then leave the final one for general tasks. This should improve overall server performance and make your applications run more smoothly.