title: Bash `wait` command, waiting for more than 1 PID to finish execution
body: <p>I recently posted a question asking if it was possible to <a href="https://stackoverflow.com/questions/40359901/bash-is-it-possible-to-stop-a-pid-from-being-reused/40368941#40368941">prevent PID's from being re-used</a>.</p>
  
  <p>So far the answer appears to be no. (Which is fine.)</p>
  
  <p>However, the user <a href="https://stackoverflow.com/users/236465/diego-torres-milano">Diego Torres Milano</a> added an answer to that question, and my question here is in regards to that answer.</p>
  
  <p>Diego answered,</p>
  
  <blockquote>
    <p>If you are afraid of reusing PID's, which won't happen if you wait as
    other answers explain, you can use</p>
  
  <pre><code>echo 4194303 &gt; /proc/sys/kernel/pid_max
  </code></pre>
    
    <p>to decrease your fear ;-)</p>
  </blockquote>
  
  <p>I don't actually understand why Diego has used the number <code>4194303</code> here, but that's another question.</p>
  
  <p>My understanding was that I had a problem with the following code:</p>
  
  <pre><code>for pid in "${PIDS[@]}"
  do
      wait $pid
  done
  </code></pre>
  
  <p>The problem being that I have multiple PIDs in an array, and that the for loop will run the <code>wait</code> command sequentially with each PID in the array, however I cannot predict that the processes will finish in the same order that their PIDs are stored in this array.</p>
  
  <p>ie; the following <em>could</em> happen:</p>
  
  <ul>
  <li>Start waiting for PID in array index 0</li>
  <li>Process with PID in index 1 of array terminates</li>
  <li>New job(s) run on system, resulting in PID which is stored in index 1 of PID array being <em>reused for another process</em></li>
  <li><code>wait</code> terminates as PID in array index 0 exits</li>
  <li>Start waiting for PID in array index 0, <em>except this is now a different process and we have no idea what it is</em></li>
  <li>The process which was run which re-used the PID which <code>wait</code> is currently waiting for <em>never</em> terminates. Perhaps it is the PID of a mail server or something which a system admin has started.</li>
  <li><code>wait</code> keeps waiting until the next serious linux bug is found and the system is rebooted or there is a power outage</li>
  </ul>
  
  <p>Diego said:</p>
  
  <blockquote>
    <p>which won't happen if you wait as other answers explain</p>
  </blockquote>
  
  <p>ie; that the situation I have described above cannot happen.</p>
  
  <p>Is Diego correct?</p>
  
  <ul>
  <li>If so, why can the situation I discribed above not occur?</li>
  </ul>
  
  <p>Or is Diego not correct?</p>
  
  <ul>
  <li>If so, well, then I post a new question later today...</li>
  </ul>
  
  <h1>Additional notes</h1>
  
  <p>It has occured to me that this question might be confusing, unless you are aware that the PID's are PID's of processes launched in the background. ie;</p>
  
  <pre><code>my_function &amp;
  PID="$!"
  PIDS+=($PID)
  </code></pre>