• 驱动开发,IO多路复用实现过程,epoll方式


    1.框架图

    被称为当前时代最好用的io多路复用方式;

    核心操作:一棵树(红黑树)、一张表(内核链表)以及三个接口;

     思想:(fd代表文件描述符)

            epoll要把检测的事件fd挂载到内核空间红黑树上,遍历红黑树,调用每个fd对应的操作方法,找到发生事件的fd,如果没有发生事件的fd,进程休眠,如果事件发生,将发生事件的fd拷贝一份放到内核链表,每个节点对应一个fd,最后把链表的节点信息传递到用户空间的数组中,用户空间无需判断事件的发生,需要判断事件类型(读写等);

     

    2.代码

    ---pro1.c---应用程序(epoll方式)
    1. #include <sys/types.h>
    2. #include <sys/stat.h>
    3. #include <fcntl.h>
    4. #include <stdio.h>
    5. #include <string.h>
    6. #include <unistd.h>
    7. #include <stdlib.h>
    8. #include <sys/ioctl.h>
    9. #include <sys/select.h>
    10. #include <sys/time.h>
    11. #include <sys/epoll.h>
    12. int main(int argc, const char *argv[])
    13. {
    14. int fd1,fd2,epfd;
    15. char buf[128] = {0};
    16. struct epoll_event event; //用于操作epoll
    17. struct epoll_event events[10]; //用户空间存放发生事件的数组
    18. //创建epoll句柄,红黑树根节点
    19. epfd = epoll_create(1);
    20. if(epfd < 0)
    21. {
    22. printf("epoll_create fail\n");
    23. exit(-1);
    24. }
    25. //打开设备文件
    26. fd1 = open("/dev/input/mouse0", O_RDWR);
    27. if (fd1 < 0)
    28. {
    29. printf("鼠标事件文件失败\n");
    30. exit(-1);
    31. }
    32. fd2 = open("/dev/myled0", O_RDWR);
    33. if (fd2 < 0)
    34. {
    35. printf("自定义事件文件失败\n");
    36. exit(-1);
    37. }
    38. //添加准备就绪事件到epoll
    39. event.events = EPOLLIN; //读事件
    40. event.data.fd = fd1;
    41. if((epoll_ctl(epfd,EPOLL_CTL_ADD,fd1,&event)) < 0)
    42. {
    43. printf("epoll_ctl fd1 fail\n");
    44. }
    45. event.events = EPOLLIN; //读事件
    46. event.data.fd = fd2;
    47. if((epoll_ctl(epfd,EPOLL_CTL_ADD,fd2,&event)) < 0)
    48. {
    49. printf("epoll_ctl fd2 fail\n");
    50. }
    51. //监听时间是否发生
    52. while(1)
    53. {
    54. //成功接收返回时间的个数,放入events数组中
    55. int ret = epoll_wait(epfd,events,10,-1);
    56. if(ret < 0)
    57. {
    58. printf("epoll_wait fail\n");
    59. exit(-1);
    60. }
    61. int i;
    62. //循环遍历数组,做事件的处理
    63. for(i=0; i<ret; i++)
    64. {
    65. if(events[i].events & EPOLLIN) //发生事件是读事件
    66. {
    67. read(events[i].data.fd,buf,sizeof(buf));
    68. printf("buf:%s\n",buf);
    69. memset(buf,0,sizeof(buf));
    70. }
    71. }
    72. }
    73. close(fd1);
    74. close(fd2);
    75. return 0;
    76. }
    ---pro2.c---应用程序(模拟自定义设备数据就绪)
    1. #include <sys/types.h>
    2. #include <sys/stat.h>
    3. #include <fcntl.h>
    4. #include <stdio.h>
    5. #include <string.h>
    6. #include <unistd.h>
    7. #include <stdlib.h>
    8. int main(int argc, const char *argv[])
    9. {
    10. char buf[128] = "hello world";
    11. int fd = open("/dev/myled0", O_RDWR);
    12. if (fd < 0)
    13. {
    14. printf("打开设备文件失败\n");
    15. exit(-1);
    16. }
    17. write(fd, buf, sizeof(buf));
    18. close(fd);
    19. return 0;
    20. }
    ---epoll.c---驱动程序

     

    1. #include <linux/init.h>
    2. #include <linux/module.h>
    3. #include <linux/fs.h>
    4. #include <linux/io.h>
    5. #include <linux/device.h>
    6. #include <linux/uaccess.h>
    7. #include <linux/wait.h>
    8. #include<linux/poll.h>
    9. char kbuf[128] = {0};
    10. unsigned int major;
    11. struct class *cls;
    12. struct device *dev;
    13. unsigned int condition = 0;
    14. // 定义一个等待队列头
    15. wait_queue_head_t wq_head;
    16. // 封装操作方法
    17. int mycdev_open(struct inode *inode, struct file *file)
    18. {
    19. printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    20. return 0;
    21. }
    22. ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *lof)
    23. {
    24. int ret;
    25. ret = copy_to_user(ubuf, kbuf, size);
    26. if (ret)
    27. {
    28. printk("copy_to_ user err\n");
    29. return -EIO;
    30. }
    31. condition = 0; // 下一次硬件数据没有就绪
    32. return 0;
    33. }
    34. ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
    35. {
    36. int ret;
    37. // 从用户拷贝数据,模拟硬件数据
    38. ret = copy_from_user(kbuf, ubuf, size);
    39. if (ret)
    40. {
    41. printk("copy_from_user err\n");
    42. return -EIO;
    43. }
    44. condition = 1;
    45. wake_up_interruptible(&wq_head);
    46. return 0;
    47. }
    48. //封装POLL方法
    49. __poll_t mycdev_poll(struct file *file, struct poll_table_struct *wait)
    50. {
    51. __poll_t mask = 0;
    52. //向上提交等待队列头
    53. poll_wait(file,&wq_head,wait);
    54. //根据事件是否发生给一个合适的返回值
    55. if(condition)
    56. {
    57. mask = POLLIN;
    58. }
    59. return mask;
    60. }
    61. int mycdev_close(struct inode *inode, struct file *file)
    62. {
    63. printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    64. return 0;
    65. }
    66. struct file_operations fops = {
    67. .open = mycdev_open,
    68. .read = mycdev_read,
    69. .poll = mycdev_poll,
    70. .write = mycdev_write,
    71. .release = mycdev_close,
    72. };
    73. // 入口函数
    74. static int __init mycdev_init(void)
    75. {
    76. //初始化等待队列
    77. init_waitqueue_head(&wq_head);
    78. major = register_chrdev(0, "myled", &fops);
    79. if (major < 0)
    80. {
    81. printk("字符设备驱动注册失败\n");
    82. return major;
    83. }
    84. printk("字符设备驱动注册成功:major=%d\n", major);
    85. // 向上提交目录
    86. cls = class_create(THIS_MODULE, "MYLED");
    87. if (IS_ERR(cls))
    88. {
    89. printk("向上提交目录失败\n");
    90. return -PTR_ERR(cls);
    91. }
    92. printk("向上提交目录成功\n");
    93. // 向上提交设备节点信息
    94. int i;
    95. for (i = 0; i < 3; i++)
    96. {
    97. dev = device_create(cls, NULL, MKDEV(major, i), NULL, "myled%d", i);
    98. if (IS_ERR(dev))
    99. {
    100. printk("向上提交设备节点信息失败\n");
    101. return -PTR_ERR(dev);
    102. }
    103. }
    104. printk("向上提交设备节点信息成功\n");
    105. return 0;
    106. }
    107. // 出口函数
    108. static void __exit mycdev_exit(void)
    109. {
    110. // 销毁设备节点信息
    111. int i;
    112. for (i = 0; i < 3; i++)
    113. {
    114. device_destroy(cls, MKDEV(major, i));
    115. }
    116. // 销毁目录信息
    117. class_destroy(cls);
    118. // 字符设备驱动注销
    119. unregister_chrdev(major, "myled");
    120. }
    121. // 声明
    122. // 入口函数地址
    123. module_init(mycdev_init);
    124. // 出口函数地址
    125. module_exit(mycdev_exit);
    126. // 遵循的GPL协议
    127. MODULE_LICENSE("GPL");

     

    3.测试结果

     执行pro2.c,自定义事件被监听到;

     在ubuntu上动鼠标,鼠标事件被监听;

  • 相关阅读:
    os_cfg.h、os_cpu.h和ucos_ii.h
    在centos7下docker 制作 java8镜像,上传到阿里云镜像仓库
    第11章 AOF持久化
    qt中qstring合并字符串
    安装和应用anaconda过程中的一些问题
    为什么说网络安全是风口行业?是it行业最后的红利?
    2023.10.17
    CMakeLists编译前拷贝文件或目录
    虚拟机使用 WinSCP & Putty
    前端入门学习笔记四十六
  • 原文地址:https://blog.csdn.net/weixin_46260677/article/details/132911473