Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ standard I/O and signals

Tags:

c++

posix

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.

like image 870
Mike Sweeney Avatar asked Jun 11 '19 05:06

Mike Sweeney


People also ask

What is standard IO in C?

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.

What is IO signal?

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.

What is an IO stream in C?

iostream is a C++ library for input-output. The C equivalent would be stdio.h.

What is digital input and output signal?

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.


2 Answers

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.

And that should not be an issue for your code because:

  1. read can fail, hence the return value and errno must be handled anyway.
  2. 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.
like image 105
Maxim Egorushkin Avatar answered Oct 07 '22 22:10

Maxim Egorushkin


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.

like image 43
Davis Herring Avatar answered Oct 07 '22 22:10

Davis Herring