Home > IT Architecture, Programming > bash shell mark & simulating multi-processing feature

bash shell mark & simulating multi-processing feature

November 13th, 2010

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

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

Ok.Now let's use time command to test the execution time of the script:
time ./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

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.

Problem resolved?


  1. Korziner
    September 22nd, 2011 at 14:14 | #1

    “ignore the &” Why present in the second script?

    • Anonymous
      September 26th, 2011 at 06:36 | #2

      oops.. Thanks for reminding

  1. No trackbacks yet.