epoll的内核实现

select/poll的缺点

A. 每次调用时重复的从用户态读入参数

B. 每次调用时全量的扫描文件描述符

C. 每次调用开始,将进程加入到每个文件描述符的等待队列,在调用结束后又把进程从等待队列中删除。

D. 在不修改内核的情况下,select最多支持1024个文件描述符。

文件系统中的一些重要结构

在linux中,进程通过file_struct结构与文件关联,而文件通过等待队列与进程关联,进而形成一种多对多的关系。

首先,文件对象struct file_struct

struct files_struct {  
    atomic_t        count;              //自动增量  
    struct fdtable  *fdt;  
    struct fdtable  fdtab;  
    fd_set      close_on_exec_init;     //执行exec时需要关闭的文件描述符集合  
    fd_set      open_fds_init;          //当前打开文件的文件描述符屏蔽字  
    struct file  *fd_array[NR_OPEN_DEFAULT];//文件对象数组  
    spinlock_t  file_lock;    
};

该结构在进程的task_struct中使用,用于保存进程当前打开的文件集合。其中struct file称为文件对象,保存文件的信息,这里我们仅列出我们可能会用到的成员

struct file{  
     ....  
     struct file_operations *f_op;  
     ...  
}

在struct file_operations对象中存储对文件对象可以进行各种操作的指针:

struct file_operations {  
  struct module *owner;  
  loff_t(*llseek) (struct file *, loff_t, int);  
  ssize_t(*read) (struct file *, char __user *, size_t, loff_t *);  
  ssize_t(*aio_read) (struct kiocb *, char __user *, size_t, loff_t);  
  ssize_t(*write) (struct file *, const char __user *, size_t, loff_t *);  
  ssize_t(*aio_write) (struct kiocb *, const char __user *, size_t, loff_t);  
  int (*readdir) (struct file *, void *, filldir_t);  
  unsigned int (*poll) (struct file *, struct poll_table_struct *);  
  int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);  
  int (*mmap) (struct file *, struct vm_area_struct *);  
  int (*open) (struct inode *, struct file *);  
  int (*flush) (struct file *);  
  int (*release) (struct inode *, struct file *);  
  int (*fsync) (struct file *, struct dentry *, int datasync);  
  int (*aio_fsync) (struct kiocb *, int datasync);  
  int (*fasync) (int, struct file *, int);  
  int (*lock) (struct file *, int, struct file_lock *);  
  ssize_t(*readv) (struct file *, const struct iovec *, unsigned long, loff_t *);  
  ssize_t(*writev) (struct file *, const struct iovec *, unsigned long, loff_t *);  
  ssize_t(*sendfile) (struct file *, loff_t *, size_t, read_actor_t, void __user *);  
  ssize_t(*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);  
  unsigned long (*get_unmapped_area) (struct file *, unsigned long,  
         unsigned long, unsigned long,  
         unsigned long);  
};

epoll模型

epoll自己保存传入的文件描述符,同时通过设备等待队列唤醒时调用“回调函数”实现事件的通知。

epoll模型将select/poll单个的操作拆分:

int epoll_create(int size);   
int epoll_ctl(int epfd, int op, int fd ,struct epoll_event *event);  
int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout);

epoll机制实现了自己特有的文件系统eventpoll filesystem。

epoll_create闯将一个属于该文件系统的文件,并返回其文件描述符。struct eventpoll 保存了epoll文件节点的扩展信息,该结构保存在private_data域中,每个由epoll_create得到的文件描述符都分配了一个该结构体,让我们来看一下struct eventpll的内部结构:

struct eventpoll {  
    /* 用于维护自身的状态,可用于中断上下文 */  
    spinlock_t lock;  
    /*  
     * 用户进程上下文中  
     */  
    struct mutex mtx;  
    /* 进程等待队列,由 sys_epoll_wait()使用,调用epoll_wait时,休眠在这里 */  
    wait_queue_head_t wq;  
    /* 进程等待队列,由 file->poll()使用 ,epollfd本身被poll时,休眠在这里*/  
    wait_queue_head_t poll_wait;  
    /* 就绪文件描述符链表 */  
    struct list_head rdllist;  
    /* 红黑树头节点,该红黑树用于存储要监控的文件描述符 */  
    struct rb_root rbr;  
    /*  
     * ready事件的临时存放链表  
     */  
    struct epitem *ovflist;  
    /* 创建eventpoll descriptor的用户 */  
    struct user_struct *user;  
};

epoll_ctl 接口加入该epoll描述符监听的套接字属于socket filesystem,每一个都对应一个epitem结构体,该结构以红黑树的方式存储,eventpoll中的rbr成员指向该红黑树的root节点,而有监听事件到来的套接字结构以双向连表的形式保存,其头结点对应eventpoll中的rdllist成员。

struct epitem {  
    /*红黑树节点 */  
    struct rb_node rbn;  
    /*就绪描述符链表节点 */  
    struct list_head rdllink;  
    /*  
     * Works together "struct eventpoll"->ovflist in keeping the  
     * single linked chain of items.  
     */  
    struct epitem *next;  
    /* 本结构对应的文件描述符信息 */  
    struct epoll_filefd ffd;  
    /* Number of active wait queue attached to poll operations */  
    int nwait;  
    /* List containing poll wait queues */  
    struct list_head pwqlist;  
    /* The "container" of this item */  
    struct eventpoll *ep;  
    /* List header used to link this item to the "struct file" items list */  
    struct list_head fllink;  
    /* The structure that describe the interested events and the source fd */  
    struct epoll_event event;  
};

上述结构中fllink是指向文件系统链表的立案表头,struct file 称为文件结构,一般代表一个打开的文件描述符。

而epoll_filefd结构则表明了epite

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值