Skip to contentSkip to conversation

Keeping all CPUs busy

DISCUSSION Urs Thuermann 2 participants 2 messages Aug 13, 2003

lore
Share & tools
lore
#0 lore raw

I sometimes run many CPU-intensive processes in a loop like this

    for f in *; do lame -h $f ${f/wav/mp3}; done

However, this will run only one process at a time. Executing all processes in the background would execute them all at the same time. Is there an easy way to run n processes at time, when you have n CPUs?

On could of course write an Makefile and run make -j2, but I'd like to know I there is a simpler way without Makefile or something like that.

urs

#1 lore raw
Quoted 10 lines · Urs Thuermann

I sometimes run many CPU-intensive processes in a loop like this

    for f in *; do lame -h $f ${f/wav/mp3}; done

However, this will run only one process at a time. Executing all processes in the background would execute them all at the same time. Is there an easy way to run n processes at time, when you have n CPUs?

On could of course write an Makefile and run make -j2, but I'd like to know I there is a simpler way without Makefile or something like that.

        for f in *; do lame -h $f ${f/wav/mp3} &; done; wait

That starts them all in the background. I wouldn't normally do that if the number of files is large and the total work done is nontrivial.

The "wait" command tells bash to wait for all the background processes to complete. Without it, you get the shell prompt back immediately (which may be what you want).

If you're working on a shared system, you should think twice about grabbing so many resources at once, and if you still decide to do it you should do "... do nice lame ..." or something similar to run them at a lower priority so that other interactive users (including you) get first shot at the CPU.

--
Jeff Woods <[email protected]>