title: bash: Are writes to named pipes atomic?
body: <p>I'm trying to parallelize the processing of a file set using bash. I'm using named pipes for keeping number of process fixed and to gather output from the processes.</p>
  
  <p>I'm assuming that the writes to named pipe are atomic, i.e the output of different process is not mixed up. Is that a safe assumption? </p>
  
  <p>Any advice is greatly appreciated. I'm limited to using bash.</p>
  
  <p>Here's the code:</p>
  
  <pre><code>mytask()
  {
    wItem=$1
    #dummy func; process workItem
    rt=$RANDOM
    st=$rt;
    let "rt %= 2"
    let "st %= 10"
    sleep $st
    return $rt
  }
  
  parallelizeTask()
  {
  workList=$1
  threadCnt=$2
  task=$3
  threadSyncPipeD=$4
  outputSyncPipeD=$5
  
  ti=0
  for workItem in $workList; do
    if [ $ti -lt $threadCnt ]; then
      { $task $workItem; if [ $? == 0 ]; then result="success"; else result="failure"; fi; \
        echo "$result:$workItem" &gt;&amp;$outputSyncPipeD; echo "$result:$workItem" &gt;&amp;$threadSyncPipeD; } &amp;
      ((ti++))
      continue;
    fi
    while read n; do
        ((ti--))
        break;
    done &lt;&amp;$threadSyncPipeD
    { $task $workItem; if [ $? == 0 ]; then result="success"; else result="failure"; fi; \
      echo "$result:$workItem" &gt;&amp;$outputSyncPipeD; echo "$result:$workItem" &gt;&amp;$threadSyncPipeD;} &amp;
    ((i++))
  done
  wait
  echo "quit" &gt;&amp;$outputSyncPipeD
  
  while read n; do
   if [[ $n == "quit" ]]; then
      break;
   else
      eval $6="\${$6}\ \$n"
   fi
   done &lt;&amp;$outputSyncPipeD;
  }
  
  main()
  {
    if [ ! -p threadSyncPipe ]; then
       mkfifo threadSyncPipe
     fi
  
     if [ ! -p outputSyncPipe ]; then
        mkfifo outputSyncPipe
     fi
  
     exec 4&lt;&gt;threadSyncPipe
     exec 3&lt;&gt;outputSyncPipe
     gout=
     parallelizeTask "f1 f2 f3 f4 f5 f6" 2 mytask 3 4 gout
  
     echo "finalOutput: $gout";
     for f in $gout; do
         echo $f
     done
  
     rm outputSyncPipe
     rm threadSyncPipe
  }
  
  main
  </code></pre>
  
  <p>I found below related post with answer to my question. I have revised the title to make it more appropriate.</p>
  
  <p><a href="https://stackoverflow.com/questions/587727/are-there-repercussions-to-having-many-processes-write-to-a-single-reader-on-a-n">Are there repercussions to having many processes write to a single reader on a named pipe in posix?</a></p>