Re: I don't understand the errors on this fork/exec



K-mart Cashier wrote:
On Jun 9, 1:55 pm, Eric Sosman <esos...@xxxxxxxxxxxxxxxxxxx> wrote:
K-mart Cashier wrote:
Given:
[... code snipped; see up-thread ...]
exit status = 1
First error code: : Success
Second error code: : Illegal seek
How come the first perror() prints 'Success'. but the second one
prints 'Illegal seek'?
Probably because system services used by printf() and
perror() can change the value of errno. By the time you
execute the first perror(), the printf() may already have
polluted the errno value. When you get to the second, the
first perror() may have polluted it even more.

If I would set up a signal handler to trap SIGCHLD, and then save
errno in the signal handler, would errno still be polluted by the time
I executed the first printf()?

Any system service -- indeed, any C library function --
may change the value of errno, even if the service or function
succeeds. But in a span of code where you call no library
functions or system services, errno will retain its value.

The upshot is that there are essentially two ways to deal
with errno:

- Use the errno value *immediately* after a call indicates
a failure. The "use" often amounts to calling perror()
or sometimes strerror(errno); sometimes it might involve
testing for one or two "anticipated" error conditions that
you will handle differently from others (errno==EAGAIN might
be one such case).

- Capture the errno value before doing anything that might
disturb it, and then restore it after all the potential
disturbances are past. The code often looks like

if (func(...) == FAILURE) {
int errno_save = errno;
fprintf (stderr, "Aaaaaaack!\n");
fclose (some_stream);
do_other_stuff_that_might_change_errno();
errno = errno_save;
return I_FAILED;
}

The important thing to realize is that the value of errno
is -- well, I'd say "volatile," but `volatile' has a special
meaning to C programmers. "Transient" might be a good word, or
maybe "transitory" or "evanescent." You must catch it while you
can, and not expect even a "perfectly innocuous" library call to
leave it alone.

--
Eric Sosman
esosman@xxxxxxxxxxxxxxxxxxx
.



Relevant Pages

  • Re: pthread_create returns 12?
    ... And eliminating all existing users of errno would ... > strerror() can be used to get the description of any error code. ... complicated than using perror. ...
    (comp.os.linux.development.apps)
  • Re: When to use "perror" and "fprintf"
    ... >> range and set errno to ERANGE. ... the error message output by perror ... If they're identical, display only one, otherwise display ... Dan Pop ...
    (comp.lang.c)
  • Re: perror sets errno to "Illegal Seek"
    ... Calling perror() again may print out anything, ... you call a library function you can't depend on errno not being touched. ... *if* the function reports failure *and* the specification says the function ... not ESUCCESS otherwise". ...
    (comp.os.linux.development.apps)
  • Re: How to write to a file including full directory in C under Unix?
    ... copy the literal into a buffer and then use the buffer (for ... Use perror() or errno to show what was the error. ... have an appropriate newsgroups line in your header for your mail to be seen, ...
    (comp.lang.c.moderated)
  • Re: VC++ 2005 beta1 fails with pthreads benchmark tests
    ... >>> It is the last assert which fails in the first of the failing test ... >> Why should 'perror' be expected to cause an 'EGAIN' error? ... > It is not perror which sets errno ...
    (microsoft.public.dotnet.languages.vc)