Linux Administrator

How to Run Process or Program on Specific CPU Cores in Linux

cpu
mm
Written by Santosh Prasad

Multi-core CPUs becoming popular these days on server-grade hardware and end-user desktop PCs, Laptops. Suppose we have a multi-core processor, whenever we run a program (process) it may be execute using all the available cores or if it does not need more CPU usage then single core is more capable to handle it. But still while executing, it may be switch between the multiple CPU cores by Operating System. It is almost best for letting the Operating System to handle process. But smart user have the ability to make a certain process to execute by only using a single CPU core. It has a slight benefit in server performance. Because in a multi-core CPU environment all individual CPUs have separate cache for each CPU core which is only accessible by that CPU ( CPU cache know as L1, L2, and L3)

In this tutorial I am going to describe how we can run a process or program on specific CPU cores in Linux.

To run a process or program on specific CPU cores you, you can use taskset. It is a command line tool for setting a process CPU affinity in Linux.

Install taskset

Taskset tools is part of util-linux package in Linux, and it comes with pre-installed in most Linux distros by default. If it is not available by default on your Linux machine you can install it using below command.

Install taskset on Ubuntu and Debian system

Run the below command to install taskset package on Ubuntu and Debian system.

$ sudo apt-get install util-linux

Install taskset on CentOS,RHEL and Fedora system

# yum install util-linux

Know the CPU affinity of a running process

Follow the below command to know the CPU affinity of a process with process ID of 5850.

# taskset -p 5850
pid 5850’s current affinity mask: ff

In above example, the affinity corresponds to “11111111” in binary format. It means the process can run on any eight cores ( from 0 to 7 ).

Instead of a bitmask, tasktest can show CPU affinity as a list of processors which is easy to read. Run the below command with -c option to use this.

# taskset -cp 5850
pid 5850's current affinity list: 0-7

Assign Running Process To Particular CPU Cores

Follow the below command to pin a running process to particular CPU cores.

# taskset -p <COREMASK> <PID>
# taskset -cp <CORE-LIST> <PID>

For example, assign a process to CPU core 0 and 4.

# taskset -p 0x11 6075

pid 6075's current affinity mask: ff
pid 6075's new affinity mask: 11

OR

# taskset -cp 0,4 6075

Run a Program on Specific CPU cores

You can also launch a new program as assign to specific CPU cores with taskset.

# taskset <COREMASK> <EXECUTABLE>

For example, launch google-chrome on CPU core 0.

# taskset 0x1 google-chrome

Dedicate a Whole CPU core to a Particular Program

Using taskset we can assign particular program to certain CPUs, It does’t mean that no other process or programs will be scheduled on those CPUs. We can use “isolcpus” kernel parameter to dedicate a whole CPU core to a particular program. Using kernel parameter we can reserve the CPU core during boot.

Update GRUB Configuration File to Dedicate a Whole CPU Core to a Particular Program

You will need to update the grub configuration file to dedicate a whole CPU cores to particular program. Then Linux scheduler will not schedule any other regular process on the reserved CPU cores. Update grub configuration file with below content.

GRUB_CMDLINE_LINUX_DEFAULT="cpuidle.off=1 idle=poll isolcpus=0,1 nohz_full=5 maxcpus=6"

Above :

cpuidle.off=1 :- Do not make cpu idle
isolcpus=0,1 :- Core 0 and 1 are isolate (reserved)
maxcpus=6 :- Use only 6 cores out of 8 cores of system
idle=poll :- Poll forces a polling idle loop

I hope this article will help to to run process or program on specific CPU cores in Linux. Read our another articles Googler – Command Line Google Search On Linux System and Top 5 Web Based Linux Monitoring Tools. If you have any queries and problem please comment in comment section or you can also ask your question.

Thanks:)

About the author

mm

Santosh Prasad

Hi! I'm Santosh and I'm here to post some cool article for you. If you have any query and suggestion please comment in comment section.

Leave a Comment