How to bind CPU process in Ubuntu?

  

The Linux process consists of a core process and a normal process. When the normal process is bound to the Linux system CPU core, the normal process becomes the core process. This article uses Ubuntu as an example to introduce how to bind CPU processes in Ubuntu.

taskset -cp "CPU ID |  CPU IDs "Process ID"

The following is a simple example to illustrate how to do this.

1. Sample code with 100% CPU utilization:

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, execute the following command:

# apt-get install htop

Reading package lists.. Done

Building dependency tree

Reading state information... Done

The following NEW packages will be installed:

htop

0 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/precision/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)...< Br>

Processing triggers for man-db ...

Setting up htop (1.0.1-1)...

After the installation is complete, execute the command:

# htop

the above views can be seen, CPU2 the utilization rate of 100%, and this process is likely to be assigned to other CPU core run, this distribution is uncertain.

4. Process Binding CPU Core

Run the following command to permanently assign this Java process (process ID number 26502) to CPU No. 5 (CPU core number starts from 0) Therefore, the serial number 4 refers to the CPU core No. 5)

# taskset -cp 5 26531

pid 26531‘s current affinity list: 0-7

pid 26531’ s new affinity list: 5

from the above you can see the view number 6 CPU core utilization as 100%.

The above is how to bind the CPU process in Ubuntu, of course, some CPU cores may be more than one, but no matter which core effect is bound to the same.

Copyright © Windows knowledge All Rights Reserved