Implement 'multithreading 'in shell'

Source: Internet
Author: User
That is, for example, I have 100 executable files, and there is no particular sequential execution relationship between them, such as CODE: job_1job_2job_2 ..... job_100 wants to use csh/bash to call and execute multiple threads at a time. For example, if five threads are opened at a time, job_1, 2, 3, 4, 5 start first. If any of the threads is completed first, continue to execute the next file that has not been initially executed, such as job_6, 7, 8 ...., in this way, all 100 files are executed at the specified number of threads. I originally wanted to use & quot

That is, for example, I have 100 executable files and there is no particular sequential execution relationship between them, as shown in figure

CODE:

job_1
job_2
job_2
.....
job_100
Want to use csh/bash for multi-thread invocation and execution
For example, if five threads are opened at a time, job_1, 2, 3, 4, and 5 are started first. If any of the threads is executed first, the next file that has not been initially executed will be executed, such as job_6, 7, 8 ...., in this way, all 100 files are executed at the specified number of threads.
I originally wanted to use "&" to put it in the background, but I can specify 5 at a time to put it in the background, but I cannot know when any of the programs will be executed, so we cannot continue to execute the next program!


Perfect solution

-(dearvoid@LinuxEden:Forum)-(~/tmp)-[$$=6718 $?=0] ; cat job_1#!/bin/bashn=$((RANDOM % 5 + 1))echo "$0 sleeping for $n seconds ..."sleep $necho "$0 exiting ..."-(dearvoid@LinuxEden:Forum)-(~/tmp)-[$$=6718 $?=0] ; for ((i = 2; i <= 10; ++i)); do cp job_1 job_$i; done-(dearvoid@LinuxEden:Forum)-(~/tmp)-[$$=6718 $?=0] ; cat jobs.sh#!/bin/bashnParellel=5nJobs=10sJobPattern='./job_%d'aJobs=()sNextJob=for ((iNextJob = 1; iNextJob <= nJobs; )); do    for ((iJob = 0; iJob < nParellel; ++iJob)); do        if [ $iNextJob -gt $nJobs ]; then            break;        fi        if [ ! "${aJobs[iJob]}" ] || ! kill -0 ${aJobs[iJob]} 2> /dev/null; then            printf -v sNextJob "$sJobPattern" $((iNextJob++))            echo "$sNextJob starting ..."            $sNextJob &            aJobs[iJob]=$!        fi    done    sleep .1donewait-(dearvoid@LinuxEden:Forum)-(~/tmp)-[$$=6718 $?=0] ; ./jobs.sh./job_1 starting ..../job_1 sleeping for 3 seconds ..../job_2 starting ..../job_2 sleeping for 2 seconds ..../job_3 starting ..../job_3 sleeping for 5 seconds ..../job_4 starting ..../job_5 starting ..../job_4 sleeping for 4 seconds ..../job_5 sleeping for 2 seconds ..../job_2 exiting ..../job_6 starting ..../job_6 sleeping for 2 seconds ..../job_5 exiting ..../job_7 starting ..../job_7 sleeping for 1 seconds ..../job_1 exiting ..../job_8 starting ..../job_8 sleeping for 3 seconds ..../job_7 exiting ..../job_9 starting ..../job_9 sleeping for 5 seconds ..../job_4 exiting ..../job_6 exiting ..../job_10 starting ..../job_10 sleeping for 5 seconds ..../job_3 exiting ..../job_8 exiting ..../job_9 exiting ..../job_10 exiting ...-(dearvoid@LinuxEden:Forum)-(~/tmp)-[$$=6718 $?=0] ; bye


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.