(0000414)
msbrown (manager)
2010-05-06 15:12
|
David Korn notes:
This is historical practice and a rather important optimization.
The system(string) function invokes sh -c string and is often
called with a string that is just a simple command. The performace
would be impaired if this caused two forks() rather than just one.
However, there are many circumstances where the optimization should
not be allowed since it would affect the behavior of a script.
For example, if the user has set a trap on EXIT, the last command
cannot exec since this would prevent the trap from taking place.
Note, that the script writer can always prevent the optimization by
adding
exit $?
to the end of the script.
With at least ksh88 and ksh93 (and I suspect others),
sh -c 'exec cat myfile'
will not save a process since the shell will recognized that this is
the last process and then exec cat without forking.
Moreover, if cat is implemented as a builtin, it will be slower
than
sh -c 'cat myfile'
In fact, I am unaware of any case where exec will save a process
creation. If I could think of one, I would look at how to
detect this without exec. |