Error checking with execlp() - Unix

This is a discussion on Error checking with execlp() - Unix ; This questions stems from an off topic post in comp.lang.c...... The following appears to work correctly on my system m-net% more yes.c #include #include #include int main(void) { pid_t pid; pid=fork(); if (pid==0) { if( (execlp("who", "who", (char *)0)) perror("exec ...

+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 20 of 34

Thread: Error checking with execlp()

  1. Error checking with execlp()

    This questions stems from an off topic post in comp.lang.c......

    The following appears to work correctly on my system

    m-net% more yes.c
    #include
    #include
    #include

    int main(void)
    {
    pid_t pid;
    pid=fork();
    if (pid==0) {
    if( (execlp("who", "who", (char *)0)) < 0 ){
    perror("exec failure");
    exit(EXIT_FAILURE);
    }
    }
    return 0;

    }
    m-net% gcc -Wall yes.c -o yes
    m-net% ./yes
    sashas ttyp0 Jun 28 15:48 (91.76.233.62)
    drugr3f ttyp3 Jun 28 08:58 (124-169-99-210.d)
    raju217 ttyp5 Jun 28 14:48 (12-214-218-86.cl)
    hunterfx ttyp7 May 11 13:56 (195.161.234.152)
    mastro ttyp8 Jun 28 10:15 (83.9.73.98)
    marcvh ttypa Jun 17 20:40 (74.85.160.100)
    serg7 ttype Jun 27 19:42 (12.150.181.20)
    casper ttypf Mar 20 17:48 (207.150.165.110
    bamoha ttypl Jun 28 16:55 (ip70-162-200-27.)
    tanis ttypm Jun 28 12:37 (68.231.13.51)
    casper ttypq Mar 20 17:48 (207.150.165.110
    lightop ttypr Jun 28 16:04 (89.254.201.192)
    cdalten ttypu Jun 28 16:06 (76.21.10.82)
    haifeng ttyq0 Jun 27 10:46 (116.232.194.108)
    trex ttyq1 Jun 26 11:26 (198.108.48.82)
    m-net%


    Now, I when remove the error checking for execlp(), the code will
    sometimes work. But once in a great while it will hang after it prints
    out all the output.

    m-net% more yes.c
    #include
    #include
    #include

    int main(void)
    {
    pid_t pid;
    pid=fork();
    if (pid==0) {
    execlp("who", "who", (char *)0);
    perror("exec failure");
    exit(EXIT_FAILURE);
    }
    return 0;

    }


    m-net% ./yes
    m-net% sashas ttyp0 Jun 28 15:48 (91.76.233.62)
    drugr3f ttyp3 Jun 28 08:58 (124-169-99-210.d)
    raju217 ttyp5 Jun 28 14:48 (12-214-218-86.cl)
    hunterfx ttyp7 May 11 13:56 (195.161.234.152)
    mastro ttyp8 Jun 28 10:15 (83.9.73.98)
    marcvh ttypa Jun 17 20:40 (74.85.160.100)
    serg7 ttype Jun 27 19:42 (12.150.181.20)
    casper ttypf Mar 20 17:48 (207.150.165.110
    bamoha ttypl Jun 28 16:55 (ip70-162-200-27.)
    tanis ttypm Jun 28 12:37 (68.231.13.51)
    casper ttypq Mar 20 17:48 (207.150.165.110
    lightop ttypr Jun 28 16:04 (89.254.201.192)
    cdalten ttypu Jun 28 16:06 (76.21.10.82)
    haifeng ttyq0 Jun 27 10:46 (116.232.194.108)
    trex ttyq1 Jun 26 11:26 (198.108.48.82)


    Right afer it prints the line trex, the system will just hang. Ie, I
    won't get the shell prompt. Why is this?

  2. Re: Error checking with execlp()

    K-mart Cashier writes:

    > This questions stems from an off topic post in comp.lang.c......
    >
    > The following appears to work correctly on my system
    >
    > m-net% more yes.c
    > #include
    > #include
    > #include
    >
    > int main(void)
    > {
    > pid_t pid;
    > pid=fork();
    > if (pid==0) {
    > if( (execlp("who", "who", (char *)0)) < 0 ){


    As someone has apparently already told you, the error check is pointless,
    since exec() only returns on errors.

    > perror("exec failure");


    You shouldn't call stdio functions from a child process without
    flushing the streams before the fork() call.

    > exit(EXIT_FAILURE);


    You should also not use exit() in this context. Use _exit() or
    _Exit() instead.

    > }
    > }
    > return 0;
    >
    > }
    > m-net% gcc -Wall yes.c -o yes
    > m-net% ./yes
    > sashas ttyp0 Jun 28 15:48 (91.76.233.62)
    > drugr3f ttyp3 Jun 28 08:58 (124-169-99-210.d)
    > raju217 ttyp5 Jun 28 14:48 (12-214-218-86.cl)
    > hunterfx ttyp7 May 11 13:56 (195.161.234.152)
    > mastro ttyp8 Jun 28 10:15 (83.9.73.98)
    > marcvh ttypa Jun 17 20:40 (74.85.160.100)
    > serg7 ttype Jun 27 19:42 (12.150.181.20)
    > casper ttypf Mar 20 17:48 (207.150.165.110
    > bamoha ttypl Jun 28 16:55 (ip70-162-200-27.)
    > tanis ttypm Jun 28 12:37 (68.231.13.51)
    > casper ttypq Mar 20 17:48 (207.150.165.110
    > lightop ttypr Jun 28 16:04 (89.254.201.192)
    > cdalten ttypu Jun 28 16:06 (76.21.10.82)
    > haifeng ttyq0 Jun 27 10:46 (116.232.194.108)
    > trex ttyq1 Jun 26 11:26 (198.108.48.82)
    > m-net%
    >
    > Now, I when remove the error checking for execlp(), the code will
    > sometimes work. But once in a great while it will hang after it prints
    > out all the output.
    >
    > m-net% more yes.c
    > #include
    > #include
    > #include
    >
    > int main(void)
    > {
    > pid_t pid;
    > pid=fork();
    > if (pid==0) {
    > execlp("who", "who", (char *)0);
    > perror("exec failure");
    > exit(EXIT_FAILURE);
    > }
    > return 0;
    >
    > }
    >
    > m-net% ./yes
    > m-net% sashas ttyp0 Jun 28 15:48 (91.76.233.62)

    ^^^^^^
    > drugr3f ttyp3 Jun 28 08:58 (124-169-99-210.d)
    > raju217 ttyp5 Jun 28 14:48 (12-214-218-86.cl)
    > hunterfx ttyp7 May 11 13:56 (195.161.234.152)
    > mastro ttyp8 Jun 28 10:15 (83.9.73.98)
    > marcvh ttypa Jun 17 20:40 (74.85.160.100)
    > serg7 ttype Jun 27 19:42 (12.150.181.20)
    > casper ttypf Mar 20 17:48 (207.150.165.110
    > bamoha ttypl Jun 28 16:55 (ip70-162-200-27.)
    > tanis ttypm Jun 28 12:37 (68.231.13.51)
    > casper ttypq Mar 20 17:48 (207.150.165.110
    > lightop ttypr Jun 28 16:04 (89.254.201.192)
    > cdalten ttypu Jun 28 16:06 (76.21.10.82)
    > haifeng ttyq0 Jun 27 10:46 (116.232.194.108)
    > trex ttyq1 Jun 26 11:26 (198.108.48.82)
    >
    > Right afer it prints the line trex, the system will just hang. Ie, I
    > won't get the shell prompt. Why is this?


    What you see has nothing to do with the error checking. After the
    fork(), it is undefined in which order the processes get to run. Your
    system apparently tends to schedule the child immediately, and it
    usually finishes before the parent gets to run again. Once in a
    while, the parent runs first (this could be a side-effect of other
    system activity), and exits before the child has started. When this
    happens, you immediately get the shell prompt, followed by the output
    from the child process.

    Adding a wait() call before returning from the parent should take care
    of synchronisation between the processes.

    --
    MŚns RullgŚrd
    mans@mansr.com

  3. Re: Error checking with execlp()

    On Jun 28, 3:14*pm, MŚns RullgŚrd wrote:
    > K-mart Cashier writes:
    > > This questions stems from an off topic post in comp.lang.c......

    >
    > > The following appears to work correctly on my system

    >
    > > m-net% more yes.c
    > > #include
    > > #include
    > > #include

    >
    > > int main(void)
    > > {
    > > * pid_t pid;
    > > * pid=fork();
    > > * if (pid==0) {
    > > * * * if( (execlp("who", "who", (char *)0)) < 0 ){

    >
    > As someone has apparently already told you, the error check is pointless,
    > since exec() only returns on errors.
    >


    Huh? Maybe I was really missing it, but I don't ever recall anyone
    telling me the error check was pointless.

  4. Re: Error checking with execlp()

    K-mart Cashier writes:

    > On Jun 28, 3:14*pm, MŚns RullgŚrd wrote:
    >> K-mart Cashier writes:
    >> > This questions stems from an off topic post in comp.lang.c......

    >>
    >> > The following appears to work correctly on my system

    >>
    >> > m-net% more yes.c
    >> > #include
    >> > #include
    >> > #include

    >>
    >> > int main(void)
    >> > {
    >> > * pid_t pid;
    >> > * pid=fork();
    >> > * if (pid==0) {
    >> > * * * if( (execlp("who", "who", (char *)0)) < 0 ){

    >>
    >> As someone has apparently already told you, the error check is pointless,
    >> since exec() only returns on errors.

    >
    > Huh? Maybe I was really missing it, but I don't ever recall anyone
    > telling me the error check was pointless.


    I assumed this was the case, since the rest of the post dealt with
    removing the redundant check.

    --
    MŚns RullgŚrd
    mans@mansr.com

  5. Re: Error checking with execlp()

    K-mart Cashier wrote:
    > On Jun 28, 3:14¬*pm, M√•ns Rullg√•rd wrote:
    > > K-mart Cashier writes:
    > > > This questions stems from an off topic post in comp.lang.c......

    > >
    > > > The following appears to work correctly on my system

    > >
    > > > m-net% more yes.c
    > > > #include
    > > > #include
    > > > #include

    > >
    > > > int main(void)
    > > > {
    > > > ¬* pid_t pid;
    > > > ¬* pid=fork();
    > > > ¬* if (pid==0) {
    > > > ¬* ¬* ¬* if( (execlp("who", "who", (char *)0)) < 0 ){

    > >
    > > As someone has apparently already told you, the error check is pointless,
    > > since exec() only returns on errors.
    > >

    > Huh? Maybe I was really missing it, but I don't ever recall anyone
    > telling me the error check was pointless.


    Here's one of the very few cases where checking for a return value
    doesn't make too much sense. If execlp() (or any function of the
    exec() family) returns at all you can be sure that something went
    wrong. It doesn't matter what value gets returned, the mere fact
    that something after the call is executed is enough to know that
    an error happened.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ jt@toerring.de
    \__________________________ http://toerring.de

  6. Re: Error checking with execlp()

    On Jun 28, 5:19*pm, j...@toerring.de (Jens Thoms Toerring) wrote:
    > K-mart Cashier wrote:
    > > On Jun 28, 3:14*pm, MŚns RullgŚrd wrote:
    > > > K-mart Cashier writes:
    > > > > This questions stems from an off topic post in comp.lang.c......

    >
    > > > > The following appears to work correctly on my system

    >
    > > > > m-net% more yes.c
    > > > > #include
    > > > > #include
    > > > > #include

    >
    > > > > int main(void)
    > > > > {
    > > > > * pid_t pid;
    > > > > * pid=fork();
    > > > > * if (pid==0) {
    > > > > * * * if( (execlp("who", "who", (char *)0)) < 0 ){

    >
    > > > As someone has apparently already told you, the error check is pointless,
    > > > since exec() only returns on errors.

    >
    > > Huh? Maybe I was really missing it, but I don't ever recall anyone
    > > telling me the error check was pointless.

    >
    > Here's one of the very few cases where checking for a return value
    > doesn't make too much sense. If execlp() (or any function of the
    > exec() family) returns at all you can be sure that something went
    > wrong. It doesn't matter what value gets returned, the mere fact
    > that something after the call is executed is enough to know that
    > an error happened.
    > * * * * * * * * * * * * * * * *Regards, Jens



    Okay. I need a moment for that to sink into my snoodle.

  7. Re: Error checking with execlp()

    >> Here's one of the very few cases where checking for a return value
    >> doesn't make too much sense. If execlp() (or any function of the
    >> exec() family) returns at all you can be sure that something went
    >> wrong. It doesn't matter what value gets returned, the mere fact
    >> that something after the call is executed is enough to know that
    >> an error happened.
    >> * * * * * * * * * * * * * * * *Regards, Jens

    >
    >
    >Okay. I need a moment for that to sink into my snoodle.


    That means, for example, that it is not necessary to check the return
    value of exit() :-). If it returned, you have an error. Now try to figure
    out how to report it and what to do afterwords.


  8. Re: Error checking with execlp()

    In article <6co2otF3hld3pU1@mid.uni-berlin.de>,
    jt@toerring.de (Jens Thoms Toerring) wrote:

    > K-mart Cashier wrote:
    > > On Jun 28, 3:14¬*pm, M√•ns Rullg√•rd wrote:
    > > > K-mart Cashier writes:
    > > > > This questions stems from an off topic post in comp.lang.c......
    > > >
    > > > > The following appears to work correctly on my system
    > > >
    > > > > m-net% more yes.c
    > > > > #include
    > > > > #include
    > > > > #include
    > > >
    > > > > int main(void)
    > > > > {
    > > > > ¬* pid_t pid;
    > > > > ¬* pid=fork();
    > > > > ¬* if (pid==0) {
    > > > > ¬* ¬* ¬* if( (execlp("who", "who", (char *)0)) < 0 ){
    > > >
    > > > As someone has apparently already told you, the error check is pointless,
    > > > since exec() only returns on errors.
    > > >

    > > Huh? Maybe I was really missing it, but I don't ever recall anyone
    > > telling me the error check was pointless.

    >
    > Here's one of the very few cases where checking for a return value
    > doesn't make too much sense. If execlp() (or any function of the
    > exec() family) returns at all you can be sure that something went
    > wrong. It doesn't matter what value gets returned, the mere fact
    > that something after the call is executed is enough to know that
    > an error happened.


    I think you folks are making a big deal out of nothing. Sure, checking
    the return value is unnecessary. But it's a good habit to remember to
    check system call return values. This one case is redundant, but who
    cares?

    I think you're just trying to be too clever by pointing this out,
    especially since it's totally unrelated to the meat of his question so
    it's confusing the OP more.

    --
    Barry Margolin, barmar@alum.mit.edu
    Arlington, MA
    *** PLEASE post questions in newsgroups, not directly to me ***
    *** PLEASE don't copy me on replies, I'll read them in the group ***

  9. Re: Error checking with execlp()

    Barry Margolin writes:
    > jt@toerring.de (Jens Thoms Toerring) wrote:
    >> K-mart Cashier wrote:
    >> > On Jun 28, 3:14¬*pm, M√•ns Rullg√•rd wrote:
    >> > > K-mart Cashier writes:
    >> > > > This questions stems from an off topic post in comp.lang.c......
    >> > >
    >> > > > The following appears to work correctly on my system
    >> > >
    >> > > > m-net% more yes.c
    >> > > > #include
    >> > > > #include
    >> > > > #include
    >> > >
    >> > > > int main(void)
    >> > > > {
    >> > > > ¬* pid_t pid;
    >> > > > ¬* pid=fork();
    >> > > > ¬* if (pid==0) {
    >> > > > ¬* ¬* ¬* if( (execlp("who", "who", (char *)0)) < 0 ){
    >> > >
    >> > > As someone has apparently already told you, the error check is pointless,
    >> > > since exec() only returns on errors.
    >> > >
    >> > Huh? Maybe I was really missing it, but I don't ever recall anyone
    >> > telling me the error check was pointless.

    >>
    >> Here's one of the very few cases where checking for a return value
    >> doesn't make too much sense. If execlp() (or any function of the
    >> exec() family) returns at all you can be sure that something went
    >> wrong. It doesn't matter what value gets returned, the mere fact
    >> that something after the call is executed is enough to know that
    >> an error happened.

    >
    > I think you folks are making a big deal out of nothing. Sure, checking
    > the return value is unnecessary. But it's a good habit to remember to
    > check system call return values. This one case is redundant, but who
    > cares?


    I see no real difference between

    rc = execlp("program", "program", (char *)0);
    if (rc == -1) ...

    and

    a = 3;
    if (a == 3) ...


  10. Re: Error checking with execlp()

    In article <871w2gqufl.fsf@fever.mssgmbh.com>,
    Rainer Weikusat wrote:

    > Barry Margolin writes:
    > > jt@toerring.de (Jens Thoms Toerring) wrote:
    > >> K-mart Cashier wrote:
    > >> > On Jun 28, 3:14¬*pm, M√•ns Rullg√•rd wrote:
    > >> > > K-mart Cashier writes:
    > >> > > > This questions stems from an off topic post in comp.lang.c......
    > >> > >
    > >> > > > The following appears to work correctly on my system
    > >> > >
    > >> > > > m-net% more yes.c
    > >> > > > #include
    > >> > > > #include
    > >> > > > #include
    > >> > >
    > >> > > > int main(void)
    > >> > > > {
    > >> > > > ¬* pid_t pid;
    > >> > > > ¬* pid=fork();
    > >> > > > ¬* if (pid==0) {
    > >> > > > ¬* ¬* ¬* if( (execlp("who", "who", (char *)0)) < 0 ){
    > >> > >
    > >> > > As someone has apparently already told you, the error check is
    > >> > > pointless,
    > >> > > since exec() only returns on errors.
    > >> > >
    > >> > Huh? Maybe I was really missing it, but I don't ever recall anyone
    > >> > telling me the error check was pointless.
    > >>
    > >> Here's one of the very few cases where checking for a return value
    > >> doesn't make too much sense. If execlp() (or any function of the
    > >> exec() family) returns at all you can be sure that something went
    > >> wrong. It doesn't matter what value gets returned, the mere fact
    > >> that something after the call is executed is enough to know that
    > >> an error happened.

    > >
    > > I think you folks are making a big deal out of nothing. Sure, checking
    > > the return value is unnecessary. But it's a good habit to remember to
    > > check system call return values. This one case is redundant, but who
    > > cares?

    >
    > I see no real difference between
    >
    > rc = execlp("program", "program", (char *)0);
    > if (rc == -1) ...
    >
    > and
    >
    > a = 3;
    > if (a == 3) ...
    >


    That's ridiculous. a = 3 doesn't do anything other than set a to 3.

    On the other hand, execlp() always returning -1 is a secondary
    consequence, not the specific purpose of the function. Also, an
    assignment isn't a system call. As a general rule, system calls should
    be checked for errors, while assignments don't need to be. The fact
    that execlp() doesn't need to be is a very special, unusual case.

    Let me turn this around: why does execlp() return -1 on error? The
    POSIX authors could have said that the return value is unspecified,
    because just returning indicates an error. But they presumably wanted
    to make it consistent with most other system calls. And it's not
    unreasonable for callers to follow in this consistency.

    --
    Barry Margolin, barmar@alum.mit.edu
    Arlington, MA
    *** PLEASE post questions in newsgroups, not directly to me ***
    *** PLEASE don't copy me on replies, I'll read them in the group ***

  11. Re: Error checking with execlp()

    Barry Margolin writes:
    > Rainer Weikusat wrote:


    [...]

    >> > I think you folks are making a big deal out of nothing. Sure, checking
    >> > the return value is unnecessary. But it's a good habit to remember to
    >> > check system call return values. This one case is redundant, but who
    >> > cares?

    >>
    >> I see no real difference between
    >>
    >> rc = execlp("program", "program", (char *)0);
    >> if (rc == -1) ...
    >>
    >> and
    >>
    >> a = 3;
    >> if (a == 3) ...
    >>

    >
    > That's ridiculous. a = 3 doesn't do anything other than set a to 3.
    > On the other hand, execlp() always returning -1 is a secondary
    > consequence, not the specific purpose of the function.


    Part of the specific purpose of the function is that only errors can
    ever cause it return.

    > Also, an assignment isn't a system call. As a general rule, system
    > calls should be checked for errors, while assignments don't need to
    > be.


    Any subroutine has some kind of 'communication protocol' attached
    to it which defines some constraints regarding its inputs and
    specifies something regarding its output(s). Code using the subroutine
    should try to do 'something sensible' for any possible output,
    specifcally including outputs (indirectly) caused by programming errors
    elsewhere, because this makes debugging a lot easier (and limits the
    possible 'damage' caused by error propagation). But again, each of the
    exec*-routines has exactly one 'possible output': An error has
    ocurred. So, what's the point of writing code to check that exec*
    returned because of an error?

    > Let me turn this around: why does execlp() return -1 on error? The
    > POSIX authors could have said that the return value is unspecified,
    > because just returning indicates an error. But they presumably
    > wanted to make it consistent with most other system calls.


    The POSIX authors didn't 'make' this system call. They inherited
    it. And they didn't change the inherited semantics because it would
    have been possible to define as sensible 'other semantics'. That's
    always possible.

    > And it's not unreasonable for callers to follow in this consistency.


    There is no universally consistent convention regarding 'UNIX(*) API
    call outputs' (eg fopen, mmap, strtol, gethostbyname).


  12. Re: Error checking with execlp()

    On Sun, 29 Jun 2008 23:02:13 -0400 Barry Margolin wrote:
    | In article <871w2gqufl.fsf@fever.mssgmbh.com>,
    | Rainer Weikusat wrote:

    [...]

    |> I see no real difference between
    |>
    |> rc = execlp("program", "program", (char *)0);
    |> if (rc == -1) ...
    |>
    |> and
    |>
    |> a = 3;
    |> if (a == 3) ...
    |>
    |
    | That's ridiculous. a = 3 doesn't do anything other than set a to 3.

    I think the point is that in both cases the conditional is unneeded. In the
    latter case I would expect the compiler to remove the conditional for all but
    the leaset level of optimization (and even then I would not be surprised).
    In the former case, a compiler built _for_ a POSIX environment might do it
    there as well, especially at higher optimizations.


    | On the other hand, execlp() always returning -1 is a secondary
    | consequence, not the specific purpose of the function. Also, an
    | assignment isn't a system call. As a general rule, system calls should
    | be checked for errors, while assignments don't need to be. The fact
    | that execlp() doesn't need to be is a very special, unusual case.

    For sure, the "..." part will be executed always in the latter case and rarely
    in the former case. But what about:

    a = 3;
    rc = execlp("program", "program", (char *)0);
    if ( rc == -1 && a == 3 ) ...


    | Let me turn this around: why does execlp() return -1 on error? The
    | POSIX authors could have said that the return value is unspecified,
    | because just returning indicates an error. But they presumably wanted
    | to make it consistent with most other system calls. And it's not
    | unreasonable for callers to follow in this consistency.

    I never test the return code for an execXX() call because it's definitely
    an error with respect to my program the fact that it returns, regardless
    of what the return value is. It's errno that holds useful info, though it
    could be argued that if execXX() returned 0 then errno would not have been
    set.

    Or in a simpler case:

    exit( -1 == execlp("program", "program", (char *)0) );

    vs.

    exit( execlp("program", "program", (char *)0) ), 1 );

    --
    |WARNING: Due to extreme spam, googlegroups.com is blocked. Due to ignorance |
    | by the abuse department, bellsouth.net is blocked. If you post to |
    | Usenet from these places, find another Usenet provider ASAP. |
    | Phil Howard KA9WGN (email for humans: first name in lower case at ipal.net) |

  13. Re: Error checking with execlp()

    In article <87hcbbz7z5.fsf@fever.mssgmbh.com>,
    Rainer Weikusat wrote:

    > Barry Margolin writes:
    > > Rainer Weikusat wrote:

    >
    > [...]
    >
    > >> > I think you folks are making a big deal out of nothing. Sure, checking
    > >> > the return value is unnecessary. But it's a good habit to remember to
    > >> > check system call return values. This one case is redundant, but who
    > >> > cares?
    > >>
    > >> I see no real difference between
    > >>
    > >> rc = execlp("program", "program", (char *)0);
    > >> if (rc == -1) ...
    > >>
    > >> and
    > >>
    > >> a = 3;
    > >> if (a == 3) ...
    > >>

    > >
    > > That's ridiculous. a = 3 doesn't do anything other than set a to 3.
    > > On the other hand, execlp() always returning -1 is a secondary
    > > consequence, not the specific purpose of the function.

    >
    > Part of the specific purpose of the function is that only errors can
    > ever cause it return.


    I disagree. The specific purpose of the function is to load and run a
    program into the process. It's a logical consequence of this that it
    can't return unless it gets an error, but that's not what it's intended
    to do.

    By your reasoning, all the things that some function can't do are part
    of its specific purpose. When you write a call to read(), are you
    really thinking about the fact that it WON'T modify the file's contents?
    But that's certainly guaranteed by its definition, so I guess it's its
    specific purpose.

    --
    Barry Margolin, barmar@alum.mit.edu
    Arlington, MA
    *** PLEASE post questions in newsgroups, not directly to me ***
    *** PLEASE don't copy me on replies, I'll read them in the group ***

  14. Re: Error checking with execlp()

    In article , phil-news-nospam@ipal.net
    wrote:

    > On Sun, 29 Jun 2008 23:02:13 -0400 Barry Margolin wrote:
    > | In article <871w2gqufl.fsf@fever.mssgmbh.com>,
    > | Rainer Weikusat wrote:
    >
    > [...]
    >
    > |> I see no real difference between
    > |>
    > |> rc = execlp("program", "program", (char *)0);
    > |> if (rc == -1) ...
    > |>
    > |> and
    > |>
    > |> a = 3;
    > |> if (a == 3) ...
    > |>
    > |
    > | That's ridiculous. a = 3 doesn't do anything other than set a to 3.
    >
    > I think the point is that in both cases the conditional is unneeded. In the
    > latter case I would expect the compiler to remove the conditional for all but
    > the leaset level of optimization (and even then I would not be surprised).
    > In the former case, a compiler built _for_ a POSIX environment might do it
    > there as well, especially at higher optimizations.


    Are you really concerned about optimizing the error case of a program?
    99% of the time the error handling code after a call to exec() will
    never be run. And when it is, do we really care how quickly it reports
    the error?

    --
    Barry Margolin, barmar@alum.mit.edu
    Arlington, MA
    *** PLEASE post questions in newsgroups, not directly to me ***
    *** PLEASE don't copy me on replies, I'll read them in the group ***

  15. Re: Error checking with execlp()

    Barry Margolin writes:
    > In article , phil-news-nospam@ipal.net
    > wrote:
    >
    >> On Sun, 29 Jun 2008 23:02:13 -0400 Barry Margolin wrote:
    >> | In article <871w2gqufl.fsf@fever.mssgmbh.com>,
    >> | Rainer Weikusat wrote:
    >>
    >> [...]
    >>
    >> |> I see no real difference between
    >> |>
    >> |> rc = execlp("program", "program", (char *)0);
    >> |> if (rc == -1) ...
    >> |>
    >> |> and
    >> |>
    >> |> a = 3;
    >> |> if (a == 3) ...
    >> |>
    >> |
    >> | That's ridiculous. a = 3 doesn't do anything other than set a to 3.
    >>
    >> I think the point is that in both cases the conditional is unneeded. In the
    >> latter case I would expect the compiler to remove the conditional for all but
    >> the leaset level of optimization (and even then I would not be surprised).
    >> In the former case, a compiler built _for_ a POSIX environment might do it
    >> there as well, especially at higher optimizations.

    >
    > Are you really concerned about optimizing the error case of a
    > program?


    I am really concerned with not writing code which doesn't really
    perform some useful action. It's more work to do so initially and
    it makes the resulting text more complicated to understand. In
    independent reader is not going to know a priori that some
    conditionals are habitually being used in the wrong places, but needs
    to understand them before knowing that he could have save the effort.


  16. Re: Error checking with execlp()

    Barry Margolin writes:
    > In article <87hcbbz7z5.fsf@fever.mssgmbh.com>,
    > Rainer Weikusat wrote:
    >> Barry Margolin writes:
    >> > Rainer Weikusat wrote:

    >>
    >> [...]
    >>
    >> >> > I think you folks are making a big deal out of nothing. Sure, checking
    >> >> > the return value is unnecessary. But it's a good habit to remember to
    >> >> > check system call return values. This one case is redundant, but who
    >> >> > cares?
    >> >>
    >> >> I see no real difference between
    >> >>
    >> >> rc = execlp("program", "program", (char *)0);
    >> >> if (rc == -1) ...
    >> >>
    >> >> and
    >> >>
    >> >> a = 3;
    >> >> if (a == 3) ...
    >> >>
    >> >
    >> > That's ridiculous. a = 3 doesn't do anything other than set a to 3.
    >> > On the other hand, execlp() always returning -1 is a secondary
    >> > consequence, not the specific purpose of the function.

    >>
    >> Part of the specific purpose of the function is that only errors can
    >> ever cause it return.

    >
    > I disagree. The specific purpose of the function is to load and run a
    > program into the process. It's a logical consequence of this that it
    > can't return unless it gets an error, but that's not what it's intended
    > to do.


    Of course it is intended that this function only returns in case of an
    error.

  17. Re: Error checking with execlp()

    In article <87y74mdl56.fsf@fever.mssgmbh.com>,
    Rainer Weikusat wrote:

    > Barry Margolin writes:
    > > In article , phil-news-nospam@ipal.net
    > > wrote:
    > >
    > >> On Sun, 29 Jun 2008 23:02:13 -0400 Barry Margolin
    > >> wrote:
    > >> | In article <871w2gqufl.fsf@fever.mssgmbh.com>,
    > >> | Rainer Weikusat wrote:
    > >>
    > >> [...]
    > >>
    > >> |> I see no real difference between
    > >> |>
    > >> |> rc = execlp("program", "program", (char *)0);
    > >> |> if (rc == -1) ...
    > >> |>
    > >> |> and
    > >> |>
    > >> |> a = 3;
    > >> |> if (a == 3) ...
    > >> |>
    > >> |
    > >> | That's ridiculous. a = 3 doesn't do anything other than set a to 3.
    > >>
    > >> I think the point is that in both cases the conditional is unneeded. In
    > >> the
    > >> latter case I would expect the compiler to remove the conditional for all
    > >> but
    > >> the leaset level of optimization (and even then I would not be surprised).
    > >> In the former case, a compiler built _for_ a POSIX environment might do it
    > >> there as well, especially at higher optimizations.

    > >
    > > Are you really concerned about optimizing the error case of a
    > > program?

    >
    > I am really concerned with not writing code which doesn't really
    > perform some useful action.


    Then why did you bring up the issue of the compiler removing code?
    That's about optimization.

    --
    Barry Margolin, barmar@alum.mit.edu
    Arlington, MA
    *** PLEASE post questions in newsgroups, not directly to me ***
    *** PLEASE don't copy me on replies, I'll read them in the group ***

  18. Re: Error checking with execlp()

    Barry Margolin writes:
    > In article <87y74mdl56.fsf@fever.mssgmbh.com>,
    > Rainer Weikusat wrote:
    >> Barry Margolin writes:
    >> > In article , phil-news-nospam@ipal.net
    >> > wrote:
    >> >
    >> >> On Sun, 29 Jun 2008 23:02:13 -0400 Barry Margolin
    >> >> wrote:
    >> >> | In article <871w2gqufl.fsf@fever.mssgmbh.com>,
    >> >> | Rainer Weikusat wrote:
    >> >>
    >> >> [...]
    >> >>
    >> >> |> I see no real difference between
    >> >> |>
    >> >> |> rc = execlp("program", "program", (char *)0);
    >> >> |> if (rc == -1) ...
    >> >> |>
    >> >> |> and
    >> >> |>
    >> >> |> a = 3;
    >> >> |> if (a == 3) ...
    >> >> |>
    >> >> |
    >> >> | That's ridiculous. a = 3 doesn't do anything other than set a to 3.
    >> >>
    >> >> I think the point is that in both cases the conditional is unneeded. In
    >> >> the
    >> >> latter case I would expect the compiler to remove the conditional for all
    >> >> but
    >> >> the leaset level of optimization (and even then I would not be surprised).
    >> >> In the former case, a compiler built _for_ a POSIX environment might do it
    >> >> there as well, especially at higher optimizations.
    >> >
    >> > Are you really concerned about optimizing the error case of a
    >> > program?

    >>
    >> I am really concerned with not writing code which doesn't really
    >> perform some useful action.

    >
    > Then why did you bring up the issue of the compiler removing code?
    > That's about optimization.


    I didn't.

  19. Re: Error checking with execlp()

    On Mon, 30 Jun 2008 21:06:42 -0400 Barry Margolin wrote:
    | In article , phil-news-nospam@ipal.net
    | wrote:
    |
    |> On Sun, 29 Jun 2008 23:02:13 -0400 Barry Margolin wrote:
    |> | In article <871w2gqufl.fsf@fever.mssgmbh.com>,
    |> | Rainer Weikusat wrote:
    |>
    |> [...]
    |>
    |> |> I see no real difference between
    |> |>
    |> |> rc = execlp("program", "program", (char *)0);
    |> |> if (rc == -1) ...
    |> |>
    |> |> and
    |> |>
    |> |> a = 3;
    |> |> if (a == 3) ...
    |> |>
    |> |
    |> | That's ridiculous. a = 3 doesn't do anything other than set a to 3.
    |>
    |> I think the point is that in both cases the conditional is unneeded. In the
    |> latter case I would expect the compiler to remove the conditional for all but
    |> the leaset level of optimization (and even then I would not be surprised).
    |> In the former case, a compiler built _for_ a POSIX environment might do it
    |> there as well, especially at higher optimizations.
    |
    | Are you really concerned about optimizing the error case of a program?
    | 99% of the time the error handling code after a call to exec() will
    | never be run. And when it is, do we really care how quickly it reports
    | the error?

    It's not about _wanting_ optimization; it's about the fact that optimization
    (even when it's unimportant) showing you want isn't doing anything useful.
    The optimization has to still be correct. If something gets optimized out,
    then it was "correct" (in the sense that the code does the same thing, not
    necessarily in well written code) to leave out that code.

    --
    |WARNING: Due to extreme spam, googlegroups.com is blocked. Due to ignorance |
    | by the abuse department, bellsouth.net is blocked. If you post to |
    | Usenet from these places, find another Usenet provider ASAP. |
    | Phil Howard KA9WGN (email for humans: first name in lower case at ipal.net) |

  20. Re: Error checking with execlp()

    On Wed, 02 Jul 2008 07:33:33 +0200 Rainer Weikusat wrote:
    | Barry Margolin writes:
    |> In article <87y74mdl56.fsf@fever.mssgmbh.com>,
    |> Rainer Weikusat wrote:
    |>> Barry Margolin writes:
    |>> > In article , phil-news-nospam@ipal.net
    |>> > wrote:
    |>> >
    |>> >> On Sun, 29 Jun 2008 23:02:13 -0400 Barry Margolin
    |>> >> wrote:
    |>> >> | In article <871w2gqufl.fsf@fever.mssgmbh.com>,
    |>> >> | Rainer Weikusat wrote:
    |>> >>
    |>> >> [...]
    |>> >>
    |>> >> |> I see no real difference between
    |>> >> |>
    |>> >> |> rc = execlp("program", "program", (char *)0);
    |>> >> |> if (rc == -1) ...
    |>> >> |>
    |>> >> |> and
    |>> >> |>
    |>> >> |> a = 3;
    |>> >> |> if (a == 3) ...
    |>> >> |>
    |>> >> |
    |>> >> | That's ridiculous. a = 3 doesn't do anything other than set a to 3.
    |>> >>
    |>> >> I think the point is that in both cases the conditional is unneeded. In
    |>> >> the
    |>> >> latter case I would expect the compiler to remove the conditional for all
    |>> >> but
    |>> >> the leaset level of optimization (and even then I would not be surprised).
    |>> >> In the former case, a compiler built _for_ a POSIX environment might do it
    |>> >> there as well, especially at higher optimizations.
    |>> >
    |>> > Are you really concerned about optimizing the error case of a
    |>> > program?
    |>>
    |>> I am really concerned with not writing code which doesn't really
    |>> perform some useful action.
    |>
    |> Then why did you bring up the issue of the compiler removing code?
    |> That's about optimization.
    |
    | I didn't.

    I did. And it wasn't brought for optimization. It was brought up to show
    what optimization would do. I don't care if the error handler is faster or
    slower. What I care is whether or not it is (still) correct when some piece
    of code is omitted. Optimization has to be confined to a space of correctness.

    --
    |WARNING: Due to extreme spam, googlegroups.com is blocked. Due to ignorance |
    | by the abuse department, bellsouth.net is blocked. If you post to |
    | Usenet from these places, find another Usenet provider ASAP. |
    | Phil Howard KA9WGN (email for humans: first name in lower case at ipal.net) |

+ Reply to Thread
Page 1 of 2 1 2 LastLast