I've found this piece of code used several times (also a similar one where it's used open()
instead of write()
).
int c = write(fd, &v, sizeof(v)); if (c == -1 && errno != EINTR) { perror("Write to output file"); exit(EXIT_FAILURE); }
Why it is checked if && errno != EINTR
here ?
Looking for errno
on man I found the following text about EINTR
, but even if I visited man 7 signal
that doesn't enlighten me.
EINTR Interrupted function call (POSIX.1); see signal(7).
EINTR is the error which so-called interruptible system calls may return. If a signal occurs while a system call is running, that signal is not ignored. If a signal handler was defined for it without SA_RESTART set and this handler handles that signal, then the system call will return the EINTR error code.
Viewing and Printing the Errno Value Your program can use the strerror() and perror() functions to print the value of errno. The strerror() function returns a pointer to an error message string that is associated with errno. The perror() function prints a message to stderr.
errno is a preprocessor macro used for error indication. It expands to a static (until C++11) thread-local (since C++11) modifiable lvalue of type int. Several standard library functions indicate errors by writing positive integers to errno .
errno The value in errno is significant only when the return value of the call indicated an error (i.e., -1 from most system calls; -1 or NULL from most library functions); a function that succeeds is allowed to change errno. The value of errno is never set to zero by any system call or library function.
Many system calls will report the EINTR
error code if a signal occurred while the system call was in progress. No error actually occurred, it's just reported that way because the system isn't able to resume the system call automatically. This coding pattern simply retries the system call when this happens, to ignore the interrupt.
For instance, this might happen if the program makes use of alarm()
to run some code asynchronously when a timer runs out. If the timeout occurs while the program is calling write()
, we just want to retry the system call (aka read/write, etc).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With