Re: I don't understand the errors on this fork/exec
- From: Eric Sosman <esosman@xxxxxxxxxxxxxxxxxxx>
- Date: Sat, 09 Jun 2007 21:24:33 -0400
K-mart Cashier wrote:
On Jun 9, 1:55 pm, Eric Sosman <esos...@xxxxxxxxxxxxxxxxxxx> wrote:K-mart Cashier wrote:If I would set up a signal handler to trap SIGCHLD, and then saveGiven:Probably because system services used by printf() and
[... 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'?
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.
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
.
- References:
- I don't understand the errors on this fork/exec
- From: K-mart Cashier
- Re: I don't understand the errors on this fork/exec
- From: Eric Sosman
- Re: I don't understand the errors on this fork/exec
- From: K-mart Cashier
- I don't understand the errors on this fork/exec
- Prev by Date: Re: parsing a command from a config file
- Next by Date: Re: I don't understand the errors on this fork/exec
- Previous by thread: Re: I don't understand the errors on this fork/exec
- Next by thread: Re: I don't understand the errors on this fork/exec
- Index(es):
Relevant Pages
|