zoukankan      html  css  js  c++  java
  • LDD3源码分析之poll分析

    编译环境:Ubuntu 10.10

    内核版本:2.6.32-38-generic-pae

    LDD3源码路径:examples/scull/pipe.c  examples/scull/main.c

    本文分析LDD3第6章的poll(轮询)操作。要理解驱动程序中poll函数的作用和实现,必须先理解用户空间中poll和select函数的用法。本文与前面的文章介绍的顺序有所不同,首先分析测试程序,以此理解用户空间中的poll和select函数的用法。然后再分析驱动程序怎样对用户空间的poll和select函数提供支持。

    一、poll函数的使用

    用户态的poll函数用以监测一组文件描述符是否可以执行指定的I/O操作,如果被监测的文件描述符都不能执行指定的I/O操作,则poll函数会阻塞,直到有文件描述符的状态发生变化,可以执行指定的I/O操作才解除阻塞。poll函数还可以指定一个最长阻塞时间,如果时间超时,将直接返回。poll函数的函数原型如下:

    int poll(struct pollfd *fds, nfds_t nfds, int timeout);

    要监测的文件描述符由第一个参数fds指定,它是一个struct pollfd数组,pollfd结构体定义如下:

    struct pollfd {
      int fd; /* file descriptor */
      short events; /* requested events */
      short revents; /* returned events */
    };

    pollfd结构体的第一个成员fd是文件描述符,代表一个打开的文件。第二个成员events是一个输入参数,用于指定poll监测哪些事件(如可读、可写等等)。第三个成员revents是一个输出参数,由内核填充,指示对于文件描述符fd,发生了哪些事件(如可读、可写等等)。

    poll函数的第二个参数nfds代表监测的文件描述符的个数,即fds数组的成员个数。

    poll函数的第三个参数timeout代表阻塞时间(以毫秒为单位),如果poll要求监测的事件没有发生,则poll会阻塞最多timeout毫秒。如果timeout设置为负数,则poll会一直阻塞,直到监测的事件发生。

    poll函数如果返回一个正数,代表内核返回了状态(保存在pollfd.revents中)的文件描述符的个数。如果poll返回0,表明是因为超时而返回的。如果poll返回-1,表明poll调用出错。

    poll函数可以监测哪些状态(由pollfd.events指定),以及内核可以返回哪些状态(保存在pollfd.revents中),由下面的宏设定:

    POLLIN:There is data to read.
    
    POLLOUT:Writing now will not block.
    
    POLLPRI:There is urgent data to read (e.g., out-of-band data on TCP socket; pseudo-terminal master in packet mode has seen state change in slave).
    
    POLLRDHUP: (since Linux 2.6.17) Stream socket peer closed connection, or shut down writing half of connection.  The _GNU_SOURCE feature test macro must be defined in order  to  obtain this definition.
    
    POLLERR:Error condition (output only).
    
    POLLHUP:Hang up (output only).
    
    POLLNVAL:Invalid request: fd not open (output only).
    
    When compiling with _XOPEN_SOURCE defined, one also has the following, which convey no further information beyond the bits listed above:
    
    POLLRDNORM:Equivalent to POLLIN.
    
    POLLRDBAND:Priority band data can be read (generally unused on Linux).
    
    POLLWRNORM:Equivalent to POLLOUT.
    
    POLLWRBAND:Priority data may be written.

    下面我们看一个测试scullpipe设备的poll操作(内核态poll)的测试程序,该程序使用了我们前面介绍的poll函数(用户态poll)。其代码如下:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <linux/poll.h>
    #include <sys/time.h>
    #include <sys/types.h>
    #include <sys/stat.h>
     
    int main(int argc, char *argv[])
    {
        int fd0, fd1, fd2, fd3;
        struct pollfd poll_fd[4];
        char buf[100];
        int retval;
     
        if(argc != 2 || ((strcmp(argv[1], "read") != 0) && (strcmp(argv[1], "write") != 0)))
        {
            printf("usage: ./poll_test read|write
    ");
            return -1;
        }
     
        fd0 = open("/dev/scullpipe0", O_RDWR);
        if ( fd0 < 0)
        {
            printf("open scullpipe0 error
    ");
            return -1;
        }
        
        fd1 = open("/dev/scullpipe1", O_RDWR);
        if ( fd1 < 0)
        {
            printf("open scullpipe1 error
    ");
            return -1;
        }
        
        fd2 = open("/dev/scullpipe2", O_RDWR);
        if ( fd2 < 0)
        {
            printf("open scullpipe2 error
    ");
            return -1;
        }
        
        fd3 = open("/dev/scullpipe3", O_RDWR);
        if ( fd3 < 0)
        {
            printf("open scullpipe3 error
    ");
            return -1;
        }
     
        if(strcmp(argv[1], "read") == 0)
        {
            poll_fd[0].fd = fd0;
            poll_fd[1].fd = fd1;
            poll_fd[2].fd = fd2;
            poll_fd[3].fd = fd3;
     
            poll_fd[0].events = POLLIN | POLLRDNORM;
            poll_fd[1].events = POLLIN | POLLRDNORM;
            poll_fd[2].events = POLLIN | POLLRDNORM;
            poll_fd[3].events = POLLIN | POLLRDNORM;
     
            retval = poll(poll_fd, 4, 10000);
        }
        else
        {
            poll_fd[0].fd = fd0;
            poll_fd[1].fd = fd1;
            poll_fd[2].fd = fd2;
            poll_fd[3].fd = fd3;
     
            poll_fd[0].events = POLLOUT | POLLWRNORM;
            poll_fd[1].events = POLLOUT | POLLWRNORM;
            poll_fd[2].events = POLLOUT | POLLWRNORM;
            poll_fd[3].events = POLLOUT | POLLWRNORM;
            
            retval = poll(poll_fd, 4, 10000);
        }
     
        if (retval == -1)
        {
            printf("poll error!
    ");
            return -1;
        }
        else if (retval)
        {
            if(strcmp(argv[1], "read") == 0)
            {
                if(poll_fd[0].revents & (POLLIN | POLLRDNORM))
                {
                    printf("/dev/scullpipe0 is readable!
    ");
                    memset(buf, 0, 100);
                    read(fd0, buf, 100);
                    printf("%s
    ", buf);
                }
     
                if(poll_fd[1].revents & (POLLIN | POLLRDNORM))
                {
                    printf("/dev/scullpipe1 is readable!
    ");
                    memset(buf, 0, 100);
                    read(fd1, buf, 100);
                    printf("%s
    ", buf);
                }
     
                if(poll_fd[2].revents & (POLLIN | POLLRDNORM))
                {
                    printf("/dev/scullpipe2 is readable!
    ");
                    memset(buf, 0, 100);
                    read(fd2, buf, 100);
                    printf("%s
    ", buf);
                }
     
                if(poll_fd[3].revents & (POLLIN | POLLRDNORM))
                {
                    printf("/dev/scullpipe3 is readable!
    ");
                    memset(buf, 0, 100);
                    read(fd3, buf, 100);
                    printf("%s
    ", buf);
                }
            }
            else
            {
                if(poll_fd[0].revents & (POLLOUT | POLLWRNORM))
                {
                    printf("/dev/scullpipe0 is writable!
    ");
                }
     
                if(poll_fd[1].revents & (POLLOUT | POLLWRNORM))
                {
                    printf("/dev/scullpipe1 is writable!
    ");
                }
     
                if(poll_fd[2].revents & (POLLOUT | POLLWRNORM))
                {
                    printf("/dev/scullpipe2 is writable!
    ");
                }
     
                if(poll_fd[3].revents & (POLLOUT | POLLWRNORM))
                {
                    printf("/dev/scullpipe3 is writable!
    ");
                }
            }
        }
        else
        {
            if(strcmp(argv[1], "read") == 0)
            {
                printf("No data within ten seconds.
    ");
            }
            else
            {
                printf("Can not write within ten seconds.
    ");
            }
        }
     
        return 0;
    }

    测试过程如下图所示:

     

     从上图可以看出,scullpipe0 - scullpipe3都是可写的。但是因为没有向其中写入任何内容,所以读的时候会阻塞住,因为测试程序设置最长阻塞时间为10秒,所以10秒后解除阻塞退出,并打印”No data within ten seconds.”。

    下面再次测试读操作,因为设备中没有内容,还是阻塞住,但是在10秒钟内,从另外一个终端向/dev/scullpipe2写入数据,这里是写入字符串”hello”,可以看到,写入数据后,测试程序解除阻塞,并打印”/dev/scullpipe2 is readable!”和”hello”字符串,这个过程如下图所示:

       

    二、select函数的使用

    select函数的作用与poll函数类似,也是监测一组文件描述符是否准备好执行指定的I/O操作。如果没有任何一个文件描述符可以完成指定的操作,select函数会阻塞住。select函数可以指定一个最长阻塞时间,如果超时,则直接返回。

    select函数的函数原型如下:

    int select(int nfds, fd_set *readfds, fd_set *writefds,
    fd_set *exceptfds, struct timeval *timeout);

    select通过三个独立的文件描述符集(fd_set)readfds,writefds,exceptfds指示监测哪些文件描述符,其中readfds中的文件描述符监测是否可进行非阻塞的读操作。writefds数组中的文件描述符监测是否可进行非阻塞的写操作。exceptfds数组中的文件描述符监测是否有异常(exceptions)。

    select的第一个参数nfds是三个文件描述符集readfds、writefds、exceptfds中最大文件描述符值加1。

    select的最后一个参数timeout代表最长阻塞时间。

    select函数返回满足指定I/O要求的文件描述符的个数,如果是超时退出的,select函数返回0。如果出错,select函数返回-1。

    有四个宏用来操作文件描述符集:

    void FD_ZERO(fd_set *set);  清空一个文件描述符集。
    
    void FD_SET(int fd, fd_set *set); 将一个文件描述符fd加入到指定的文件描述符集set中。
    
    void FD_CLR(int fd, fd_set *set); 将一个文件描述符fd从指定的文件描述符集set中删除。
    
    int  FD_ISSET(int fd, fd_set *set); 测试文件描述符fd是否在指定的文件描述符集set中。

    下面我们看使用select函数实现的测试驱动中poll操作的测试程序,其代码如下:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <sys/time.h>
    #include <sys/types.h>
    #include <sys/stat.h>
     
    int main(int argc, char *argv[])
    {
        fd_set rfds, wfds;
        int fd0, fd1, fd2, fd3;
        char buf[100];
        int retval;    
     
        /* Wait up to ten seconds. */
        struct timeval tv;
        tv.tv_sec = 10;
        tv.tv_usec = 0;
     
        if(argc != 2 || ((strcmp(argv[1], "read") != 0) && (strcmp(argv[1], "write") != 0)))
        {
            printf("usage: ./select_test read|write
    ");
            return -1;
        }
     
        fd0 = open("/dev/scullpipe0", O_RDWR);
        if ( fd0 < 0)
        {
            printf("open scullpipe0 error
    ");
            return -1;
        }
        
        fd1 = open("/dev/scullpipe1", O_RDWR);
        if ( fd1 < 0)
        {
            printf("open scullpipe1 error
    ");
            return -1;
        }
        
        fd2 = open("/dev/scullpipe2", O_RDWR);
        if ( fd2 < 0)
        {
            printf("open scullpipe2 error
    ");
            return -1;
        }
        
        fd3 = open("/dev/scullpipe3", O_RDWR);
        if ( fd3 < 0)
        {
            printf("open scullpipe3 error
    ");
            return -1;
        }
     
        if(strcmp(argv[1], "read") == 0)
        {
            FD_ZERO(&rfds);
            FD_SET(fd0, &rfds);
            FD_SET(fd1, &rfds);
            FD_SET(fd2, &rfds);
            FD_SET(fd3, &rfds);
            retval = select(fd3 + 1, &rfds, NULL, NULL, &tv);
        }
        else
        {
            FD_ZERO(&wfds);
            FD_SET(fd0, &wfds);
            FD_SET(fd1, &wfds);
            FD_SET(fd2, &wfds);
            FD_SET(fd3, &wfds);
            retval = select(fd3 + 1, NULL, &wfds, NULL, &tv);
        }
     
        if (retval == -1)
        {
            printf("select error!
    ");
            return -1;
        }
        else if (retval)
        {
            if(strcmp(argv[1], "read") == 0)
            {
                if(FD_ISSET(fd0, &rfds))
                {
                    printf("/dev/scullpipe0 is readable!
    ");
                    memset(buf, 0, 100);
                    read(fd0, buf, 100);
                    printf("%s
    ", buf);
                }
     
                if(FD_ISSET(fd1, &rfds))
                {
                    printf("/dev/scullpipe1 is readable!
    ");
                    memset(buf, 0, 100);
                    read(fd1, buf, 100);
                    printf("%s
    ", buf);
                }
     
                if(FD_ISSET(fd2, &rfds))
                {
                    printf("/dev/scullpipe2 is readable!
    ");
                    memset(buf, 0, 100);
                    read(fd2, buf, 100);
                    printf("%s
    ", buf);
                }
     
                if(FD_ISSET(fd3, &rfds))
                {
                    printf("/dev/scullpipe3 is readable!
    ");
                    memset(buf, 0, 100);
                    read(fd3, buf, 100);
                    printf("%s
    ", buf);
                }
            }
            else
            {
                if(FD_ISSET(fd0, &wfds))
                {
                    printf("/dev/scullpipe0 is writable!
    ");
                }
     
                if(FD_ISSET(fd1, &wfds))
                {
                    printf("/dev/scullpipe1 is writable!
    ");
                }
     
                if(FD_ISSET(fd2, &wfds))
                {
                    printf("/dev/scullpipe2 is writable!
    ");
                }
     
                if(FD_ISSET(fd3, &wfds))
                {
                    printf("/dev/scullpipe3 is writable!
    ");
                }
            }
        }
        else
        {
            if(strcmp(argv[1], "read") == 0)
            {
                printf("No data within ten seconds.
    ");
            }
            else
            {
                printf("Can not write within ten seconds.
    ");
            }
        }
     
        return 0;
    }

    测试过程如下图所示:

       

     从上图可以看出,scullpipe0 - scullpipe3都是可写的。但是因为没有向其中写入任何内容,所以读的时候会阻塞住,因为测试程序设置最长阻塞时间为10秒,所以10秒后解除阻塞退出,并打印”No data within ten seconds.”。

    下面再次测试读操作,因为设备中没有内容,还是阻塞住,但是在10秒钟内,从另外一个终端向/dev/scullpipe2写入数据,这里是写入字符串”hello”,可以看到,写入数据后,测试程序解除阻塞,并打印”/dev/scullpipe2 is readable!”和”hello”字符串,这个过程如下图所示:

       

    三、驱动程序中poll操作的实现

    用户空间的poll和select函数,最终都会调用驱动程序中的poll函数,其函数原型如下:

    unsigned int (*poll) (struct file *filp, poll_table *wait);

    poll函数应该实现两个功能:

    一是把能标志轮询状态变化的等待队列加入到poll_table中,这通过调用poll_wait函数实现。

    二是返回指示能进行的I/O操作的标志位。

    poll函数的第二个参数poll_table,是内核中用来实现poll,select系统调用的结构体,对于驱动开发者来说,不必关心其具体内容,可以把poll_table看成是不透明的结构体,只要拿过来使用就可以了。驱动程序通过poll_wait函数,把能够唤醒进程,改变轮询状态的等待队列加入到poll_table中。该函数定义如下:

    void poll_wait (struct file *, wait_queue_head_t *, poll_table *);

    对于poll函数的第二个功能,返回的标志位与用户空间相对应,最常用的标志位是POLLIN | POLLRDNORM和POLLOUT | POLLWRNORM,分别标志可进行非阻塞的读和写操作。

    下面看scullpipe设备对poll操作的实现,其内容其实非常简单:

    228static unsigned int scull_p_poll(struct file *filp, poll_table *wait)
    229{
    230    struct scull_pipe *dev = filp->private_data;
    231    unsigned int mask = 0;
    232
    233    /*
    234     * The buffer is circular; it is considered full
    235     * if "wp" is right behind "rp" and empty if the
    236     * two are equal.
    237     */
    238    down(&dev->sem);
    239    poll_wait(filp, &dev->inq,  wait);
    240    poll_wait(filp, &dev->outq, wait);
    241    if (dev->rp != dev->wp)
    242        mask |= POLLIN | POLLRDNORM;    /* readable */
    243    if (spacefree(dev))
    244        mask |= POLLOUT | POLLWRNORM;   /* writable */
    245    up(&dev->sem);
    246    return mask;
    247}

    第239行,调用poll_wait函数将读等待队列加入到poll_table中。

    第240行,调用poll_wait函数将写等待队列加入到poll_table中。

    241 - 242行,如果有内容可读,设置可读标志位。

    243 - 244行,如果有空间可写,设置可写标志位。

    246行,将标志位返回。

    驱动程序中的poll函数很简单,那么内核是怎么实现poll和select系统调用的呢?当用户空间程序调用poll和select函数时,内核会调用由用户程序指定的全部文件的poll方法,并向它们传递同一个poll_table结构。poll_table结构其实是一个生成”实际数据结构”的函数(名为poll_queue_proc)的封装,这个函数poll_queue_proc,不同的应用场景,内核有不同的实现,这里我们不仔细研究对应的函数。对于poll和select系统调用来说,这个”实际数据结构”是一个包含poll_table_entry结构的内存页链表。这里提到的相关数据结构在linux-2.6.32-38源码中,定义在include/linux/poll.h文件中,代码如下所示:

     30/*
     31 * structures and helpers for f_op->poll implementations
     32 */
     33typedef void (*poll_queue_proc)(struct file *, wait_queue_head_t *, struct poll_table_struct *);
     34
     35typedef struct poll_table_struct {
     36    poll_queue_proc qproc;
     37    unsigned long key;
     38} poll_table;
     39
     40static inline void poll_wait(struct file * filp, wait_queue_head_t * wait_address, poll_table *p)
     41{
     42    if (p && wait_address)
     43        p->qproc(filp, wait_address, p);
     44}
     45
     46static inline void init_poll_funcptr(poll_table *pt, poll_queue_proc qproc)
     47{
     48    pt->qproc = qproc;
     49    pt->key   = ~0UL; /* all events enabled */
     50}
     51
     52struct poll_table_entry {
     53    struct file *filp;
     54    unsigned long key;
     55    wait_queue_t wait;
     56    wait_queue_head_t *wait_address;
     57};

    poll操作我们就分析完了,内核要求驱动程序做的事并不多,但是我们在学习时,要把poll操作和前面介绍的阻塞型read/write函数以及scullpipe设备的等待队列等结合起来考虑,因为scullpipe设备是一个完整的程序模块。

  • 相关阅读:
    ajax提交转码解码
    关于idea开发工具常用的快捷键
    oracle 查询某个时间段数据
    hibernate : object references an unsaved transient instance 问题
    log4j日志
    JS关键字 import
    代码正常,junit却报错原因及解决方法
    hdu 5868 Polya计数
    hdu 5893 (树链剖分+合并)
    hdu 5895 广义Fibonacci数列
  • 原文地址:https://www.cnblogs.com/jiangzhaowei/p/11728439.html
Copyright © 2011-2022 走看看