Yes, can be reused. Just to add to Stéphane Chazelas answer:
In case you need this for scripting, for example you want to kill $PID
where $PID is a single child process and you are not sure if you are killing the right process, then at least in bash you can check if the process with some $PID is a child of current shell like this:
if grep -qFx $PID <(jobs -rp); then
kill $PID
fi
Here jobs -rp
prints children PIDs and grep -qFx $PID
returns 0 when $PID is matched.
This approach helps when you need to distinguish a single child from other system processes.
But if you want to distinguish between the multiple children of the current shell, you probably need to notify the current shell on each child exit. For example:
{
trap "echo $BASHPID >> exited" EXIT
# some long action
} &
Here, when the child process exits, it writes its PID to the file exited
. The main process can then check if the process with specific PID is already exited before killing it.
Note. The SIGCHLD signal is sent to the current shell each time child exits. This can be used to optimize the process (eg. trap "something..." SIGCHLD
)
cat /proc/sys/kernel/pid_max
is reached, it might start from 2 looking for the next available, unused pid. I'd say yes. – Michael D. Jan 05 '18 at 11:50