Does the child process get the PID for reuse if the parent process is still running?

I am running on a nix based OS and have a script that starts several processes at the same time. The main goal for me is to initiate these processes at the same time and collect the return exit statuses for each of the processes. I found that use wait(pid)would achieve this since all child processes belong to the parent process. However, I am concerned that after the completion of the child process (one of the parallel processes), its PID will be released and available for reuse in the system.

So, I think the question is that if the parent process initiates several child processes at the same time, will the PID of the child process that is completed be available to the system for re-processing until the parent process is complete? If so, what is the best way to get the exit status of each of the child processes?

An example 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
+4
source share
1 answer

The PID of the child process becomes reusable when the parent process calls waiteither waitpid(or any other function of this family, such as wait3, wait4etc.).

, - , . waitpid , ( , ), (.. , ). wait , , .

SIGCHLD , , . SIGCHLD ; ., , POSIX .

, , , init, PID 1. init, .

script wait - wait. script , wait , , wait , ( , ). wait $pid1 , , $pid2 ; $pid2 , wait $pid2 . , , $pid2 .

+3

Source: https://habr.com/ru/post/1619555/


All Articles