无法理解read_proc的工作在Linux内核模块 [英] Unable to understand working of read_proc in Linux kernel module

查看:347
本文介绍了无法理解read_proc的工作在Linux内核模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我检讨这个

在程序中使用的read_proc如下:

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;
}

有人能解释为什么关闭进行检查,大于0。而且有人可以解释什么是关的重要性和计数参数。

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.

到目前为止,我的理解是,我们必须写在页面数据并设置EOF当数据已经结束。

My understanding so far is that we have to write data in page and have to set eof when data has ended.

感谢。

推荐答案

关闭时从那里数据在文件中的位置被读出。这就像断设定正常的文件。但是,在proc_read的情况下,它是一些什么不同。例如,如果你调用proc文件的读取调用读取100个字节的数据,关闭并在proc_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:

在第一时间,关= 0,在你proc_read数100比方说你只返回10个字节。那么控制不能回来给用户的应用程序,你proc_read将由内核再次与关闭称为10和计数为90。同样地,如果您在proc_read返回20,你将再次调用了30,算70。这样你会叫,直到计数达到0,然后将数据写入到给定用户缓冲区和应用程序阅读()调用返回。

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.

但是,如果你没有几百个字节的数据,并希望返回只有几个字节,必须设置EOF为1然后read()函数立即返回。

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.

      /*
       * How to be a proc read function
       * ------------------------------
                     * Prototype:
                     *    int f(char *buffer, char **start, off_t offset,
                     *          int count, int *peof, void *dat)
                     *
                     * Assume that the buffer is "count" bytes in size.
                     *
                     * If you know you have supplied all the data you
                     * have, set *peof.
                     *
                     * You have three ways to return data:
                     * 0) Leave *start = NULL.  (This is the default.)
                     *    Put the data of the requested offset at that
                     *    offset within the buffer.  Return the number (n)
                     *    of bytes there are from the beginning of the
                     *    buffer up to the last byte of data.  If the
                     *    number of supplied bytes (= n - offset) is 
                     *    greater than zero and you didn't signal eof
                     *    and the reader is prepared to take more data
                     *    you will be called again with the requested
                     *    offset advanced by the number of bytes 
                     *    absorbed.  This interface is useful for files
                     *    no larger than the buffer.
                     * 1) Set *start = an unsigned long value less than
                     *    the buffer address but greater than zero.
                     *    Put the data of the requested offset at the
                     *    beginning of the buffer.  Return the number of
                     *    bytes of data placed there.  If this number is
                     *    greater than zero and you didn't signal eof
                     *    and the reader is prepared to take more data
                     *    you will be called again with the requested
                     *    offset advanced by *start.  This interface is
                     *    useful when you have a large file consisting
                     *    of a series of blocks which you want to count
                     *    and return as wholes.
                     *    (Hack by Paul.Russell@rustcorp.com.au)
                     * 2) Set *start = an address within the buffer.
                     *    Put the data of the requested offset at *start.
                     *    Return the number of bytes of data placed there.
                     *    If this number is greater than zero and you
                     *    didn't signal eof and the reader is prepared to
                     *    take more data you will be called again with the
                     *    requested offset advanced by the number of bytes
                     *    absorbed.
                     */

这篇关于无法理解read_proc的工作在Linux内核模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆