For example, I have 100 executable files, there is no special relationship between each other, such as code:
Copy Code code as follows:
Job_1
Job_2
Job_2
.....
job_100
Want to use Csh/bash to multithreading call execution.
For example, if you open 5 threads at a time, then job_1,2,3,4,5 starts together, then any one of these threads, if executed first, continues to execute the next file that has not been first executed, such as job_6,7,8 ..., so that all 100 files are executed with the number of threads specified.
I would like to use "&" to put in the background, but so I can specify 5 in the background, but I do not know when any one of the programs completed, so I can not continue to execute the next program Ah!
Perfect solution:
Copy Code code as follows:
-(Dearvoid@linuxeden:forum)-(~/tmp)-
[$$=6718 $?=0]; Cat Job_1
#!/bin/bash
n=$ ((RANDOM% 5 + 1))
echo "$ sleeping for $n seconds ..."
Sleep $n
echo "$ exiting ..."
-(Dearvoid@linuxeden:forum)-(~/tmp)-
[$$=6718 $?=0]; for ((i = 2; I <= ++i)); Do CP job_1 job_$i; Done
-(Dearvoid@linuxeden:forum)-(~/tmp)-
[$$=6718 $?=0]; Cat jobs.sh
#!/bin/bash
Nparellel=5
njobs=10
Sjobpattern= './job_%d '
Ajobs= ()
snextjob=
for ((inextjob = 1; inextjob <= njobs;)); Todo
for ((ijob = 0; ijob < nparellel; ++ijob)); Todo
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. 1
Done
Wait
-(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