Ubuntu系统进程绑定CPU核
生活随笔
收集整理的這篇文章主要介紹了
Ubuntu系统进程绑定CPU核
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Ubuntu系統(tǒng)進(jìn)程綁定CPU核
作者:chszs。版權(quán)全部,未經(jīng)允許,不得轉(zhuǎn)載。
博主主頁:http://blog.csdn.net/chszs
本文講述如何在Ubuntu系統(tǒng)中,把指定的進(jìn)程綁定到指定的CPU核執(zhí)行。
而一般是由操作系統(tǒng)負(fù)責(zé)管理進(jìn)程和線程的調(diào)度。可是這樣的情況下是不清楚由哪個(gè)CPU核執(zhí)行你的進(jìn)程,由于操作系統(tǒng)的調(diào)度是基于資源的可用性進(jìn)行推斷的。
能夠這樣。把指定的CPU核綁定到你的進(jìn)程。
taskset -cp <CPU ID | CPU IDs> <Process ID>
以下用一個(gè)簡單的例子來說明如何做到。
1. CPU利用率達(dá)100%的例子代碼:
class Test {
public static void main(String args[]) {
int i = 0;
while (true) {
i++;
}
}
}
2. 編譯并執(zhí)行上面的例子代碼
# javac Test.java
# java Test &
[1] 26531
3. 使用htop命令查看CPU的利用率
假設(shè)未安裝htop工具。執(zhí)行以下的命令:
# 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) ...
安裝完畢后,執(zhí)行命令:
# htop
上面的視圖能夠看到。CPU2的利用率達(dá)到100%,且這個(gè)進(jìn)程有可能被分配到其他CPU核上執(zhí)行,這個(gè)分配是不定的。
4. 進(jìn)程綁定CPU核
執(zhí)行以下命令。把此Java進(jìn)程(進(jìn)程ID號(hào)為26502)永久的分配給5號(hào)CPU核(CPU核號(hào)從0開始計(jì)算。因此序號(hào)4指的是5號(hào)CPU核)
# taskset -cp 5 26531
pid 26531's current affinity list: 0-7
pid 26531's new affinity list: 5
從上面的視圖中能夠看到6號(hào)CPU核的利用率為100%。
總結(jié)
以上是生活随笔為你收集整理的Ubuntu系统进程绑定CPU核的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华为手机怎么设置语言为英文
- 下一篇: 关于 SAP Spartacus OAu