⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 multiple-processes.sh

📁 一本完整的描述Unix Shell 编程的工具书的所有范例
💻 SH
字号:
#!/bin/bash# parent.sh# Running multiple processes on an SMP box.# Author: Tedman Eng#  This is the first of two scripts,#+ both of which must be present in the current working directory.LIMIT=$1         # Total number of process to startNUMPROC=4        # Number of concurrent threads (forks?)PROCID=1         # Starting Process IDecho "My PID is $$"function start_thread() {        if [ $PROCID -le $LIMIT ] ; then                ./child.sh $PROCID&                let "PROCID++"        else           echo "Limit reached."           wait           exit        fi}while [ "$NUMPROC" -gt 0 ]; do        start_thread;        let "NUMPROC--"donewhile truedotrap "start_thread" SIGRTMINdoneexit 0# ======== Second script follows ========#!/bin/bash# child.sh# Running multiple processes on an SMP box.# This script is called by parent.sh.# Author: Tedman Engtemp=$RANDOMindex=$1shiftlet "temp %= 5"let "temp += 4"echo "Starting $index  Time:$temp" "$@"sleep ${temp}echo "Ending $index"kill -s SIGRTMIN $PPIDexit 0# ======================= SCRIPT AUTHOR'S NOTES ======================= ##  It's not completely bug free.#  I ran it with limit = 500 and after the first few hundred iterations,#+ one of the concurrent threads disappeared!#  Not sure if this is collisions from trap signals or something else.#  Once the trap is received, there's a brief moment while executing the#+ trap handler but before the next trap is set.  During this time, it may#+ be possible to miss a trap signal, thus miss spawning a child process.#  No doubt someone may spot the bug and will be writing #+ . . . in the future.# ===================================================================== ## ----------------------------------------------------------------------################################################################### The following is the original script written by Vernia Damiano.# Unfortunately, it doesn't work properly.##################################################################!/bin/bash#  Must call script with at least one integer parameter#+ (number of concurrent processes).#  All other parameters are passed through to the processes started.INDICE=8        # Total number of process to startTEMPO=5         # Maximum sleep time per processE_BADARGS=65    # No arg(s) passed to script.if [ $# -eq 0 ] # Check for at least one argument passed to script.then  echo "Usage: `basename $0` number_of_processes [passed params]"  exit $E_BADARGSfiNUMPROC=$1              # Number of concurrent processshiftPARAMETRI=( "$@" )      # Parameters of each processfunction avvia() {         local temp         local index         temp=$RANDOM         index=$1         shift         let "temp %= $TEMPO"         let "temp += 1"         echo "Starting $index Time:$temp" "$@"         sleep ${temp}         echo "Ending $index"         kill -s SIGRTMIN $$}function parti() {         if [ $INDICE -gt 0 ] ; then              avvia $INDICE "${PARAMETRI[@]}" &                let "INDICE--"         else                trap : SIGRTMIN         fi}trap parti SIGRTMINwhile [ "$NUMPROC" -gt 0 ]; do         parti;         let "NUMPROC--"donewaittrap - SIGRTMINexit $?: <<SCRIPT_AUTHOR_COMMENTSI had the need to run a program, with specified options, on a number ofdifferent files, using a SMP machine. So I thought [I'd] keep runninga specified number of processes and start a new one each time . . . oneof these terminates.The "wait" instruction does not help, since it waits for a given processor *all* process started in background. So I wrote [this] bash scriptthat can do the job, using the "trap" instruction.  --Vernia DamianoSCRIPT_AUTHOR_COMMENTS

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -