If I call a POSIX function like read() that isn't part of the C++ standard library I have to worry about it being interrupted by a signal and handle an EINTR return value. If I call a C++ standard library function like fread() based on this documentation there is no mention of EINTR so it seems like the standard library hides this from the user. Is my understanding correct and does this hold for all C++ standard library functions?
Update: So what is still not clear from the responses is that is the conclusion that one can't write standard C++ that properly functions on every platform? I'm seeing people mention POSIX behavior that is not part of the standard so this is confusing.
When we say Input, it means to feed some data into a program. An input can be given in the form of a file or from the command line. C programming provides a set of built-in functions to read the given input and feed it to the program as per requirement.
In computing, input/output (I/O, or informally io or IO) is the communication between an information processing system, such as a computer, and the outside world, possibly a human or another information processing system.
iostream is a C++ library for input-output. The C equivalent would be stdio.h.
A digital signal is a signal that exists in one of two states: high or low, open or closed, on or off. A digital input/output (DIO) device is hardware that sends or receives these digital signals. DIO devices are usually built around simple relays such as reed relays.
If I call a POSIX function like
read()that isn't part of the C++ standard library I have to worry about it being interrupted by a signal and handle anEINTRreturn value.
And that should not be an issue for your code because:
read can fail, hence the return value and errno must be handled anyway.read can do a partial read, so that in blocking mode you have to call read in a loop. errno == EINTR is just another condition to retry the call in both blocking and non-blocking mode. POSIX covers fread as well, which is good since the C and C++ standards say very little about signals to support more than just POSIX. It says, partially on the fgetc page, that fread may return early (possibly returning 0), set ferror, and set errno to EINTR. Of course, if a signal causes a short read, there is no way for the C library to detect that and it will simply call read again, so you can’t rely on responding to the signal in a timely fashion. Writing is analogous.
There are very few other operations in the C++ standard library that can produce EINTR; unfortunately, POSIX doesn’t cover C++, so there isn’t an established answer for things like sleep_for that aren’t in C.
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