How do I bind the CPU process in Ubuntu?

Source: Internet
Author: User
Tags bind

Now the technology is constantly developing and the trend of multiple CPUs is getting bigger. Sometimes in order to better operate the machine, you need to bind a process to the specific CPU. You may not understand what it means to bind a process to a CPU, but simply process/thread and CPU binding, the most intuitive benefit is to improve the CPU cache hit rate, thereby reducing memory access loss, improve the speed of the program, the ordinary process into a core process. The following small series is like the introduction of how to bind the CPU process in Ubuntu, Ubuntu (diagram) is a desktop application-oriented Linux operating system, and small knitting together.

How to bind the CPU process in Ubuntu

TASKSET-CP CPU ID | CPU IDs "Process ID"

Here is a simple example to illustrate how to do this.

1. Sample code with CPU utilization rate up to 100%:

Class Test {

public static void Main (String args[]) {

int i = 0;

while (true) {

i++;

}

}

}

2. Compile and run the sample code above

# 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 would be installed:

Htop

0 upgraded, 1 newly installed, 0 to-remove and not upgraded.

Need to get 66.9 KB of archives.

After this operation, 183 KB of additional disk space would 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, execute the command:

# Htop

The above view can see that the utilization of CPU2 is up to 100%, and that the process may be allocated to other CPU cores, and this allocation is variable.

4. Process Binding CPU Core

Run the following command to permanently allocate this Java process (process ID number 26502) to the 5th CPU core (The CPU core number is calculated from 0, so serial number 4 refers to the 5th CPU core)

# TASKSET-CP 5 26531

PID 26531 ' s current affinity list:0-7

PID 26531 ' s new affinity List:5

From the view above, you can see that the 6th CPU core utilization is 100%.

With the CPU kernel of multiple, such binding method is the same, no matter which CPU core can be bound to start the same effect, I believe we all pursue the high speed of running, quickly to learn how to bind the CPU process!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.