Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: child time accounting is different from other shells



On Wed, Sep 10, 2014 at 5:30 AM, Vincent Lefevre <vincent@xxxxxxxxxx> wrote:

>
> xvii% time sh -c 'pi 500000 > /dev/null & sleep 2'
> sh -c 'pi 500000 > /dev/null & sleep 2'  1.16s user 0.02s system 58% cpu
> 2.017 total
> xvii% time zsh -c 'pi 500000 > /dev/null & sleep 2'
> zsh -c 'pi 500000 > /dev/null & sleep 2'  0.00s user 0.00s system 0% cpu
> 2.023 total
>
> Is this a bug?


No, it isn't.

Zsh does a sort of tail-call optimization:  In the expression "left &
right", zsh can tell that there is no further need for job-control of
"left", so it does an implicit "exec right".  Other shells fork off a new
process for "right" and wait for it.

You can see the difference if you forcibly prevent the optimization by
appending a no-op command, e.g.

schaefer[101] time zsh -c 'ps & sleep 2; :'
  PID TTY           TIME CMD
47681 ttys000    0:00.47 -zsh
50366 ttys000    0:00.01 zsh -c ps & sleep 2; :
50370 ttys000    0:00.00 sleep 2
zsh -c 'ps & sleep 2; :'  0.01s user 0.01s system 1% cpu 2.019 total
schaefer[102] time zsh -c 'ps & sleep 2'
  PID TTY           TIME CMD
47681 ttys000    0:00.48 -zsh
50372 ttys000    0:00.01 sleep 2
zsh -c 'ps & sleep 2'  0.01s user 0.01s system 1% cpu 2.018 total

You can also prevent the optimization by adding an exit trap, etc.


> Shouldn't this behavior be changed to make zsh
> consistent with the other shells?
>

I would not count on this ever happening.


Messages sorted by: Reverse Date, Date, Thread, Author