Writing multi-process application in UNIX-Shell

Posted on May 14, 2007 by ZDima.
Categories: UNIX-Shell.

Sometimes you need to improve performance of an application where UNIX shell script need to start a several process. The solution below can help you if application logic allows to run them in parallel, and application will not advance to the next step till all started processes are finished.

The solution have two parts. First part is to start processes simultaneously:

  1. totalProc=4
  2. idx=0
  3.  
  4. while [[ $idx -lt $totalProc ]]
  5. do
  6.   # will wait for each OS
  7.   if [[ ${CHILDS[$idx]} -gt 2 ]]
  8.   then
  9.     # starting child process
  10.     ${executable name} 2&gt;&amp;1 &gt; ${logname}.log <strong>&amp;</strong>
  11.     CHILDS[$idx]=$!
  12.   fi
  13.   idx=$(( $idx + 1 ))
  14. done

Next step is to wait till child processes are finished:

  1. idx=0
  2. while [[ $idx -lt $totalProc ]]
  3. do
  4.   # will wait for each OS
  5.   if [[ ${CHILDS[$idx]} -gt 2 ]]
  6.   then
  7.     echo "===================== waiting for ${CHILDS[$idx]}"
  8.     PID=${CHILDS[$idx]}
  9.     wait $PID
  10.     CHILDS[$idx]=$?
  11.     if [[ ${CHILDS[$idx]} -ne 0 ]]
  12.     then
  13.       # process failed
  14.     else
  15.       # process finished
  16.     fi
  17.   fi
  18.   idx=$((idx+1))
  19. done

This script will wait for all children processes one by one in order they were created.

no comments yet.

Leave a comment

Names and email addresses are required (email addresses aren't displayed), url's are optional.

Comments may contain the following xhtml tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>