Re: Bash script and using multiple cores
Not sure that using the 4 cores is going to be faster, this kind of action is typically I/O bound, even with a SSD. To use several cores you could start several instances of find/grep in parallel on subdirectories, and then concatenate the result files: something like:
Code:
set -a pids
for subdir in *.
do
cd $subdir
# start grep in background over the subdirectory, and keep its pid
grep -R {pattern} > ../$subdir.partresult &
pids[${#pids[*]}]=$!
done
# Now wait until all processes are finished. We don't need to for wait them in the order
# in which they terminate, we just to make sure that the rest of the code isn't run until
# all have finished.
for pid in ${pids[*]}
do
wait $pid
# print "Sensed end of $pid"
done
# All processes finished, get he final result from the parts
cat *.partresult >final.result
On a hard disk this would be utterly pointless, because the hard disk contention (HDD head moves) is going to cost you a lot more than what you gain when searching with more cores. On a SSD the I/O contention is only on the data rate through the interface, so it will never be worse than with a single process, even if I doubt that it can get significantly better.
Last edited by ofnuts; February 7th, 2014 at 02:24 PM.
Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Bookmarks