I recently posted a question asking if it was possible to prevent PID's from being re-used.
So far the answer appears to be no. (Which is fine.)
However, the user Diego Torres Milano added an answer to that question, and my question here is in regards to that answer.
Diego answered,
If you are afraid of reusing PID's, which won't happen if you wait as other answers explain, you can use
echo 4194303 > /proc/sys/kernel/pid_max
to decrease your fear ;-)
I don't actually understand why Diego has used the number 4194303
here, but that's another question.
My understanding was that I had a problem with the following code:
for pid in "${PIDS[@]}"
do
wait $pid
done
The problem being that I have multiple PIDs in an array, and that the for loop will run the wait
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.
ie; the following could happen:
- Start waiting for PID in array index 0
- Process with PID in index 1 of array terminates
- New job(s) run on system, resulting in PID which is stored in index 1 of PID array being reused for another process
wait
terminates as PID in array index 0 exits- Start waiting for PID in array index 0, except this is now a different process and we have no idea what it is
- The process which was run which re-used the PID which
wait
is currently waiting for never terminates. Perhaps it is the PID of a mail server or something which a system admin has started. wait
keeps waiting until the next serious linux bug is found and the system is rebooted or there is a power outage
Diego said:
which won't happen if you wait as other answers explain
ie; that the situation I have described above cannot happen.
Is Diego correct?
- If so, why can the situation I discribed above not occur?
Or is Diego not correct?
- If so, well, then I post a new question later today...
Additional notes
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;
my_function &
PID="$!"
PIDS+=($PID)