Linux 一个专用于我的流程的核心
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9072060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
One core exclusively for my process
提问by Kornel Szymkiewicz
Possible Duplicate:
how to set CPU affinity of a particular pthread?
可能的重复:
如何设置特定 pthread 的 CPU 关联?
Is there a way in Linux to disable one core for all processes except one process? I would like to have one core reserved only and only for my process.
Linux 中是否有一种方法可以为除一个进程之外的所有进程禁用一个内核?我想只保留一个内核,并且只用于我的进程。
Expected behavior is as follows:
预期行为如下:
- Processes which will be spawned after my process, should not see this core and use the others.
- When my process is spawned, all processes which are utilizing this core, should be switched to other cores.
- 在我的进程之后产生的进程不应该看到这个核心并使用其他的。
- 当我的进程产生时,所有使用这个内核的进程都应该切换到其他内核。
回答by shodanex
You can have a look at this lwn articlefor a discussion of kernel solution to this problem.
您可以查看这篇lwn 文章,以讨论针对此问题的内核解决方案。
回答by gby
Yes, there is. You want to create two cpusets, one with your isolated CPU and the other with all the rest of the CPUs. Assign your special process to the isolated cpuset and all the rest of the processes to the other cpuset.
就在这里。您想创建两个 cpuset,一个用于隔离的 CPU,另一个用于所有其余的 CPU。将您的特殊进程分配给隔离的 cpuset,并将所有其余进程分配给另一个 cpuset。
Here is a simple example script that will do it:
这是一个简单的示例脚本,可以执行此操作:
mkdir /cpuset
mount -t cpuset none /cpuset/
cd /cpuset
mkdir sys # create sub-cpuset for system processes
/bin/echo 0-2 > sys/cpuset.cpus # assign cpus (cores) 0-2 to this set
# adjust if you have more/less cores
/bin/echo 1 > sys/cpuset.cpu_exclusive
/bin/echo 0 > sys/cpuset.mems
mkdir rt # create sub-cpuset for my process
/bin/echo 3 > rt/cpuset.cpus # assign cpu (core) 3 to this cpuset
# adjust this to number of cores-1
/bin/echo 1 > rt/cpuset.cpu_exclusive
/bin/echo 0 > rt/cpuset.mems
/bin/echo 0 > rt/cpuset.sched_load_balance
/bin/echo 1 > rt/cpuset.mem_hardwall
# move all processes from the default cpuset to the sys-cpuset
for T in `cat tasks`; do echo "Moving " $T; /bin/echo $T > sys/tasks; done
Now start your process and find out its PID and go:
现在开始你的过程并找出它的PID然后去:
/bin/echo $PID > /cpuset/rt/tasks
If you want to revert these changes, just restart your system or do:
如果要还原这些更改,只需重新启动系统或执行以下操作:
# move tasks back from sys-cpuset to root cpuset
for T in `cat /cpuset/sys/tasks`; do echo "Moving " $T; /bin/echo $T > /cpuset/tasks; done
# remove sys-cpuset
rmdir /cpuset/sys
# move tasks back from rt-cpuset to root cpuset
for T in `cat /cpuset/rt/tasks`; do echo "Moving " $T; /bin/echo $T > /cpuset/tasks; done
# remove rt-cpuset
rmdir /cpuset/rt
# unmount and remove /cpuset
umount /cpuset
rmdir /cpuset
Here is the man page: http://www.kernel.org/doc/man-pages/online/pages/man7/cpuset.7.html
这是手册页:http: //www.kernel.org/doc/man-pages/online/pages/man7/cpuset.7.html
There are also more complicated shell wrappers that can help you automate this, such as cset. See: http://web.archive.org/web/20120428093126/http://www.suse.com/documentation/slerte_11/slerte_tutorial/data/slerte_tutorial.html
还有更复杂的外壳包装器可以帮助您自动执行此操作,例如 cset。请参阅:http: //web.archive.org/web/20120428093126/http: //www.suse.com/documentation/slerte_11/slerte_tutorial/data/slerte_tutorial.html