Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the conditions under which a short read/write can occur?

Tags:

c

posix

signals

The read and write functions (and relatives like send, recv, readv, ...) can return a number of bytes less than the requested read/write length if interrupted by a signal (under certain circumstances), and perhaps in other cases too. Is there a well-defined set of conditions for when this can happen, or is it largely up to the implementation? Here are some particular questions I'm interested in the answers to:

  • If a signal handler is non-interrupting (SA_RESTART) that will cause IO operations interrupted before any data is transferred to be restarted after the signal handler returns. But if a partial read/write has already occurred and the signal handler is non-interrupting, will the syscall return immediately with the partial length, or will it be resumed attempting to read/write the remainder?
  • Obviously read functions can return short reads on network, pipe, and terminal file descriptors when less data than the requested amount is available. But can write functions return short writes in these cases due to limited buffer size, or will they block until all the data can be written?

I'd be interested in all three of standards-required, common, and Linux-specific behavior.

like image 351
R.. GitHub STOP HELPING ICE Avatar asked Mar 26 '11 13:03

R.. GitHub STOP HELPING ICE


1 Answers

For your second question : write can return short writes for a limited buffer size if it is non-blocking

like image 67
Heisenbug Avatar answered Sep 17 '22 18:09

Heisenbug