Ubuntu系统进程绑定CPU核

简介: 版权声明:本文为博主chszs的原创文章,未经博主允许不得转载。 https://blog.csdn.net/chszs/article/details/47278019 Ubuntu系统进程绑定CPU核作者:chszs,版权所有,未经同意,不得转载。
版权声明:本文为博主chszs的原创文章,未经博主允许不得转载。 https://blog.csdn.net/chszs/article/details/47278019

Ubuntu系统进程绑定CPU核

作者:chszs,版权所有,未经同意,不得转载。博主主页:http://blog.csdn.net/chszs

本文讲述如何在Ubuntu系统中,把指定的进程绑定到指定的CPU核运行。而通常是由操作系统负责管理进程和线程的调度,但是这种情况下是不清楚由哪个CPU核运行你的进程,因为操作系统的调度是基于资源的可用性进行判断的。

可以这样,把指定的CPU核绑定到你的进程。

taskset -cp <CPU ID | CPU IDs> <Process ID>

下面用一个简单的例子来说明怎样做到。

1. CPU利用率达100%的样例代码:

class Test {
    public static void main(String args[]) {
        int i = 0;
        while (true) {
            i++;
        }
    }
}

2. 编译并运行上面的样例代码

# javac Test.java
# java Test &
[1] 26531

3. 使用htop命令查看CPU的利用率

如果未安装htop工具,执行下面的命令:

# 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/ 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) ...

安装完成后,执行命令:

# htop

这里写图片描述
上面的视图可以看到,CPU2的利用率达到100%,且这个进程有可能被分配到其它CPU核上运行,这个分配是不定的。

4. 进程绑定CPU核

运行以下命令,把此Java进程(进程ID号为26502)永久的分配给5号CPU核(CPU核号从0开始计算,因此序号4指的是5号CPU核)

# taskset -cp 5 26531
pid 26531's current affinity list: 0-7
pid 26531's new affinity list: 5

这里写图片描述
从上面的视图中可以看到6号CPU核的利用率为100%。

目录
相关文章
|
24天前
|
Ubuntu Windows
【Ubuntu/Arm】Ubuntu 系统如何链接有线网络(非虚拟机)?
【Ubuntu/Arm】Ubuntu 系统如何链接有线网络(非虚拟机)?
|
29天前
|
Ubuntu NoSQL 关系型数据库
Ubuntu系统下安装常用软件
Ubuntu系统下安装常用软件
59 0
Ubuntu系统下安装常用软件
|
2月前
|
缓存 运维 Linux
Linux系统调优详解(三)——CPU状态查看相关命令
Linux系统调优详解(三)——CPU状态查看相关命令
42 1
|
2月前
|
运维 Linux
Linux系统调优详解(二)——CPU负载查看相关命令
Linux系统调优详解(二)——CPU负载查看相关命令
51 10
|
2月前
|
Ubuntu
如何在 Ubuntu 中创建网络绑定和桥接?
如何在 Ubuntu 中创建网络绑定和桥接?
89 0
如何在 Ubuntu 中创建网络绑定和桥接?
|
3月前
|
Ubuntu 安全 网络安全
百度搜索:蓝易云【Ubuntu系统SSH服务端配置】
现在,你已经成功在Ubuntu系统上配置了SSH服务端。这将允许其他计算机通过SSH协议连接到你的Ubuntu系统,并进行远程管理和操作。请注意,远程访问有安全风险,建议在生产环境中采取必要的安全措施来保护系统。
40 3
|
3月前
|
Linux
|
3月前
|
Ubuntu 网络安全
百度搜索:蓝易云【ubuntu系统ufw开放端口教程】
现在,你已经成功在Ubuntu系统中使用ufw开放了指定的端口。请确保只开放必要的端口,并注意网络安全。
89 3
|
3月前
|
存储 Ubuntu
百度搜索:蓝易云【在ubuntu系统下安装配置onedrive步骤】
现在,你已经成功在Ubuntu系统下安装和配置了OneDrive,可以使用该工具与OneDrive云端存储进行同步。
41 0
|
3月前
|
Ubuntu
百度搜索:蓝易云【Ubuntu系统apt添加第三方PPA源教程】
现在,你已经成功在Ubuntu系统中添加了第三方PPA源。系统将会从该源获取更新和软件包。请注意,添加第三方PPA源时要确保其来源可信,以免引入不安全的软件包。
40 5