Re: How difficult would it be to add a timeout to "wait"?

2023-04-21 Thread Chet Ramey

On 4/21/23 11:34 AM, Dale R. Worley wrote:


My interest here isn't "Can I accomplish this task with Bash?" but quite
literally, Can I make this a *feature* of Bash so I don't have to set up
the mechanics?  (I already know of a couple of other ways to do it with
Bash.)


My point is that if it's easy enough -- and it is -- to do it without
adding features to bash itself, then there's not a compelling argument,
nor sufficient demand, to add more code to bash.


So then fleshing this out, let me ask:  Is it reasonable to add an
optional timeout to the "wait" builtin using this mechanism?


See above.

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/




Re: How difficult would it be to add a timeout to "wait"?

2023-04-21 Thread Dale R. Worley
Chet Ramey  writes:
> On 4/20/23 1:53 PM, Dale R. Worley wrote:
>> How difficult would it be to add an optional timeout argument to the
>> "wait" builtin?
>
> Try a variant of this.
> ...

My interest here isn't "Can I accomplish this task with Bash?" but quite
literally, Can I make this a *feature* of Bash so I don't have to set up
the mechanics?  (I already know of a couple of other ways to do it with
Bash.)

An interesting point, which you may or may not have intended, was that
this Bash code can be turned into C code fairly easily:  Fork a
subprocess to wait for the timeout and when it fires, send USR1 to Bash.
Then make sure Bash is waiting for the timeout subprocess as well as
whatever other children it should be waiting for.

My *reflex* is that this is a really heavyweight implementation, but on
consideration, I think I'm wrong:  fork() is cheap in Un*x, indeed, that
may have been its central design innovation, make processes cheap and
use them freely.

So then fleshing this out, let me ask:  Is it reasonable to add an
optional timeout to the "wait" builtin using this mechanism?

Dale



Re: How difficult would it be to add a timeout to "wait"?

2023-04-20 Thread Chet Ramey

On 4/20/23 1:53 PM, Dale R. Worley wrote:

How difficult would it be to add an optional timeout argument to the
"wait" builtin?


Try a variant of this.

trap 'echo timeout!' USR1 # choose your fighter

wait_with_timeout()
{
pid=$1
timeout=$2

{ sleep $timeout && kill -USR1 $$; } & tpid=$!

wait $pid
stat=$?

kill -TERM $tpid 2>/dev/null

return $stat
}

{ sleep 10; exit 42; } &
pid=$!
wait_with_timeout $pid 5# will time out
echo $?

wait_with_timeout $pid 10   # won't time out
echo $?


--
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/