#!/bin/bash


CPU=`cat /proc/cpuinfo | grep processor | wc -l`

while read PID
do

  # Wait until number of openssl processes goes bellow number of
  # CPU. This method isn't very precise (because key-gen2.sh do not
  # execute only openssl)but easy to implenet. Second while is trying to 

  while [ `ps uax |grep openssl  |grep -v grep | wc -l` -gt $CPU ]; do sleep .5; done

  # Check if load isn't too high
  while [ `cat /proc/loadavg | cut -d " " -f 1 | sed "s/\..*//"` -gt $CPU ]; do sleep .5; done

  bash key-gen2.sh $PID 4096 &

  # To give script chance exec openssl process
  #sleep .3;
done

