目的:测试服务器的CPU性能,让四个CPU满载。
1)自启动脚本testcpu:
#!/bin/bash
# chkconfig: 2345 98 5
# description: script to start/stop testcpu
case $1 in
start)
cd /work/app
taskset -c 0 ./testcpu &
taskset -c 1 ./testcpu &
taskset -c 2 ./testcpu &
taskset -c 3 ./testcpu &
;;
stop)
pkill testcpu
;;
*)
echo "Usage: $0 (start|stop)"
exit 1
;;
esac
exit 0
第二行中要先查看系统中是否有98,没有就不用改,只要与系统中已存在的不重复即可。可在/etc/rc.d/rc0.d/里面查看。
taskset为指定CPU执行某段程序。
2)升级脚本update.sh
rooter=""
mkdir -p -m 777 $rooter/work/app
cp -f ./cfg/testcpu $rooter/etc/init.d/
cp -f ./testcpu $rooter/work/app/
chmod 777 -R $rooter/work/
chkconfig iptables off
setenforce 0
chkconfig --level 235 testcpu on
echo "System will reboot in 5 seconds..."
sleep 5
reboot
说明:update.sh、testcpu可执行程序在同一级目录,testcpu自启脚本在同级目录cfg文件夹下。
3)c程序testcpu.c
int i = 0;
while(1)
{
for(i=0; i<1000000000; i++)
{
if(i == 1000000) i = 0;
}
}
编译:gcc testcpu.c -o testcpu
执行update.sh即可让程序开机自启且四个CPU满载。