【问题标题】:how read to the end of /proc file如何读到 /proc 文件的末尾
【发布时间】:2025-12-08 04:30:01
【问题描述】:

我有 /proc 文件。我不明白读取功能的一些论点。 读取函数是这样的:

int procfile_read(char *buffer,  char **buffer_location,  off_t offset, int buffer_length, int *eof, void *data)

我不知道如何使用偏移参数。

一些例子使用这样的偏移参数:

if (offset > 0)
return 0;

他们解释说:这很重要,因为库中的标准读取函数将继续发出读取系统调用,直到内核回复它没有更多信息,或者直到它的缓冲区被填满。

我有一个大于缓冲区的文件。我怎样才能读到文件的末尾???

【问题讨论】:

    标签: c linux-kernel proc


    【解决方案1】:

    查看proc_file_read 函数实现,尤其是评论:

      74                        /*
      75                         * How to be a proc read function
      76                         * ------------------------------
      77                         * Prototype:
      78                         *    int f(char *buffer, char **start, off_t offset,
      79                         *          int count, int *peof, void *dat)
      80                         *
      81                         * Assume that the buffer is "count" bytes in size.
      82                         *
      83                         * If you know you have supplied all the data you
      84                         * have, set *peof.
      85                         *
      86                         * You have three ways to return data:
      87                         * 0) Leave *start = NULL.  (This is the default.)
      88                         *    Put the data of the requested offset at that
      89                         *    offset within the buffer.  Return the number (n)
      90                         *    of bytes there are from the beginning of the
      91                         *    buffer up to the last byte of data.  If the
      92                         *    number of supplied bytes (= n - offset) is 
      93                         *    greater than zero and you didn't signal eof
      94                         *    and the reader is prepared to take more data
      95                         *    you will be called again with the requested
      96                         *    offset advanced by the number of bytes 
      97                         *    absorbed.  This interface is useful for files
      98                         *    no larger than the buffer.
      99                         * 1) Set *start = an unsigned long value less than
     100                         *    the buffer address but greater than zero.
     101                         *    Put the data of the requested offset at the
     102                         *    beginning of the buffer.  Return the number of
     103                         *    bytes of data placed there.  If this number is
     104                         *    greater than zero and you didn't signal eof
     105                         *    and the reader is prepared to take more data
     106                         *    you will be called again with the requested
     107                         *    offset advanced by *start.  This interface is
     108                         *    useful when you have a large file consisting
     109                         *    of a series of blocks which you want to count
     110                         *    and return as wholes.
     111                         *    (Hack by Paul.Russell@rustcorp.com.au)
     112                         * 2) Set *start = an address within the buffer.
     113                         *    Put the data of the requested offset at *start.
     114                         *    Return the number of bytes of data placed there.
     115                         *    If this number is greater than zero and you
     116                         *    didn't signal eof and the reader is prepared to
     117                         *    take more data you will be called again with the
     118                         *    requested offset advanced by the number of bytes
     119                         *    absorbed.
     120                         */
    

    【讨论】: