Bash: Running the same program over multiple cores

后端 未结 3 654
闹比i
闹比i 2020-12-14 10:28

I have access to a machine where I have access to 10 of the cores -- and I would like to actually use them. What I am used to doing on my own machine would be something like

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-14 11:23

    # Wait while instance count less than $3, run additional instance and exit
    function runParallel () {
        cmd=$1
        args=$2
        number=$3
        currNumber="1024"
        while true ; do
            currNumber=`ps -e | grep -v "grep" | grep " $1$" | wc -l`
            if [ $currNumber -lt $number ] ; then
                break
            fi
            sleep 1
        done
        echo "run: $cmd $args"
        $cmd $args &
    }
    
    loop=0
    # We will run 12 sleep commands for 10 seconds each 
    # and only five of them will work simultaneously
    while [ $loop -ne 12 ] ; do
        runParallel "sleep" 10 5
        loop=`expr $loop + 1`
    done
    

提交回复
热议问题