bash shell mark & simulating multi-processing feature
The so called multi-processing,means at the very same moment,multi processes are handled by the cpu.In bash shell and other shells(ksh,csh,etc.),mark & has a special simulation of multi-processing except for the widely known running background feature.The below shell snippets is an example for a explanation purpose.It’s very funny,try it!(file name as if.sh and use sh if.sh to run it)
#!/bin/bash #multi-processing
for ((i=0;i<5;i++));do
{
sleep 3;echo 1>>aa && echo “done!”
#you may of course use other more complicated operations here
} &
done
wait
cat aa|wc -l
rm aa
Ok.Now let’s use time command to test the execution time of the script:
time bash if.sh
done!
done!
done!
done!
done!
5
real 0m3.108s #here,5 loop of `sleep 3` costs 3 and some little more time
user 0m0.056s
sys 0m0.092s
What about we ignore the & ?Let’s just try it!
#!/bin/bash #single processing
for ((i=0;i<5;i++));do
{
sleep 3;echo 1>>aa && echo “done!”
}
done
wait
cat aa|wc -l
rm aa
now ,use time bash if.sh to see the result:
done!
done!
done!
done!
done!
5
real 0m15.132s
user 0m0.028s
sys 0m0.096s
Now,you can see,0m15.132s shows in the real column of the output,which we can conclude that 5 loop of `sleep 3` costs more than 15 seconds.It’s then called the single processing.
