#!/bin/bash

nbtest=1000
nbpoint=2000000
nbproc=3

pids=''
for (( j=1; j<=$nbproc; j++ ));
do
  pids[$j]=-1
done

launch_command()
{
    ./rbox $nbpoint > test_so_big_$1.dat
    ./vormesh test_so_big_$1 &
#     sleep 0.1
    pid=$!
    pids[$1]=$pid
#     echo "pid $j --> ${pids[$j]}"
#     echo "$$ $BASH_SUBSHELL $BASHPID"
    echo "--> start test $pid ..."
    wait $pid
    if [[ $? == 0 ]]
      then success $1
      else failed $1     
    fi
}

success()
{
echo "--> test ${pids[$1]} OK"
}

failed()
{
  echo "--> test ${pids[$1]} failed"
  echo "writing file test_so_big_failed_$RANDOM.dat"    
 mv test_so_big_$1.dat test_so_big_failed_$RANDOM.dat
}

for (( count=1; count<=$nbtest; ));
do
  for (( j=1; j<=$nbproc; j++ ));
  do
#     echo "pid $j --> ${pids[$j]}"    
    if (( ${pids[$j]} == -1 ));
    then
#     echo "$$ $BASH_SUBSHELL $BASHPID" 
    launch_command $j &
    count=$((count+1))    
    pids[$j]=$!      
    fi
  done
  for (( j=1; j<=$nbproc; j++ ));
  do
    if ! ps -p ${pids[$j]} -o comm= > /dev/null
    then
    pids[$j]=-1
  fi
  done
  sleep 0.1
done