Skip to content

Instantly share code, notes, and snippets.

@jcppkkk
Created March 26, 2012 12:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcppkkk/2204861 to your computer and use it in GitHub Desktop.
Save jcppkkk/2204861 to your computer and use it in GitHub Desktop.
#!/bin/bash
# bgxupdate - update active processes in a group.
# Works by transferring each process to new group
# if it is still active.
# in: bgxgrp - current group of processes.
# out: bgxgrp - new group of processes.
# out: bgxcount - number of processes in new group.
bgxupdate() {
bgxoldgrp=${bgxgrp}
bgxgrp=""
((bgxcount = 0))
bgxjobs=" $(jobs -pr | tr '\n' ' ')"
for bgxpid in ${bgxoldgrp} ; do
echo "${bgxjobs}" | grep " ${bgxpid} " >/dev/null 2>&1
if [[ $? -eq 0 ]] ; then
bgxgrp="${bgxgrp} ${bgxpid}"
((bgxcount = bgxcount + 1))
fi
done
}
# bgxlimit - start a sub-process with a limit.
# Loops, calling bgxupdate until there is a free
# slot to run another sub-process. Then runs it
# an updates the process group.
# in: $1 - the limit on processes.
# in: $2+ - the command to run for new process.
# in: bgxgrp - the current group of processes.
# out: bgxgrp - new group of processes
bgxlimit() {
bgxmax=$1 ; shift
bgxupdate
while [[ ${bgxcount} -ge ${bgxmax} ]] ; do
sleep 1
bgxupdate
done
if [[ "$1" != "-" ]] ; then
$* &
bgxgrp="${bgxgrp} $!"
fi
}
# Test program, create group and run 6 sleeps with
# limit of 3.
echo 0 $(date | awk '{print $4}') '[' ${bgxgrp} ']'
echo
for i in ../corpus/* ; do
bgxlimit 8 cpuset -l 8-15 nice -n 20 ./lexparser.sh ${i} > parseTreeOut_${i#../corpus/} 2> /dev/null
echo ${i#../corpus/} $(date | awk '{print $4}') '[' ${bgxgrp} ']'
done
# Wait until all others are finished.
echo
bgxupdate
while [[ ${bgxcount} -ne 0 ]] ; do
oldcount=${bgxcount}
while [[ ${oldcount} -eq ${bgxcount} ]] ; do
sleep 1
bgxupdate
done
echo 9 $(date | awk '{print $4}') '[' ${bgxgrp} ']'
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment