[SunHELP] Keeping certain number of background procs...

wyatt at draggoo.com wyatt at draggoo.com
Thu Mar 7 13:41:55 CST 2002


I'm not quite sure how to go about this, but I'd like a generic script
so that I can give it a list of tasks, and it always has x number
running.  When one ends, it starts another one.  I'm not sure that's
clear, so I'll give you an example.

I've got a box with 2 CPUs.  There are a whole slew of files I
want to bzip2, and since I've got 2 CPUs, and neither are real busy,
I'd like to always have 2 bzip2 processes running so that each CPU
takes one.  When one file is done, it will start the next one.  I
use bash as my shell if that helps...

On a single CPU box, a normal for loop would work:
for FILE in *; do
  bzip2 $FILE
done

But that will only do 1 at a time.  If there was a way to get the
next file from the loop, it wouldn't be perfect, but it would help:

for FILE1 in *; do
  FILE2 = <get the next one somehow...>
  bzip2 $FILE1 &
  bzip2 $FILE2 &
  wait
done

The biggest problem with this is that if FILE1 is very small, and FILE2
is huge, it will get done with FILE1, and then wait for ever for FILE2
to finish before it grabs the next two.

If anyone has any hints here, I'd appreciate it.



More information about the SunHELP mailing list