bug-bash
[Top][All Lists]
Advanced

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

Re: wait -n shouldn't collect multiple processes


From: Chet Ramey
Subject: Re: wait -n shouldn't collect multiple processes
Date: Mon, 25 Mar 2019 10:49:32 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:60.0) Gecko/20100101 Thunderbird/60.6.0

On 3/23/19 1:33 AM, Ben Elliston wrote:
> In bash 4.4.19, wait -n will collect the exit status of multiple
> processes if there are any -- not just one:
> 
> bje@bapbop:~$ sleep 10 & sleep 10 & sleep 10 & sleep 10 &
> [1] 13296
> [2] 13297
> [3] 13298
> [4] 13299
> bje@bapbop:~$ wait -n
> [1]   Done                    sleep 10
> [2]   Done                    sleep 10
> [3]-  Done                    sleep 10
> [4]+  Done                    sleep 10
> 
> This makes it impossible to wait for the completion of any process and
> then individually collect the exit status of each command. I think
> wait -n should be guaranteed to only return one of the available
> (terminated) processes. If there are multiple, they can be collected
> by calling wait -n multiple times or calling wait without '-n'.

OK, I should not have taken this report at face value without testing or
verifying it. That's my bad; I apologize for the confusion.

This doesn't test the behavior of `wait -n' at all. `wait -n' succeeds
once, then the rest of the processes are reaped before the next prompt
is printed. (I believe kre pointed this out.)

I assumed the same thing happened when the shell is running non-
interactively, and didn't check. The resulting discussion is my fault.

`wait -n' is only useful in scripts with job control enabled, so let's
run a quick test and see what happens under those conditions.

Given the following script:

set -m

{ sleep 5; exit 1; } & { sleep 5; exit 2; } & { sleep 5; exit 3; } & {
sleep 5; exit 4; } &

for f in 1 2 3 4; do
        wait -n
        echo wait return status: $?
done

jobs

which starts a set of background processes, then uses `wait -n' to wait
for them in turn, printing the exit status `wait -n' returns.

Running with bash-4.4:

$ ../bash-4.4-patched/bash --version
GNU bash, version 4.4.23(7)-release (x86_64-apple-darwin15.6.0)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
$ ../bash-4.4-patched/bash ./x19
wait return status: 1
wait return status: 2
wait return status: 4
wait return status: 3
$ ../bash-4.4-patched/bash ./x19
wait return status: 1
wait return status: 2
wait return status: 3
wait return status: 4
$ ../bash-4.4-patched/bash ./x19
wait return status: 2
wait return status: 3
wait return status: 4
wait return status: 1


This demonstrates that, despite what I said earlier, `wait -n' reaps
one process at a time and returns its exit status.

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



reply via email to

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