1753454 Members
6155 Online
108794 Solutions
New Discussion юеВ

Script to hog CPU

 
SOLVED
Go to solution
Mike Boswell_1
Frequent Advisor

Script to hog CPU

I want to fully utilize a single CPU to testing. Does anyone have a script of command that I can run that will fully utilize a single CPU for longer than 15 minutes w/ the same PID?

Thanks,
Mike
5 REPLIES 5
Murat SULUHAN
Honored Contributor
Solution

Re: Script to hog CPU

Hi

An inadvisable method like try to gzip very very big file with mpsched like

#mpsched -c 1 gzip veryBigFile

1 --> is CPU1, other CPU's will be idle

Best Regards
Murat Suluhan
Murat Suluhan
James R. Ferguson
Acclaimed Contributor

Re: Script to hog CPU

Hi Mike:

#!/usr/bin/sh
while true
do
:
done

...this will run endlessly until killed. The colon ":" is a syntatically correct null statement.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Script to hog CPU

Hi (again) Mike:

To add, if you want to run for a proscribed period of time, and have the process self-terminate, you can do:

# perl -e '$t=shift||60;$SIG{ALRM}=sub {exit};alarm $t;1 while {}'

By default this will run for 60-seconds. Pass any number of seconds as an argument to run shorter or longer before self-destructing.

Regards!

...JRF...
Mike Boswell_1
Frequent Advisor

Re: Script to hog CPU

10 point to all for being so friggin' fast.

Thank you ! I used the while loop because it was basically what I 1st attempted but the ":" made the difference.
Bill Hassell
Honored Contributor

Re: Script to hog CPU

Using the shell loop as a process, this script will schedule multiple CPU hogs, wait until terminated and then automatically kill all of them. You can saturate any size system. Note that like most Unix systems, a fully saturated system still responds very fast. Programs that do nothing but compute degrade to the worst priority so that all I/O (disk, tape, keyboard, etc) will still be processed. By using this scheduler, you can create very high load factors (runqueues).

The cpu-bound script is the same as the one James showed:

#!/usr/bin/sh
while :
do
:
done

Call the above script: cpu-bound

The script below will schedule up to 128 copies of cpu-bound. Just use CTRL-C to terminate the multi-cpu script and all the children will terminate.

#!/usr/bin/sh

# multi-cpubound

# Usage: multi-cpubound 10 script/prog_name [ "param(s)" [ nice or toggle ] ]

# Run a process or script multiple times with optional 50:50 nice
# Terminate multi-start to terminate all child processes

set -u
export PATH=/usr/bin
MYNAME=${0##*/}
SANEQTY=128

# F U N C T I O N S

function Usage
{
[ $# -gt 0 ] && echo "\n$@"
cat << EOF

Usage: $MYNAME QTY PROGNAME [ PROG_PARMS ]
where QTY=1 to $SANEQTY,
normal = all copies std priority
nice = all copies=nice,
toggle to alternate each copy
PROGNAME is program or script name
PARMS = program parameters or ""

EOF
exit 1
}

# M A I N P R O G R A M

# parameter handling/checking

[ $# -lt 3 ] && Usage "Requires 3 or more parameters"
QTY=$1

[ $QTY -lt 1 ] && QTY=1
[ $QTY -gt $SANEQTY ] && QTY=$SANEQTY

# find out the path used to start this prog
# if the PROGNAME has no directory path,
# use this program's path.

MYPATH=$(dirname $0)
PROGNAME="$MYPATH/$3"

if [ ! -x $PROGNAME ]
then
echo "\n$MYNAME: prog/script \"$PROGNAME\" not found\n"
exit
fi

# If parm#2 = normal, all procs=std pri
# If parm#2 = nice, all procs=nice
# If parm#2 = toggle, alternate procs start at nice

case $2 in
nice ) NICE=true
TOGGLE=false
;;
toggle ) NICE=false
TOGGLE=true
;;
normal ) NICE=false
TOGGLE=false
;;
* ) Usage "Requires 1 of: normal nice toggle"
esac

[ $# -gt 3 ] && PARMS="$@" || PARMS=""


COPY=0
NICE=false

# start the processes

echo "\nStarting $QTY copies of $PROGNAME $PARMS"
echo "$MYNAME PID = $$, NICE=$NICE toggle=$TOGGLE"

while [ $COPY -lt $QTY ]
do
let COPY=$COPY+1
if $TOGGLE
then
if [ $NICE = true ]
then
./$PROGNAME "$PARMS" &
NICE=false
else
nice ./$PROGNAME "$PARMS" &
NICE=true
fi
else
./$PROGNAME "$PARMS" &
fi
PIDS[$COPY]=$!
done

echo "Child PIDs = ${PIDS[@]}"
echo
trap 'echo "trap detected, stopping ${PIDS[@]}";kill ${PIDS[@]};exit' 1 2 3 15

# do nothing forever until terminated
while :
do
sleep 100
done


Bill Hassell, sysadmin