0726_驱动3 epoll

一、epoll使用API接口

#include <sys/epoll.h>
int epoll_create(int size)
函数功能:创建epoll实例
参数:
    size:没有实际含义,但是必须填充大于0的值
返回值:
    成功返回文件描述符
    失败返回-1,置位错误码

int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event)
函数功能:对epoll实例控制
参数:
    epfd:epoll_create创建文件描述符
    op:操作方法
        EPOLL_CTL_ADD:向epoll实例中添加对epoll控制
        EPOLL_CTL_MOD:修改epoll实例中对epoll控制
        EPOLL_CTL_DEL:删除epoll实例中对epoll控制
    fd:被操作的文件描述符,也就是open打开的文件描述符
    event:时间结构体
    typedef union epoll_data {
        void        *ptr;
        int          fd; ============> 被操作的文件描述符
        uint32_t     u32;
        uint64_t     u64;
    } epoll_data_t;
  
    struct epoll_event {
        uint32_t     events;      /* 事件类型 */ EPOLLIN 可读事件

        epoll_data_t data;        /* 用户数据 */
    };
返回值:
    成功返回0
    失败返回-1,置位错误码
    
int epoll_wait(int epfd, struct epoll_event *events,
int maxevents, int timeout)
函数功能:阻塞监听准备好的事件结构体
参数:
    epfd:epoll_create创建文件描述符
    events:返回准备好的事件结构体
    maxevents:监听最大文件描述符个数
    timeout:不关心超时,填写-1    
返回值:
    成功返回准备好的文件描述符个数
    失败返回-1,置位错误码

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
epoll 是 Linux 内核提供的一种高效的 I/O 多路复用机制,可以用于开发高性能的网络应用程序。epoll驱动开发中主要用于实现异步 I/O 操作,提高系统的吞吐量和并发性能。 在驱动中使用 epoll,需要调用 `epoll_create` 创建一个 epoll 实例,并使用 `epoll_ctl` 向 epoll 实例中添加文件描述符,然后调用 `epoll_wait` 等待事件的发生。具体步骤如下: 1. 调用 `epoll_create` 创建一个 epoll 实例,返回一个文件描述符。 2. 通过 `epoll_ctl` 向 epoll 实例中添加文件描述符,指定要监听的事件类型和事件处理函数。 3. 调用 `epoll_wait` 等待事件的发生,返回就绪的文件描述符列表。 4. 处理就绪的文件描述符,执行相应的操作。 下面是示例代码: ```c #include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> #include <linux/errno.h> #include <linux/types.h> #include <linux/proc_fs.h> #include <linux/fcntl.h> #include <linux/poll.h> #include <linux/sched.h> #include <linux/kthread.h> #include <linux/delay.h> #include <linux/wait.h> #include <linux/err.h> #include <linux/device.h> #include <linux/epoll.h> static int my_fd; static struct file *my_file; static int my_event_handler(struct file *file, struct epoll_event *event) { // 处理事件 return 0; } static int my_driver_init(void) { int ret; struct epoll_event event; // 打开文件 my_file = filp_open("/dev/mydevice", O_RDONLY, 0); if (IS_ERR(my_file)) { printk(KERN_ERR "Failed to open mydevice: %ld\n", PTR_ERR(my_file)); return PTR_ERR(my_file); } // 创建 epoll 实例 my_fd = epoll_create(1); if (my_fd < 0) { printk(KERN_ERR "Failed to create epoll instance: %d\n", my_fd); filp_close(my_file, NULL); return my_fd; } // 添加文件描述符到 epoll 实例中 event.events = EPOLLIN; event.data.fd = my_file->f_dentry->d_inode->i_ino; ret = epoll_ctl(my_fd, EPOLL_CTL_ADD, my_file->f_dentry->d_inode->i_ino, &event); if (ret < 0) { printk(KERN_ERR "Failed to add mydevice to epoll instance: %d\n", ret); epoll_close(my_fd); filp_close(my_file, NULL); return ret; } return 0; } static void my_driver_exit(void) { // 移除文件描述符 epoll_ctl(my_fd, EPOLL_CTL_DEL, my_file->f_dentry->d_inode->i_ino, NULL); // 关闭文件和 epoll 实例 filp_close(my_file, NULL); epoll_close(my_fd); } module_init(my_driver_init); module_exit(my_driver_exit); MODULE_LICENSE("GPL"); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值