2004-03-15 13:48:46 +00:00
|
|
|
#!/bin/bash
|
2005-08-28 18:12:12 +00:00
|
|
|
# parent.sh
|
|
|
|
# Running multiple processes on an SMP box.
|
|
|
|
# Author: Tedman Eng
|
2004-03-15 13:48:46 +00:00
|
|
|
|
2005-08-28 18:12:12 +00:00
|
|
|
# This is the first of two scripts,
|
|
|
|
#+ both of which must be present in the current working directory.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LIMIT=$1 # Total number of process to start
|
|
|
|
NUMPROC=4 # Number of concurrent threads (forks?)
|
|
|
|
PROCID=1 # Starting Process ID
|
|
|
|
echo "My PID is $$"
|
|
|
|
|
|
|
|
function start_thread() {
|
|
|
|
if [ $PROCID -le $LIMIT ] ; then
|
2014-07-05 13:38:40 +00:00
|
|
|
./child.sh $PROCID&
|
2005-08-28 18:12:12 +00:00
|
|
|
let "PROCID++"
|
|
|
|
else
|
|
|
|
echo "Limit reached."
|
|
|
|
wait
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
while [ "$NUMPROC" -gt 0 ]; do
|
|
|
|
start_thread;
|
|
|
|
let "NUMPROC--"
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
|
|
while true
|
|
|
|
do
|
|
|
|
|
|
|
|
trap "start_thread" SIGRTMIN
|
|
|
|
|
|
|
|
done
|
|
|
|
|
|
|
|
exit 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ======== Second script follows ========
|
|
|
|
|
|
|
|
|
|
|
|
#!/bin/bash
|
|
|
|
# child.sh
|
|
|
|
# Running multiple processes on an SMP box.
|
|
|
|
# This script is called by parent.sh.
|
|
|
|
# Author: Tedman Eng
|
|
|
|
|
|
|
|
temp=$RANDOM
|
|
|
|
index=$1
|
|
|
|
shift
|
|
|
|
let "temp %= 5"
|
|
|
|
let "temp += 4"
|
|
|
|
echo "Starting $index Time:$temp" "$@"
|
|
|
|
sleep ${temp}
|
|
|
|
echo "Ending $index"
|
|
|
|
kill -s SIGRTMIN $PPID
|
|
|
|
|
|
|
|
exit 0
|
|
|
|
|
|
|
|
|
|
|
|
# ======================= SCRIPT AUTHOR'S NOTES ======================= #
|
|
|
|
# It's not completely bug free.
|
|
|
|
# I ran it with limit = 500 and after the first few hundred iterations,
|
|
|
|
#+ one of the concurrent threads disappeared!
|
|
|
|
# Not sure if this is collisions from trap signals or something else.
|
2005-10-21 13:31:28 +00:00
|
|
|
# Once the trap is received, there's a brief moment while executing the
|
|
|
|
#+ trap handler but before the next trap is set. During this time, it may
|
|
|
|
#+ be possible to miss a trap signal, thus miss spawning a child process.
|
|
|
|
|
2005-08-28 18:12:12 +00:00
|
|
|
# No doubt someone may spot the bug and will be writing
|
|
|
|
#+ . . . in the future.
|
2005-10-21 13:31:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2005-08-28 18:12:12 +00:00
|
|
|
# ===================================================================== #
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#################################################################
|
|
|
|
# The following is the original script written by Vernia Damiano.
|
|
|
|
# Unfortunately, it doesn't work properly.
|
|
|
|
#################################################################
|
|
|
|
|
|
|
|
#!/bin/bash
|
2004-03-15 13:48:46 +00:00
|
|
|
|
|
|
|
# Must call script with at least one integer parameter
|
|
|
|
#+ (number of concurrent processes).
|
|
|
|
# All other parameters are passed through to the processes started.
|
|
|
|
|
|
|
|
|
|
|
|
INDICE=8 # Total number of process to start
|
|
|
|
TEMPO=5 # Maximum sleep time per process
|
|
|
|
E_BADARGS=65 # No arg(s) passed to script.
|
|
|
|
|
|
|
|
if [ $# -eq 0 ] # Check for at least one argument passed to script.
|
|
|
|
then
|
|
|
|
echo "Usage: `basename $0` number_of_processes [passed params]"
|
|
|
|
exit $E_BADARGS
|
|
|
|
fi
|
|
|
|
|
|
|
|
NUMPROC=$1 # Number of concurrent process
|
|
|
|
shift
|
|
|
|
PARAMETRI=( "$@" ) # Parameters of each process
|
|
|
|
|
|
|
|
function avvia() {
|
2005-08-28 18:12:12 +00:00
|
|
|
local temp
|
|
|
|
local index
|
|
|
|
temp=$RANDOM
|
|
|
|
index=$1
|
|
|
|
shift
|
|
|
|
let "temp %= $TEMPO"
|
|
|
|
let "temp += 1"
|
|
|
|
echo "Starting $index Time:$temp" "$@"
|
|
|
|
sleep ${temp}
|
|
|
|
echo "Ending $index"
|
|
|
|
kill -s SIGRTMIN $$
|
2004-03-15 13:48:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function parti() {
|
2005-08-28 18:12:12 +00:00
|
|
|
if [ $INDICE -gt 0 ] ; then
|
2014-07-05 13:38:40 +00:00
|
|
|
avvia $INDICE "${PARAMETRI[@]}" &
|
2005-08-28 18:12:12 +00:00
|
|
|
let "INDICE--"
|
|
|
|
else
|
|
|
|
trap : SIGRTMIN
|
|
|
|
fi
|
2004-03-15 13:48:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
trap parti SIGRTMIN
|
|
|
|
|
|
|
|
while [ "$NUMPROC" -gt 0 ]; do
|
2005-08-28 18:12:12 +00:00
|
|
|
parti;
|
|
|
|
let "NUMPROC--"
|
2004-03-15 13:48:46 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
wait
|
|
|
|
trap - SIGRTMIN
|
|
|
|
|
|
|
|
exit $?
|
|
|
|
|
2005-02-09 20:53:43 +00:00
|
|
|
: <<SCRIPT_AUTHOR_COMMENTS
|
2004-03-15 13:48:46 +00:00
|
|
|
I had the need to run a program, with specified options, on a number of
|
|
|
|
different files, using a SMP machine. So I thought [I'd] keep running
|
|
|
|
a specified number of processes and start a new one each time . . . one
|
|
|
|
of these terminates.
|
|
|
|
|
|
|
|
The "wait" instruction does not help, since it waits for a given process
|
|
|
|
or *all* process started in background. So I wrote [this] bash script
|
|
|
|
that can do the job, using the "trap" instruction.
|
|
|
|
--Vernia Damiano
|
|
|
|
SCRIPT_AUTHOR_COMMENTS
|