On Fri, Jul 26, 2024 at 10:37 AM Chet Ramey <chet.ra...@case.edu> wrote:
>
> On 7/20/24 1:50 PM, Zachary Santer wrote:
> >
> > 'wait' without -n or pid arguments doesn't look in the list of saved
> > pids and statuses simply because it would serve no purpose for it to
> > do so. The return status will be 0, no matter how any child process
> > terminated, or even if there never was a child process. *
> >
> > For 'wait -n', on the other hand:
> > "If the -n option is supplied, wait waits for a single job from the
> > list of ids or, if no ids are supplied, any job, to complete and
> > returns its exit status."
> > People are going to naturally expect 'wait -n' without pid arguments
> > to return immediately with the exit status of an already-terminated
> > child process, even if they don't provide id arguments. In order to do
> > so, 'wait -n' obviously has to look in the list of saved pids and
> > statuses.
>
> I think the part you're missing is that processes get moved to this list
> after the user has already been notified of their termination status.

I think I was missing more than that. Was the original 'wait -n'
discussion from January specific to its use within the interactive
shell?

> >> If two jobs happen to finish simultaneously, the next call to wait -n
> >> should reap one of them, and then the call after that should reap
> >> the other.  That's how everyone wants it to work, as far as I've seen.
> >>
> >> *Nobody* wants it to skip the job that happened to finish at the exact
> >> same time as the first one, and then wait for a third job.  If that
> >> happens in the loop above, you'll have only 4 jobs running instead of 5
> >> from that point onward.
>
> OK, if you can send me an example that shows bash doing the wrong thing
> here, we can talk about it.

Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: msys
Compiler: gcc
Compilation CFLAGS: -g -O2
uname output: MSYS_NT-10.0-19045 Zack2021HPPavilion
3.5.3-d8b21b8c.x86_64 2024-07-09 18:03 UTC x86_64 Msys
Machine Type: x86_64-pc-msys

Bash Version: 5.3
Patch Level: 0
Release Status: alpha

See attached. I didn't get what I was expecting when executing the
attached script. The while loop runs forever, as it should.

When sourcing the script within interactive bash-5.3-alpha, on the
other hand, I get very different and arguably undesirable behavior.
That the loop terminates at all shows the effect of ignoring child
processes that have already terminated. However, we also see that
behavior with job control off, and thus without the user being
notified of any terminated background jobs. What's going on there?

> > * Or in any case, 'wait' without arguments as the first command in a
> > script seemed to have a termination status of 0. But then the manual
> > says:
> > "If none of the supplied arguments is a child of the shell, or if no
> > arguments are supplied and the shell has no unwaited-for children, the
> > exit status is 127."
>
> That describes the behavior of `wait -n' without arguments; it immediately
> follows the sentence introducing the -n option.

Thanks for your updates to the manual.

"Wait for each specified child process id and return the termination
status of the last id."
Would it be better to say "Wait for each specified child process id
and return the termination status of the last id argument," to
emphasize that it's not returning the termination status of the last
process to terminate from among those whose ids are listed?

"Otherwise, the return status is the exit status of the last id."
Is that correct? The final paragraph is in reference to 'wait -n',
right? It could return the exit status of any one of the processes
whose ids are listed.
zsant@Zack2021HPPavilion MSYS ~/repos/bash
$ set -m

zsant@Zack2021HPPavilion MSYS ~/repos/bash
$ ~/random/wait-n-failure false
# CTRL-C

zsant@Zack2021HPPavilion MSYS ~/repos/bash
$ ~/random/wait-n-failure true
/home/zsant/random/wait-n-failure: line 31: wait: 63274: no such job
/home/zsant/random/wait-n-failure: line 31: wait: 63277: no such job
/home/zsant/random/wait-n-failure: line 31: wait: 63274: no such job
/home/zsant/random/wait-n-failure: line 31: wait: 63277: no such job
# etc.
# CTRL-C


zsant@Zack2021HPPavilion MSYS ~/repos/bash
$ ^C

zsant@Zack2021HPPavilion MSYS ~/repos/bash
$ source ~/random/wait-n-failure false
[2]   Done                    random_sleep
[3]   Done                    random_sleep
[4]   Done                    random_sleep
[5]   Done                    random_sleep
[7]-  Done                    random_sleep
[6]-  Done                    random_sleep
[2]-  Done                    random_sleep
2 processes waited / 7 processes forked
1 seconds

zsant@Zack2021HPPavilion MSYS ~/repos/bash
$ source ~/random/wait-n-failure false
[2]   Done                    random_sleep
[3]   Done                    random_sleep
[4]   Done                    random_sleep
[5]   Done                    random_sleep
[7]-  Done                    random_sleep
[6]-  Done                    random_sleep
[2]-  Done                    random_sleep
2 processes waited / 7 processes forked
0 seconds

zsant@Zack2021HPPavilion MSYS ~/repos/bash
$ source ~/random/wait-n-failure true
[2]   Done                    random_sleep
[3]   Done                    random_sleep
[4]   Done                    random_sleep
[5]   Done                    random_sleep
[6]   Done                    random_sleep
[7]-  Done                    random_sleep
bash: wait: 63476: no such job
bash: wait: 63478: no such job
bash: wait: 63479: no such job
bash: wait: 63482: no such job
bash: wait: 63483: no such job
1 processes waited / 6 processes forked
1 seconds

zsant@Zack2021HPPavilion MSYS ~/repos/bash
$ source ~/random/wait-n-failure true
[4]   Done                    random_sleep
[2]   Done                    random_sleep
[3]   Done                    random_sleep
[5]   Done                    random_sleep
[6]   Done                    random_sleep
[7]-  Done                    random_sleep
bash: wait: 63487: no such job
bash: wait: 63488: no such job
bash: wait: 63493: no such job
bash: wait: 63494: no such job
bash: wait: 63497: no such job
1 processes waited / 6 processes forked
0 seconds

zsant@Zack2021HPPavilion MSYS ~/repos/bash
$ set +m

zsant@Zack2021HPPavilion MSYS ~/repos/bash
$ source ~/random/wait-n-failure false
6 processes waited / 11 processes forked
1 seconds

zsant@Zack2021HPPavilion MSYS ~/repos/bash
$ source ~/random/wait-n-failure false
2 processes waited / 7 processes forked
0 seconds

zsant@Zack2021HPPavilion MSYS ~/repos/bash
$ source ~/random/wait-n-failure true
bash: wait: 63545: no such job
bash: wait: 63547: no such job
bash: wait: 63550: no such job
bash: wait: 63551: no such job
bash: wait: 63545: no such job
bash: wait: 63547: no such job
bash: wait: 63550: no such job
bash: wait: 63551: no such job
bash: wait: 63557: no such job
2 processes waited / 7 processes forked
0 seconds

zsant@Zack2021HPPavilion MSYS ~/repos/bash
$ source ~/random/wait-n-failure true
bash: wait: 63562: no such job
bash: wait: 63563: no such job
bash: wait: 63562: no such job
bash: wait: 63563: no such job
bash: wait: 63567: no such job
bash: wait: 63570: no such job
bash: wait: 63562: no such job
bash: wait: 63563: no such job
bash: wait: 63567: no such job
bash: wait: 63570: no such job
bash: wait: 63562: no such job
bash: wait: 63563: no such job
bash: wait: 63567: no such job
bash: wait: 63570: no such job
bash: wait: 63562: no such job
bash: wait: 63563: no such job
bash: wait: 63567: no such job
bash: wait: 63570: no such job
bash: wait: 63582: no such job
5 processes waited / 10 processes forked
1 seconds

Attachment: wait-n-failure
Description: Binary data

Reply via email to