Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read, fread partial reads

I can't seem to find info about this in the documentation.

The read system call documentation says it may read less than specified. Does readattempt to read several times?

I know that fread is a wrapper for read. When I invoke fread, is it possible that it will read from the stream several times until it gets 0 or reads specified bytes, or will it only attempt to read once?

I am reading from a char device created in my kernel module, it transfers info from a data structure and supports partial reads. I am interested in reading all of the data until it returns 0.

thanks

like image 701
Eloo Avatar asked Oct 20 '25 15:10

Eloo


1 Answers

The general idea of read is that it returns as soon as some data is available¹. From an application's perspective, that's all you can assume.

If you're implementing the read callback in a kernel driver, it's up to you when read decides to return some data. But applications will² expect that read calls may be partial, and they should call read in a loop if they really need a certain number of bytes. Some applications want read not to block, so it would be a bad idea to block in a read call if some data is available.

The fread function blocks until it's read as many bytes as were requested, until it's reached the end of the file, or until an error occurs. It works by calling read in a loop.

¹ Whether and when read may return 0 bytes is beyond the scope of this answer.
² Or at least should. Buggy applications do exist.

like image 194
Gilles 'SO- stop being evil' Avatar answered Oct 23 '25 05:10

Gilles 'SO- stop being evil'



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!