Keeping all CPUs busy
I sometimes run many CPU-intensive processes in a loop like this
for f in *; do lame -h $f ${f/wav/mp3}; doneHowever, 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
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}; doneHowever, 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
Makefileand run make -j2, but I'd like to know I there is a simpler way withoutMakefileor something like that.
for f in *; do lame -h $f ${f/wav/mp3} &; done; waitThat 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]>