2012-02-15 5 views
3

я рассматриваю пример модуля ядра при этом pageНевозможно понять работу в read_proc в модуле ядра Linux

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

Может кто-то объяснить, почему ОТКЛ проверено, чтобы оно было больше 0. Более того, кто-то может объяснить важность аргументов off и count.

До сих пор я понимаю, что мы должны писать данные на странице и должны установить eof, когда данные закончились.

Спасибо.

+0

Значение 'off' (смещение в файле) больше нуля означает, что вызов функции' fourtune_read' не является первым. В этом случае пользователю нечего писать, поскольку в первый раз были данные. –

ответ

3

off - это позиция в файле, откуда должны считываться данные. Это похоже на набор нормального файла. Но в случае proc_read это нечто другое. Например, если вы вызываете вызов чтения в файле proc для чтения 100 байтов данных, выключение и подсчет в proc_read будет выглядеть так:

в первый раз, off = 0, count 100. Скажем, например, в ваш proc_read вы вернули только 10 байт. Затем элемент управления не может вернуться к пользовательскому приложению, ваш proc_read будет вызываться ядром еще раз с выключенным значением 10 и считаться как 90. Снова, если вы вернете 20 в proc_read, вы снова вызовете с выключенным 30, счет 70. Подобно этому вы будете вызваны до того, как счет достигнет 0. Затем данные записываются в данный буфер пользователя, и ваш запрос read() возвращается.

Но если у вас нет сотни байтов данных и вы хотите вернуть только несколько байтов, вы должны установить eof в 1. Затем функция read() немедленно возвращается.

Для начала следующий комментарий объясняет меня лучше, чем я.

 /* 
     * 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 [email protected]) 
        * 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. 
        */