View Issue Details

IDProjectCategoryView StatusLast Update
00002361003.1(2008)/Issue 7Shell and Utilitiespublic2010-05-06 15:12
Reportergeoffclare Assigned Toajosey  
PrioritynormalSeverityCommentTypeOmission
Status ResolvedResolutionRejected 
NameGeoff Clare
OrganizationThe Open Group
User Reference
Sectionsh
Page Number3177
Line Number105795
Interp Status---
Final Accepted Text
Summary0000236: additional sh -c example that uses exec
DescriptionIt would be useful to show an example of sh -c that uses exec
to avoid an extra process.
Desired ActionAfter example 1 insert a new example 2:
    2. As example 1, but with the shell being replaced by cat instead
       of creating a new process for cat:

       sh -c "exec cat myfile"

Renumber the current example 2 to be 3.
TagsNo tags attached.

Activities

msbrown

2010-05-06 15:12

manager   bugnote:0000414

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.

Issue History

Date Modified Username Field Change
2010-04-01 09:34 geoffclare New Issue
2010-04-01 09:34 geoffclare Status New => Under Review
2010-04-01 09:34 geoffclare Assigned To => ajosey
2010-04-01 09:34 geoffclare Name => Geoff Clare
2010-04-01 09:34 geoffclare Organization => The Open Group
2010-04-01 09:34 geoffclare Section => sh
2010-04-01 09:34 geoffclare Page Number => 3177
2010-04-01 09:34 geoffclare Line Number => 105795
2010-04-01 09:34 geoffclare Interp Status => ---
2010-05-06 15:12 msbrown Note Added: 0000414
2010-05-06 15:12 msbrown Status Under Review => Resolved
2010-05-06 15:12 msbrown Resolution Open => Rejected