get info in a loop from a sysfs entry

Dave Hylands dhylands at gmail.com
Tue Jan 4 10:52:02 EST 2011


Hi Wouter,

On Tue, Jan 4, 2011 at 2:48 AM, Wouter Simons <lkml at woutersimons.org> wrote:
> Hi all,
>
> This might be a silly question, but I want to make sure I understand
> things correctly.
>
> I have a driver with a sysfs entry to get the next data sample every
> time I read the file. Used like below it works wonderful:
>
> # cat next
> 0x15814
> # cat next
> 0x1682B
>
> The last 12 bits are the sample and the first (20) bits are the channel
> the sample is from (some ADC hardware board with 24 inputs).
>
> Now I have some C code that will loop periodically to collect the
> samples and do some magic with them and I was hoping I could simply keep
> a FILE * open with a loop like this:
>
> for (i = 0; i < count; i++) {
>        if (fscanf(fd_next, "0x%X", &sample) != 1) { /* No data */
>                continue;
>        }

I would change this to use unbuffered I/O routines (i.e.
open/read/lseek/close) and use sscanf rather than fscanf.
fopen/fread/fseek/fclose use buffering by default.

That would eliminate any buffering that the user side runtime library
is doing. I suspect that because the data is buffered by the FILE *
routines, even doing the seek is just re-returning the data that was
read the first time around.

Dave Hylands



More information about the Kernelnewbies mailing list