无法理解 Linux 内核模块中 read_proc 的工作
上的内核模块示例
我正在查看此 页面 程序中使用的 read_proc 如下:
int fortune_read( char *page, char **start, off_t off,
int count, int *eof, void *data )
{
int len;
if (off > 0) {
*eof = 1;
return 0;
}
/* Wrap-around */
if (next_fortune >= cookie_index) next_fortune = 0;
len = sprintf(page, "%s\n", &cookie_pot[next_fortune]);
next_fortune += len;
return len;
}
有人可以解释一下为什么 off 被检查为大于 0。此外有人可以解释一下 off 和 count 参数的重要性吗?
到目前为止我的理解是我们必须在页面中写入数据并且必须在数据结束时设置eof。
谢谢。
I am reviewing the kernel module example at this page
The read_proc used in the program is as follows:
int fortune_read( char *page, char **start, off_t off,
int count, int *eof, void *data )
{
int len;
if (off > 0) {
*eof = 1;
return 0;
}
/* Wrap-around */
if (next_fortune >= cookie_index) next_fortune = 0;
len = sprintf(page, "%s\n", &cookie_pot[next_fortune]);
next_fortune += len;
return len;
}
Can someone explain why off is checked to be greater than 0. Moreover can someone explain what is the importance of the off and count arguments.
My understanding so far is that we have to write data in page and have to set eof when data has ended.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
off 是文件中必须读取数据的位置。这就像普通文件的偏移量。但是,对于 proc_read 来说,情况有些不同。例如,如果您对 proc 文件调用 read 调用来读取 100 个字节的数据,proc_read 中的 off 和 count 将如下所示:
第一次,off = 0,count 100。例如,在 proc_read 中您有仅返回 10 个字节。然后控制权将无法返回到用户应用程序,您的 proc_read 将再次被内核调用,off 为 10,计数为 90。同样,如果您在 proc_read 中返回 20,您将再次以 off 30,计数 70 调用。像这样,您将被调用,直到计数达到 0。然后数据被写入给定的用户缓冲区,并且您的应用程序 read() 调用返回。
但如果你没有一百个字节的数据并且只想返回几个字节,则必须将eof设置为1。然后read()函数立即返回。
首先,以下评论比我解释得更好。
off is the position in the file from where data has to be read from. This is like off set of normal file. But, in the case of proc_read it is some what different. For example if you invoke a read call on the proc file to read 100 bytes of data, off and count in the proc_read will be like this:
in the first time, off = 0, count 100. Say for example in your proc_read you have returned only 10 bytes. Then the control cannot come back to the user application, your proc_read will be called by the kernel once again with off as 10 and count as 90. Again if you return 20 in the proc_read, you will again called with off 30, count 70. Like this you will be called till count reaches 0. Then the data is written into the given user buffer and your application read() call returns.
But if you don't have hundred bytes of data and want to return only a few bytes, you must set the eof to 1. Then the read() function returns immediately.
For the start, the following comment explains better than me.