首页 > 代码库 > linux下模拟CPU占用100%小程序

linux下模拟CPU占用100%小程序

在做一个测试时,需要模拟服务器CPU占用满的情况,在查阅相关资料后,发现网上程序不太好用,

原文在这:http://www.2cto.com/os/201304/202068.html

优化后如下:

#! /bin/sh # filename killcpu.shif [ $# -ne 1 ] ; then  echo "USAGE: $0 <CPUs>|stop"  exit 1;fistop(){ while read LINE  do    kill -9 $LINE    echo "kill $LINE sucessfull"  done < pid.txt cat /dev/null > pid.txt}start(){  echo "u want to cpus is: "$1  for i in `seq $1`do  echo -ne " i=0; while truedoi=i+1; done" | /bin/sh &  pid_array[$i]=$! ;donefor i in "${pid_array[@]}"; do  echo pid is:  $i ;;  echo $i >> pid.txtdone}case $1 in   stop)    stop  ;;   *)   start $1;;esac

运行方式:

启动:./killcpu.sh 1

停止:killcpu.sh stop

linux下模拟CPU占用100%小程序