可能重复:
如何设置特定pthread的CPU亲和力?
Linux 有没有一种方法可以禁用除一个进程之外的所有进程的一个核心?我希望只为我的流程保留一个核心。
预期行为如下:
是的,有。您想要创建两个 cpuset,一个包含独立的 CPU,另一个包含所有其余的 CPU。将您的特殊进程分配给隔离的 cpuset,并将所有其余进程分配给其他 cpuset。
这是一个简单的示例脚本,可以做到这一点:
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
现在启动你的进程并找出它的 PID 并开始:
/bin/echo $PID > /cpuset/rt/tasks
如果您想恢复这些更改,只需重新启动系统或执行以下操作:
# 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
这是手册页:http://www.kernel.org/doc/man-pages/online/pages/man7/cpuset.7.html
还有更复杂的 shell 包装器可以帮助您自动化执行此操作,例如 cset。请参阅: http://web.archive.org/web/20120428093126/http://www.suse.com/documentation/slerte_11/slerte_tutorial/data/slerte_tutorial.html