关于脚本的含义请参照:

http://www.cnitblog.com/sysop/archive/2008/11/03/50974.aspx 【BASH实现“多线程”】

 

我只是对它的范例做了一些函数的封装,便于代码块的引用。

 

 
  
  1. #!/bin/bash  
  2.  
  3.  
  4. # Init thread pool  
  5. # Usage: Thread_init 10 , init 10 thread pool  
  6. Thread_init(){  
  7.         tmp_fifofile="/tmp/$$.fifo" 
  8.         mkfifo $tmp_fifofile  
  9.         exec 6<>$tmp_fifofile  
  10.         rm $tmp_fifofile  
  11.  
  12.         [ $# -ne 0 ] && thread=$@ || thread=5 # default thread is 5  
  13.  
  14.  
  15.         for ((i=0;i<$thread;i++))  
  16.         do  
  17.                 echo  
  18.         done >&6 
  19. }  
  20.  
  21. # Check function sub is exist  
  22. # Usage: Check_cmd "Thread_init 15" ,if Thread_init function is exist, return 0 ,else return 1.  
  23. Check_cmd(){  
  24.         if [ $# -ne 0 ];then  
  25.                 type "$1" &> /dev/null  
  26.                 RET=$?  
  27.                 [ ${RET} -eq 0 ] && return 0 || return ${RET}  
  28.         else return 1 
  29.         fi  
  30. }  
  31. # Thread main functiion                                                                                               
  32. # Usage: Thread sub, sub is the function of the sub proc.                                                             
  33. Thread(){                                                                                                             
  34.         read -u6                                                                                                      
  35.         {                                                                                                             
  36.                 # $@ is the parameters about threads                                                                  
  37.                 "$@" && {                                                                                             
  38.                                 echo "Thread $i is finished"                                                          
  39.                         } || {                                                                                        
  40.                                 echo "Thread $i error!!! "                                                            
  41.                         }                                                                                             
  42.                 echo >&6                                                                                              
  43.         } &                                                                                                           
  44.         pid=$!                                                                                                        
  45.         echo "Thread $i is started [$pid]"                                                                            
  46. }                                                                                                                     
  47.                                                                                                                       
  48. # Wait sub proc finined                                                                                               
  49. # Usage Thread_wait                                                                                                   
  50. Thread_wait(){                                                                                                        
  51.         wait                                                                                                          
  52.         exec 6>&-                                                                                                     
  53.         return 0                                                                                                      
  54. }                                                                                                                     
  55.                                                                                                                       
  56.                                                                                                                       
  57. # Sub fun                                                                                                             
  58. # Usage: DIY                                                                                                          
  59. sub(){                                                                                                                
  60. sleep 3        
  61. }                                                                                                                     
  62.                                                                                                                       
  63.                                                                                                                       
  64. # Main                                                                                                                
  65. if Check_cmd sub; then                                                                                                
  66.         RET=$?                                                                                                        
  67. # DIY BEGIN                                                                                                           
  68.         Thread_init 15 # 初始化线程池                                                                                 
  69.         for i in `seq 75`                                                                                             
  70.         do                                                                                                            
  71.                 Thread sub # sub为调用的子进程函数,可以在后边加参数                                                  
  72.         done                                                                                                          
  73.         Thread_wait && exit $? # 等待子进程都执行完毕后退出                                                           
  74. # DIY END                                                                                                             
  75. else                                                                                                                  
  76.         exit ${RET}                                                                                                   
  77.                                                                                                                       
  78. fi                                                                                                                    
  79.              

 

 

通过time测试很容易发现,线程池,开15个,75个子进程,需要3s*(75/15)=15s

real    0m15.026s
user    0m0.027s
sys     0m0.071s

模拟的效果还不错。

现在我们可以修改

# DIY BEGIN 
Thread_init 15 # 初始化线程池 
for i in `seq 75
do 
    Thread sub # sub为调用的子进程函数,可以在后边加参数 
done 
Thread_wait && exit $? # 等待子进程都执行完毕后退出 
# DIY END

上面一部分的逻辑,以及脚本中的sub子进程函数。例如,我可以用它来ssh方式批量上传,大家尽管发挥想像力好了,有好的应用,也给大家分享一下。