bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: wait -n misses signaled subprocess


From: Greg Wooledge
Subject: Re: wait -n misses signaled subprocess
Date: Sun, 28 Jan 2024 22:43:13 -0500

On Sun, Jan 28, 2024 at 10:26:27PM -0500, Dale R. Worley wrote:
> The man page doesn't make clear that if you don't specify "-n" and do
> supply ids and one of them has already terminated, you'll get its status
> (from the terminated table); the wording suggests that "wait" will
> always *wait for* a termination.

This might be a result of C programmers who already know the semantics
of wait(2) writing documentation which assumes the reader *also* knows
these semantics.

wait(2) and its brethren return immediately if the process in question
has already terminated.  It's how you reap the zombie and free up the
process table slot, while also retrieving its exit status.  If it's
not already dead, then wait(2) blocks until death occurs.

The shell's "wait" command is meant to mimic this behavior, at its core.
There are some differences, however -- notably, the shell aggressively
reaps zombies and stores their exit statuses in memory, revealing them
to you in the event that you call "wait".  Normally this change is
invisible, but if you were *counting* on the zombie to be there, holding
on to that PID, preventing it from being reused until you could observe
the death and react to it, then you're screwed.  Don't use the shell for
this.

Anyway... a script writer who has a basic familiarity with wait(2) and
who reads about "wait -n" will probably assume that wait -n will return
immediately if a child process has already terminated and hasn't been
"pseudo-reaped" by a previous "wait" command yet.  If three children
have terminated, then the next three "wait -n" commands should return
immediately, and the fourth should block (assuming a fourth child exists).



reply via email to

[Prev in Thread] Current Thread [Next in Thread]