On Wed, 17 Apr 2024, Chet Ramey wrote:
On 4/15/24 1:01 PM, Carl Edquist wrote:
Yet another point brought to light by the bcalc example relates to the
coproc pid variable. The reset() function first closes the coproc
pipe fds, then sleeps for a second to give the BC coproc some time to
finish.
An alternative might be to 'wait' for the coproc to finish (likely
faster than sleeping for a second).
If the coproc has some problem and doesn't exit immediately, `wait'
without options will hang. That's why I opted for the
sleep/kill-as-insurance combo.
Yes that much was clear from the script itself.
I didn't mean any of that as a critique of the bcalc script. I just meant
it brought to light the point that the coproc pid variable is another
thing in the current deallocate-on-terminate behavior, that needs to be
copied before it can be used reliably. (With the 'kill' or 'wait'
builtins.)
Though I do suspect that the most common case with coprocs is that closing
the shell's read and write fds to the coproc is enough to cause the coproc
to finish promptly - as neither read attempts on its stdin nor write
attempts on its stdout can block anymore.
I think this is _definitely_ true for the BC coproc in the bcalc example.
But it's kind of a distraction to get hung up on that detail, because in
the general case there may very well be other scenarios where it would be
appropriate to, um, _nudge_ the coproc a bit with the kill command.
(And before you ask why I didn't use `wait -n', I wrote bcalc in 30
minutes after someone asked me a question about doing floating point
math with awk in a shell script, and it worked.)
It's fine! It's just an example, after all :)
Carl