c - 如何读到/proc 文件的末尾

标签 c linux-kernel proc

我有/proc 文件。我不明白 read 函数的一些论点。 阅读功能看起来像这样:

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

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

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

if (offset > 0)
return 0;

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

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

最佳答案

参见 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                         */

关于c - 如何读到/proc 文件的末尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10155329/

相关文章:

linux - 两个 open/proc 条目的处理方式不同

c - 不调用curl_multi_cleanup()是否安全?

c - 获取由于在 c 中执行 cmd 命令而打印的文本

c - ld 返回 1 退出状态,斐波那契搜索

在 Linux 中使用 makefile 编译内核模块

c - 如何编辑proc文件?

c - 我不知道如何通过进程名称获取进程的 pid

c - 通过用户空间写入/proc条目

linux - 进程控制 block ,Linux 中的进程描述符和 task_struct?

c++ - 与 CLOB 兼容的 Pro-C 缓冲区类型