7

On Linux, the C standard library calls fread and fwrite execute the system call equivalents, read and write.

The man pages for the system calls mention interrupts, saying that a short write may occur with errno set to EINTR. However, the man pages for the library functions say nothing about interrupts. So, can interrupts occur during these library functions?

https://linux.die.net/man/3/fwrite

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
magnus
  • 4,031
  • 7
  • 26
  • 48
  • [man 7 linux signal](http://man7.org/linux/man-pages/man7/signal.7.html) is a good place for understanding signal interruption during syscall. – JiaHao Xu Nov 11 '18 at 10:33

1 Answers1

7

Interrupts can occur during the fread() and fwrite() functions (and during the read() and write() system calls — there's no way to stop that. However, it is not so clear what happens if an interrupt occurs — whether the signal is delivered or not. It depends on how your application (thread?) is set up to handle interrupts.

  • If it ignores them, then there'll be no effect on fread() or fwrite().
  • If it has default handling, the program will stop; the functions will not return.
  • If your signal handler exits or use siglongjmp() (or longjmp()), then the system call won't return.
  • If your handler returns, it will depend on what you specified to sigaction() when you set up the handler.
    • SA_RESTART means that the underlying read or write will be retried
    • No SA_RESTART will mean that the read or write will terminate — possibly with a short read or write, or possibly with an error and errno set to EINTR.
    • If the system call indicates failure, it is probable that fread() and fwrite() will report failure too if no data was read or written before the interrupt occurred.
    • If some data was read or written, you'll probably get the short read or write response.
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278