I am running on *nix based OS's and have a script that initiates multiple processes concurrently. The main goal for me is to initiate these processes concurrently, and gather the returned exit statuses for each of the processes. I've found that using wait(pid)
will achieve this, since all child processes are owned by the parent process. However, I am concerned that once a child process (one of the concurrent processes initiated) completes, its PID will be released and made available to be recycled within the system.
So I guess the question is, if a parent process initiates several child processes concurrently, will the PID of a child process that completes be made available to the system for recycling prior to the parent process completing? If so, how can I best obtain the exit statuses of each of the child processes?
Example of bash script below:
local file=$1
local count=0
<files are split; and suffixed with aa,ab,ac,ad>
/home/text/concurrencyTest.sh $file-aa >> /home/text/$file-aa.log 2>&1 &
/home/text/concurrencyTest1.sh $file-ab >> /home/text/$file-ab.log 2>&1 &
/home/text/concurrencyTest2.sh $file-ac >> /home/text/$file-ac.log 2>&1 &
/home/text/concurrencyTest3.sh $file-ad >> /home/text/$file-ad.log 2>&1 &
for job in `jobs -p`
do
echo "Job: $job"
wait "$job"
rc=$?
echo "RC for $job is $rc"
if [[ rc -ne 0 ]]; then
FAIL[$count]="$job"
((count++))
fi
done
if [[ $count -ne 0 ]]; then
echo "ERROR: $count Job(s) Failed!"
echo "Failed Process PID(s): ${FAIL[@]}"
echo "Failed Processing for file: $file"
return 1
fi