Bind the Ubuntu system process to the CPU Core
This article describes how to bind a specified process to a specified CPU core in Ubuntu. The operating system is usually responsible for managing process and thread scheduling, but in this case it is unclear which CPU core is responsible for running your process, because the scheduling of the operating system is determined based on the resource availability.
In this way, bind the specified CPU core to your process.
taskset -cp <CPU ID | CPU IDs> <Process ID>
The following is a simple example to illustrate how to do this.
1. Sample Code with a CPU utilization rate of 100%:
class Test { public static void main(String args[]) { int i = 0; while (true) { i++; } }}
2. Compile and run the above sample code
# javac Test.java# java Test &[1] 26531
3. Use the htop command to view CPU utilization
If the htop tool is not installed, run the following command:
# apt-get install htopReading package lists... DoneBuilding dependency tree Reading state information... DoneThe following NEW packages will be installed: htop0 upgraded, 1 newly installed, 0 to remove and 41 not upgraded.Need to get 66.9 kB of archives.After this operation, 183 kB of additional disk space will be used.Get:1 http://mirrors.163.com/ubuntu/ precise/universe htop amd64 1.0.1-1 [66.9 kB]Fetched 66.9 kB in 0s (163 kB/s)Selecting previously unselected package htop.(Reading database ... 57100 files and directories currently installed.)Unpacking htop (from .../htop_1.0.1-1_amd64.deb) ...Processing triggers for man-db ...Setting up htop (1.0.1-1) ...
After the installation is complete, run the following command:
# htop
The above view shows that the utilization rate of CPU2 reaches 100%, and this process may be allocated to other CPU cores for operation. This allocation is not fixed.
4. process binding CPU Cores
Run the following command to permanently allocate the Java Process (process ID 26502) to CPU core 5 (the CPU core number starts from 0, therefore, serial number 4 refers to CPU core number 5)
# taskset -cp 5 26531pid 26531's current affinity list: 0-7pid 26531's new affinity list: 5
From the preceding view, we can see that the CPU usage of the 6th CPU core is 100%.