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%。

最新文章

  1. Windows 版本的iTunes 修改iPhone的备份路径
  2. Web 前端开发人员和设计师必读文章推荐【系列二十八】
  3. ANSI Common Lisp Learn
  4. 手机端input[type=date]的时候placeholder不起作用解决方案
  5. [Android Tips] 3. Launch CallLog Activity
  6. Hdu-3487 Splay树,删除,添加,Lazy延迟标记操作
  7. 解决libcrypto.so.0.9.8: cannot open shared object file
  8. wamp不能使用phpmyadmin,提示“You don&#39;t have permission to access /phpmyadmin/ on this server.” 转载
  9. excel导出的集中情况
  10. &lt;php&gt;添加数据注意事项
  11. 【转】有效修改max open files/ulimit -n
  12. Enthought科学计算,数据分析
  13. 成都IT公司面经及公司评价
  14. jenkins+docker 持续构建非docker in docker jenkins docker svn maven
  15. linux磁盘管理系列-LVM的使用
  16. 迭代var()内置函数的时候出现RuntimeError: dictionary changed size during iteration的解决办法
  17. STM32之使用库函数驱动LED灯
  18. RabbitMQ集群搭建和使用
  19. Vue + webpack 项目实践
  20. IIS配置文件上传大小限制

热门文章

  1. 【hdoj_1050】Moving Tables
  2. AC日记——[HNOI2008]越狱 bzoj 1008
  3. 25,Spark Sort-Based Shuffle内幕彻底解密
  4. java反射(基本知识)
  5. [centos6.5] yum makecache 连接错误的解决办法
  6. POJ 3083 Children of the Candy Corn (DFS + BFS + 模拟)
  7. 前端设计师必须知道的10个重要的CSS技巧
  8. [xsy1515]小学生数学题
  9. [HDU6268]Master of Subgraph
  10. 使用ASP.Net WebAPI构建REST服务(七)——调试工具