Linux Shell Multithreaded Programming--Original

Source: Internet
Author: User

Related command files

1,runcommandinthreads.sh Multi-threaded implementation of the main code

cdir=$ (dirname  $BASH _source)  # #Current  directory of this script file.source   $cdir/logutil.shfunction createfile ()  {local dir=$1local idx=$2local fl= "$dir/$ IDX "touch  $fl}function execshellcommand ()  { local commd=$1local subpid=" $! " $ (debug  "[$subPID] execute command  ' $COMMD '") eval  "$COMMD"}###  set up and start multithreading  ### ##  parameters: 1, execute command list (corresponding to an array) 2, number of concurrent execution Threads ##  command format: source runcommandinthreads.sh &&  setupthreadsimmediately [execution Command List] [number of concurrent execution Threads]function setupthreadsimmediately ()  {   local starttimesecs=$ (date + "%s")  local parrelthreadssize=${2:-2}local tmp=$1[@ ] local commands= (${!tmp}) local commandssize=${#commands [*]} local  Parrelthreadpidfilestoredir= "/tmp/parrel/" ' date +%y%m%d%h%m%s%s ' if [  $commandsSize  -le  0 ]; then echo  "No commands input. "  returnfiif [ ! -d  $parrelThreadPidFileStoreDir  ]; thentrace  "Create  direcotry: "$parrelThreadPidFileStoreDirmkdir  -p  $parrelThreadPidFileStoreDirfi $ (info ") Parrelthreadssize:${parrelthreadssize} ,commandssize: $commandsSize ") ### for  ((i=0;i<$ parrelthreadssize;i++));d o # $ (createfile  $parrelThreadPidFileStoreDir   "$i") # done## Local mainpid= "Main" local threadcount=0for  ((i=0; i<commandssize; i++));d o  while [ -z  ""  ]do  local chkfl= "$ (checkenableenroll $ parrelthreadpidfilestoredir  $parrelThreadsSize   $mainPID) "  if [ ! -z $ chkfl ]; then  #Start  new thread.  threadcount=$ (($threadCount + 1))    touch  $chkFl   $ (trace  "[$mainPID] start new sub-thread")   {      # #let  threadcount= $threadCount +1local tidx=$ (($i + 1)     local subpid = "$tidx-$!" local substarttm=$ (date + "%s") ##[ ! -f  $chkFl  ] && $ (debug   "[$subPID] file: $chkFl") echo  "$subPID"  >  $chkFl # #Do  process in  thread.# #local  cmd= () # #eval  cmd=\${$commands [@]}# #cmd = "$cmd"  cmd= "${commands[i]}" cmd=${cmd//,/  } cmd= $cmd " " $subPID ##$ (execshellcommand  "$cmd") $ (debug  "[$subPID] execute  command  ' $cmd ') eval  "$cmd" #------------Test-------------# $ (debug  "[$subPID]  execute command  ' $cmd ') # local ev= ' expr  $tidx  % 2 ' # if [ $ ev -eq 0 ]; then   # sleep 4# else # sleep 2#  fi   [ -f  $chkFl  ] && rm  $chkFl  &&  $ (trace " [$subPID] deleted file  $chkFl]  local subendtm=$ (date + "%s") $ (info  "$ threadcount/$commandsSize  [$subPID] sub-thread has complete, elapse:  "$ ( calcelapse  $subStartTm   $subEndTm))   } & ## work in background .  break;  fi    #chkFl = "$ (checkenableenroll $ parrelthreadpidfilestoredir  $parrelThreadsSize   $mainPID) "donedone  # #debug  " Delete  direcotry: "$parrelThreadPidFileStoreDirwait  # wait for all sub threads.   local endtimesecs=$ (date + "%s")  # #local  elapsetime=$ (($startTimeSecs-$ endtimesecs)) info  "--- Finish all, elapse: " $ (calcelapse  $startTimeSecs  $ ENDTIMESECS) "-----"  rm -rf  $parrelThreadPidFileStoreDir    }function  Calcelapse ()  {local st=$1local et=$2local elapsetime=$ (($et-$st)) echo $ ((ELAPSETIME/60)) " mins " $ ((elapsetime%60)) " seconds"}function  Checkenableenroll ()  {local dir=$1local len=$2 local pid=$3local fl= "" local  i=0;for  ((i=0;i< $len; i++));d o fl= "$dir/$i" if [ ! -f  $FL  ]; then   trace  "[$pid] found " $FL  echo  $fl  returnelse   trace   "ignore  $fl" fi doneecho  ""}

2. logutil.sh Print Log

 # if [ ! -z  $INCLUDING _log_util ]; then      # return # fiexport including_log_util= "true"     export log_ default_lvl=-1export log_trace_lvl=10export log_debug_lvl=20export log_info_lvl=30export  Log_warn_lvl=40export log_error_lvl=50function trace ()  { local loglvl=${loglvl:-$LOG _ default_lvl}   if [  $LOGLVL  -le  $LOG _trace_lvl ]; thenlocal  tm= "' date +%t '" echo  "trace  $tm  $*" >&2fi }function debug ()  {  local loglvl=${loglvl:-$LOG _default_lvl}   if [  $logLvl  -le  $LOG _DEBUG _lvl ]; thenlocal tm= "' date +%t '" echo  "debug  $tm  $*" &GT;&AMP;2FI&NBSP;} Function info ()  { local loglvl=${loglvl:-$LOG _default_lvl} if [  $LOGLVL  -le  $LOG _INFO_LVl ]; thenlocal tm= "' date +%t '" echo  "info  $tm  $*" >&2fi }   function warn ()  { local loglvl=${loglvl:-$LOG _default_lvl} if [ $ loglvl -le  $LOG _warn_lvl ]; thenlocal tm= "' date +%t '" echo  "WARN  $tm  $* ">&2fi }  function error ()  { local loglvl=${loglvl:-$LOG _ default_lvl} if [  $LOGLVL  -le  $LOG _error_lvl ]; thenlocal tm= "' Date  +%t ' "echo " error  $tm  $* "&GT;&AMP;2FI&NBSP;}

Linux Shell Multithreaded Programming--Original

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.