bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Running multiple processes using trap and kill -s


From: Sammo
Subject: Re: Running multiple processes using trap and kill -s
Date: Sun, 26 Jun 2005 23:40:56 +1000

Here is the script:

#!/bin/bash
# multiple-processes.sh: Run multiple processes on an SMP box.

# Script written by Vernia Damiano.
# Used with permission.

#  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() {
        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 $$
}

function parti() {
        if [ $INDICE -gt 0 ] ; then
                avvia $INDICE "${PARAMETRI[@]}" &
                let "INDICE--"
        else
                trap : SIGRTMIN
        fi
}

trap parti SIGRTMIN

while [ "$NUMPROC" -gt 0 ]; do
        parti;
        let "NUMPROC--"
done

wait
trap - SIGRTMIN

exit $?

: <<SCRIPT_AUTHOR_COMMENTS
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

On 6/26/05, Sammo <sammo2828@gmail.com> wrote:
> 2.05b.0(1)-release
> 
> I seem to be having problems with one of the examples in the Advanced
> Bash Scripting Guide [1]. The script is supposed to start a specified
> number of processes, and when each process finishes, it sends a trap
> signal, which starts a new process. I think the script is correct, but
> it's not working as expected.
> 
> This is what is happening: The first process that finishes sends a
> trap signal to $$, and a new process is started (as expected). But
> when subsequent processes try to send the trap signal, they can't find
> the process. Also, seems that the "wait" command is only waiting until
> the first process finishes, instead of waiting for all of them to
> finish.
> 
> Well, here is the output:
> 
> $ ./multiple-processes.sh 3
> Starting 8 Time:3
> Starting 7 Time:1
> Starting 6 Time:4
> Ending 7
> Starting 5 Time:4
> 
> $ Ending 8
> ./multiple-processes.sh: line 37: kill: (3944) - No such process
> Ending 6
> ./multiple-processes.sh: line 37: kill: (3944) - No such process
> Ending 5
> ./multiple-processes.sh: line 37: kill: (3944) - No such process
> 
> Please, can somebody help?!
> 
> References:
> 
> [1] Example 29-8. Running multiple processes (on an SMP box),
> http://www.tldp.org/LDP/abs/html/debugging.html#MULTIPLEPROC
>




reply via email to

[Prev in Thread] Current Thread [Next in Thread]