read, fread partial reads












0















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










share|improve this question

























  • Depends on whether the "stream" is a file, or something else.

    – user3386109
    Nov 23 '18 at 20:55











  • You are somewhat mixing apples and oranges. read will read the number of bytes you tell it to. When you are reading from a file, that's not a problem, the bytes are there (unless EOF is encountered first). So in that case, read may return less than requested. However, when you are reading from a network connection, there is no guarantee that any bytes will be there until they are sent by the remote host. So in that case, it is very common from read to return less than requested until the bytes become available.

    – David C. Rankin
    Nov 23 '18 at 20:56











  • @user3386109 In my case it is a char device I created in a kernel module. It supports partial reads (I maintain an offset and write the appropriate data to the userspace buffer) but I don't know on user side if I need to call read on the device in a loop or it is handled inside

    – Eloo
    Nov 23 '18 at 21:11
















0















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










share|improve this question

























  • Depends on whether the "stream" is a file, or something else.

    – user3386109
    Nov 23 '18 at 20:55











  • You are somewhat mixing apples and oranges. read will read the number of bytes you tell it to. When you are reading from a file, that's not a problem, the bytes are there (unless EOF is encountered first). So in that case, read may return less than requested. However, when you are reading from a network connection, there is no guarantee that any bytes will be there until they are sent by the remote host. So in that case, it is very common from read to return less than requested until the bytes become available.

    – David C. Rankin
    Nov 23 '18 at 20:56











  • @user3386109 In my case it is a char device I created in a kernel module. It supports partial reads (I maintain an offset and write the appropriate data to the userspace buffer) but I don't know on user side if I need to call read on the device in a loop or it is handled inside

    – Eloo
    Nov 23 '18 at 21:11














0












0








0








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










share|improve this question
















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







c linux system-calls fread






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 21:12







Eloo

















asked Nov 23 '18 at 20:52









ElooEloo

387




387













  • Depends on whether the "stream" is a file, or something else.

    – user3386109
    Nov 23 '18 at 20:55











  • You are somewhat mixing apples and oranges. read will read the number of bytes you tell it to. When you are reading from a file, that's not a problem, the bytes are there (unless EOF is encountered first). So in that case, read may return less than requested. However, when you are reading from a network connection, there is no guarantee that any bytes will be there until they are sent by the remote host. So in that case, it is very common from read to return less than requested until the bytes become available.

    – David C. Rankin
    Nov 23 '18 at 20:56











  • @user3386109 In my case it is a char device I created in a kernel module. It supports partial reads (I maintain an offset and write the appropriate data to the userspace buffer) but I don't know on user side if I need to call read on the device in a loop or it is handled inside

    – Eloo
    Nov 23 '18 at 21:11



















  • Depends on whether the "stream" is a file, or something else.

    – user3386109
    Nov 23 '18 at 20:55











  • You are somewhat mixing apples and oranges. read will read the number of bytes you tell it to. When you are reading from a file, that's not a problem, the bytes are there (unless EOF is encountered first). So in that case, read may return less than requested. However, when you are reading from a network connection, there is no guarantee that any bytes will be there until they are sent by the remote host. So in that case, it is very common from read to return less than requested until the bytes become available.

    – David C. Rankin
    Nov 23 '18 at 20:56











  • @user3386109 In my case it is a char device I created in a kernel module. It supports partial reads (I maintain an offset and write the appropriate data to the userspace buffer) but I don't know on user side if I need to call read on the device in a loop or it is handled inside

    – Eloo
    Nov 23 '18 at 21:11

















Depends on whether the "stream" is a file, or something else.

– user3386109
Nov 23 '18 at 20:55





Depends on whether the "stream" is a file, or something else.

– user3386109
Nov 23 '18 at 20:55













You are somewhat mixing apples and oranges. read will read the number of bytes you tell it to. When you are reading from a file, that's not a problem, the bytes are there (unless EOF is encountered first). So in that case, read may return less than requested. However, when you are reading from a network connection, there is no guarantee that any bytes will be there until they are sent by the remote host. So in that case, it is very common from read to return less than requested until the bytes become available.

– David C. Rankin
Nov 23 '18 at 20:56





You are somewhat mixing apples and oranges. read will read the number of bytes you tell it to. When you are reading from a file, that's not a problem, the bytes are there (unless EOF is encountered first). So in that case, read may return less than requested. However, when you are reading from a network connection, there is no guarantee that any bytes will be there until they are sent by the remote host. So in that case, it is very common from read to return less than requested until the bytes become available.

– David C. Rankin
Nov 23 '18 at 20:56













@user3386109 In my case it is a char device I created in a kernel module. It supports partial reads (I maintain an offset and write the appropriate data to the userspace buffer) but I don't know on user side if I need to call read on the device in a loop or it is handled inside

– Eloo
Nov 23 '18 at 21:11





@user3386109 In my case it is a char device I created in a kernel module. It supports partial reads (I maintain an offset and write the appropriate data to the userspace buffer) but I don't know on user side if I need to call read on the device in a loop or it is handled inside

– Eloo
Nov 23 '18 at 21:11












1 Answer
1






active

oldest

votes


















2














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.






share|improve this answer
























  • In particular, like all stdio read functions, fread is specified to behave as if by repeated calls to fgetc until certain conditions are met.

    – R..
    Nov 24 '18 at 5:21











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452824%2fread-fread-partial-reads%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














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.






share|improve this answer
























  • In particular, like all stdio read functions, fread is specified to behave as if by repeated calls to fgetc until certain conditions are met.

    – R..
    Nov 24 '18 at 5:21
















2














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.






share|improve this answer
























  • In particular, like all stdio read functions, fread is specified to behave as if by repeated calls to fgetc until certain conditions are met.

    – R..
    Nov 24 '18 at 5:21














2












2








2







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 21:31









GillesGilles

74.6k18159203




74.6k18159203













  • In particular, like all stdio read functions, fread is specified to behave as if by repeated calls to fgetc until certain conditions are met.

    – R..
    Nov 24 '18 at 5:21



















  • In particular, like all stdio read functions, fread is specified to behave as if by repeated calls to fgetc until certain conditions are met.

    – R..
    Nov 24 '18 at 5:21

















In particular, like all stdio read functions, fread is specified to behave as if by repeated calls to fgetc until certain conditions are met.

– R..
Nov 24 '18 at 5:21





In particular, like all stdio read functions, fread is specified to behave as if by repeated calls to fgetc until certain conditions are met.

– R..
Nov 24 '18 at 5:21


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452824%2fread-fread-partial-reads%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Berounka

Sphinx de Gizeh

Different font size/position of beamer's navigation symbols template's content depending on regular/plain...