The Linux Bash Shell supports inherent task parallelism. For example, you can put a & (ampersand) symbol after each command to start a task in background (asynchronously). Let’s take the following shell for example:
#!/bin/bash
s=0
for ((i=1;i<=5;i++));do
{
sleep $i
s=$((s+i))
}&
wait
echo "s=$s"
The for indeed is a “parallel for” because of the ampersand symbol. So the block of statements inside the for will be executed in parallel and asynchronously. The wait command acts as a barrier which will wait for all previous threads in the current script to finish. It takes 5 seconds (maximum thread, sleep 5) but if you take out the ampersand symbol, then the script running time will be approximately 15 seconds and you will get the result of s=15.
root@HP-PC:~# time ./test
s=0
real 0m5.048s
user 0m0.000s
sys 0m0.094s
Inside the parallel.for, 5 threads may write to the same variable, which yields the race condition and that requires a lock/mutex to allow atomic operations. However, the parallel for allows to write powerful and simple parallel tasking in Linux shell, which improves the efficiency.
–EOF (The Ultimate Computing & Technology Blog) —
295 wordsLast Post: The PHP Page Rule Checker of CloudFlare
Next Post: How to Enable AMP with WordPress? AMP Optimisation Techniques with WordPress
