Last active
August 29, 2015 14:16
-
-
Save dvdvck/66d80be2faed118525cc to your computer and use it in GitHub Desktop.
subprocess basic control job
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#Lanza subprocesos en segundo plano, manteniendo un umbral de almenos | |
#4 procesos en paralelo. | |
todo=15 | |
echo "`date +%M:%S` Starting PARENT $$ BASHPID: $BASHPID" | |
while [ $todo -gt 0 ]; do | |
rand=$[$RANDOM % 10] | |
#simulando que terminan antes entrar a background | |
#rand=0 | |
echo "`date +%M:%S` ALLOCATE job number $todo after $rand seconds" | |
(sleep $rand; echo "`date +%M:%S` DEALLOCATE number job $todo in BASHPID: $BASHPID") & | |
children=(`jobs -p`) | |
echo "`date +%M:%S` Number of children running: ${#children[@]}" | |
if [ ${#children[@]} -gt 4 ]; then | |
echo "`date +%M:%S` Waiting for an available slot ..." | |
wait -n ${children[@]} | |
fi | |
todo=$[$todo - 1] | |
done | |
echo "`date +%M:%S` waiting for the rest of children ${children[@]}" | |
wait | |
echo "`date +%M:%S` done :D" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment