#!/bin/bash # smpqueue: Simple queue-based SMP shell scheduler # Written by Patrice Levesque in 2008 # License: Public domain # # Invoke without arguments to get usage information # # Note that the scheduling queue is checked every second, # so if execution time of your tasks is very short, this # script will make total execution time higher. SYSCPU='/sys/class/cpuid/' WHICH=`which which 2> /dev/null` || { echo "Couldn't find 'which'. Exiting"; exit 1; } WC=`${WHICH} wc 2> /dev/null` || { echo "Couldn't find 'wc'. Exiting"; exit 1; } LS=`${WHICH} ls 2> /dev/null` || { echo "Couldn't find 'ls'. Exiting"; exit 1; } SLEEP=`${WHICH} sleep 2> /dev/null` || { echo "Couldn't find 'sleep'. Exiting"; exit 1; } NUMBER_OF_THREADS=`${LS} ${SYSCPU} | ${WC} -w` PROGRAM_NAME=`basename "${0}"` COMMAND="${1}" shift ALLARGS='' # Help if [ -z "${1}" -o "${1}" = "-h" -o "${1}" = "--help" ]; then echo -e "${PROGRAM_NAME}: Schedules repetitive tasks on SMP" echo echo -e "Usage:" echo -e "\t${PROGRAM_NAME} program arguments -- files" echo echo -e "Examples:" echo -e "\t${PROGRAM_NAME} oggenc --bitrate=192 -- *.wav" echo -e "\t${PROGRAM_NAME} md5sum -- *" echo -e "\t${PROGRAM_NAME} mogrify -resize 640x480 -- *.jpg" echo echo -e "(${NUMBER_OF_THREADS} processors detected on this machine)" exit 1; fi # Parse arguments while true; do ARGS=${1} if [ ${ARGS} = '--' ]; then break; fi ALLARGS="${ALLARGS} ${ARGS}" shift done # Schedule the tasks while true; do shift [ -z "${1}" ] && { wait; exit; } ENQUEUED=1 while [ ${ENQUEUED} -eq 1 ]; do CURRENT_PROCESSES=`jobs | ${WC} -l` if [ ${CURRENT_PROCESSES} -lt ${NUMBER_OF_THREADS} ]; then ${COMMAND} ${ALLARGS} "${1}" & ENQUEUED=0 else ${SLEEP} 1s fi done; done;