• Linux驱动实现IO模型


    Linux系统分为内核态和用户态,CPU会在这两个状态之间进行切换。当进行IO操作时,应用程序会使用系统调用进入内核态,内核操作系统会准备好数据,把IO设备的数据加载到内核缓冲区。

    然后内核操作系统会把内核缓冲区的数据从内核空间拷贝到用户空间。但是进行IO操作时,CPU和内存的速度远远高于外设的速度,所以需要我们使用IO模型编程解决。

    IO模型的种类:

    • 阻塞IO
    • 非阻塞IO
    • IO多路复用
    • 信号驱动IO
    • 异步IO

    阻塞IO

    当进程以阻塞的方式打开设备文件时(默认方式),如果资源不可用,那么进程阻塞,也就是进程休眠。既然有休眠,就有对应的唤醒操作,否则进程将会一直休眠下去。驱动程序应该在资源可用时负责唤醒操作。相比于非阻塞IO,其最大的优点就是,资源不可用时,不占用CPU的时间,而非阻塞IO必须要定期尝试,看看资源是否可以获得,这对于键盘鼠标这类设备来说,其效率非常低。但是阻塞IO也有一个明显的缺点,那就是进程在休眠期间再也不能做其他事情。

    通过上面的描述,发现要实现阻塞操作最重要的数据结构就是等待队列。不了解等待队列的请参考(等待队列)。

    下面通过一个简单的驱动程序来实现阻塞IO模型。

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. struct devices {
    12. char buffer[32];
    13. int flag;
    14. };
    15. struct devices qw_dev;
    16. DECLARE_WAIT_QUEUE_HEAD(wqh);
    17. int test_open(struct inode *inode, struct file *filp)
    18. {
    19. filp->private_data = &qw_dev;
    20. printk("dev open!\n");
    21. return 0;
    22. }
    23. ssize_t test_read(struct file *filp, char __user *buf, size_t size, loff_t *offset)
    24. {
    25. struct devices *dev = (struct devices *)filp->private_data;
    26. wait_event_interruptible(wqh, dev->flag);
    27. if(copy_to_user(buf, dev->buffer, size)!=0) {
    28. printk("copy to user error\n");
    29. return -EFAULT;
    30. }
    31. return size;
    32. }
    33. ssize_t test_write(struct file *filp, const char __user *buf, size_t size, loff_t *offset)
    34. {
    35. struct devices *dev = (struct devices *)filp->private_data;
    36. if(copy_from_user(dev->buffer, buf, size)) {
    37. printk("copy from user error\n");
    38. return -EFAULT;
    39. }
    40. dev->flag = 1;
    41. wake_up_interruptible(&wqh);
    42. printk("WRITE: %s\n", dev->buffer);
    43. return size;
    44. }
    45. int test_release(struct inode *inode, struct file *filp)
    46. {
    47. printk("dev close!\n");
    48. return 0;
    49. }
    50. //声明操作函数集合
    51. struct file_operations wq_fops = {
    52. .owner = THIS_MODULE,
    53. .open = test_open,
    54. .read = test_read,
    55. .write = test_write,
    56. .release = test_release,
    57. };
    58. //分配初始化miscdevice
    59. struct miscdevice misc_dev = {
    60. .minor = MISC_DYNAMIC_MINOR,//系统分配次设备号
    61. .name = "qw",//设备文件名
    62. .fops = &wq_fops,//操作函数集合
    63. };
    64. //加载函数
    65. int test_init(void)
    66. {
    67. int ret;
    68. //注册miscdevice
    69. ret = misc_register(&misc_dev);
    70. if (ret < 0) {
    71. misc_deregister(&misc_dev);
    72. return -1;
    73. }
    74. qw_dev.flag = 0;
    75. return 0;
    76. }
    77. //卸载函数
    78. void test_exit(void)
    79. {
    80. //注销miscdevice
    81. misc_deregister(&misc_dev);
    82. }
    83. //声明为模块的入口和出口
    84. module_init(test_init);
    85. module_exit(test_exit);
    86. MODULE_LICENSE("GPL");//GPL模块许可证
    87. MODULE_AUTHOR("xin");//作者
    88. MODULE_VERSION("2.0");//版本
    89. MODULE_DESCRIPTION("WQ driver!");//描述信息

    向驱动写数据的应用程序。 

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. int main(int argc, char *argv[])
    8. {
    9. int fd;
    10. char buf2[32] = "hello world";
    11. fd = open("/dev/qw", O_RDWR);
    12. if (fd < 0) {
    13. perror("open error");
    14. return fd;
    15. }
    16. write(fd, buf2, strlen(buf2));
    17. close(fd);
    18. return 0;
    19. }

    向驱动读数据的应用程序。 

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. int main(int argc, char *argv[])
    8. {
    9. int fd;
    10. char buf1[32] = {0};
    11. fd = open("/dev/qw", O_RDWR);
    12. if (fd < 0) {
    13. perror("open error");
    14. return fd;
    15. }
    16. read(fd, buf1, 32);
    17. printf("READ:%s\n",buf1);
    18. close(fd);
    19. return 0;
    20. }

    将驱动模块加载到内核中后,先运行读程序,发现程序会阻塞。然后另开一个终端运行写程序,读程序才会打印写程序的数据。

    非阻塞IO

    设备不一定随时都能给用户提供服务,这就有了资源可用和不可用两种状态。如果应用程序以非阻塞的方法打开设备文件,当资源不可用时,驱动就应该立即返回,并用一个错误码EAGAIN来通知应用程序此时资源不可用,应用程序应该稍后再尝试。

    下面通过一个简单的驱动程序来实现阻塞IO模型。在阻塞IO模型上进行修改,当应用程序打开设备文件时,以非阻塞方式打开 。

    fd = open("/dev/qw", O_RDWR | O_NONBLOCK);

    驱动程序读函数中判断设备是否以非阻塞方式打开,并且资源是否准备好。struct file 结构体中的f_flags成员存储打开设备文件的标志。

    1. if (filp->f_flags & O_NONBLOCK) {
    2. if (dev->flag != 1) {
    3. return -EAGAIN;
    4. }
    5. }

    驱动程序 

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. struct devices {
    12. char buffer[32];
    13. int flag;
    14. };
    15. struct devices qw_dev;
    16. DECLARE_WAIT_QUEUE_HEAD(wqh);
    17. int test_open(struct inode *inode, struct file *filp)
    18. {
    19. filp->private_data = &qw_dev;
    20. printk("dev open!\n");
    21. return 0;
    22. }
    23. ssize_t test_read(struct file *filp, char __user *buf, size_t size, loff_t *offset)
    24. {
    25. struct devices *dev = (struct devices *)filp->private_data;
    26. if (filp->f_flags & O_NONBLOCK) {
    27. if (dev->flag != 1) {
    28. return -EAGAIN;
    29. }
    30. }
    31. wait_event_interruptible(wqh, dev->flag);
    32. if(copy_to_user(buf, dev->buffer, size)!=0) {
    33. printk("copy to user error\n");
    34. return -EFAULT;
    35. }
    36. return size;
    37. }
    38. ssize_t test_write(struct file *filp, const char __user *buf, size_t size, loff_t *offset)
    39. {
    40. struct devices *dev = (struct devices *)filp->private_data;
    41. if(copy_from_user(dev->buffer, buf, size)) {
    42. printk("copy from user error\n");
    43. return -EFAULT;
    44. }
    45. dev->flag = 1;
    46. wake_up_interruptible(&wqh);
    47. printk("WRITE: %s\n", dev->buffer);
    48. return size;
    49. }
    50. int test_release(struct inode *inode, struct file *filp)
    51. {
    52. printk("dev close!\n");
    53. return 0;
    54. }
    55. //声明操作函数集合
    56. struct file_operations wq_fops = {
    57. .owner = THIS_MODULE,
    58. .open = test_open,
    59. .read = test_read,
    60. .write = test_write,
    61. .release = test_release,
    62. };
    63. //分配初始化miscdevice
    64. struct miscdevice misc_dev = {
    65. .minor = MISC_DYNAMIC_MINOR,//系统分配次设备号
    66. .name = "qw",//设备文件名
    67. .fops = &wq_fops,//操作函数集合
    68. };
    69. //加载函数
    70. int test_init(void)
    71. {
    72. int ret;
    73. //注册miscdevice
    74. ret = misc_register(&misc_dev);
    75. if (ret < 0) {
    76. misc_deregister(&misc_dev);
    77. return -1;
    78. }
    79. qw_dev.flag = 0;
    80. return 0;
    81. }
    82. //卸载函数
    83. void test_exit(void)
    84. {
    85. //注销miscdevice
    86. misc_deregister(&misc_dev);
    87. }
    88. //声明为模块的入口和出口
    89. module_init(test_init);
    90. module_exit(test_exit);
    91. MODULE_LICENSE("GPL");//GPL模块许可证
    92. MODULE_AUTHOR("xin");//作者
    93. MODULE_VERSION("2.0");//版本
    94. MODULE_DESCRIPTION("WQ driver!");//描述信息

    读应用程序如下所示,写程序前面的一样就不展示出来。

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. int main(int argc, char *argv[])
    8. {
    9. int fd;
    10. char buf1[32] = {0};
    11. fd = open("/dev/qw", O_RDWR | O_NONBLOCK);
    12. if (fd < 0) {
    13. perror("open error");
    14. return fd;
    15. }
    16. while(1) {
    17. read(fd, buf1, 32);
    18. printf("READ:%s\n",buf1);
    19. sleep(1);
    20. }
    21. close(fd);
    22. return 0;
    23. }

    将驱动模块加载到内核中后,先运行读程序,发现程序不会阻塞,但是没有数据。然后另开一个终端运行写程序,读程序才会打印出写程序的数据。

    IO多路复用

    阻塞IO优点是在设备资源不可用时,进程主动放弃CPU,但是进程阻塞后不能做其他操作。非阻塞IO优点是资源不可用时不会阻塞,但是会不停地轮询,系统效率降低。当一个进程要同时对多个设备进程操作时以上两种方法显得非常不方便。这就需要使用到IO复用模型。

    它允许一个进程可以同时监视多个文件描述符,并且可以在其中任何一个文件描述符上等待数据可读或可写,从而实现并发IO操作。在应用程序中Linux提供了三种API函数:poll,select和epoll。在驱动程序中只需要实现poll函数。

    poll和select基本上是一样 ,都可以监听多个文件描述符,通过轮询多个文件描述符来获得已经准备好的文件描述符。epoll是将主动轮询变成了被动通知,当事件发生时,被动的接受通知。

    接下来以poll为例进行说明,应用程序中poll系统调用的原型及相关的数据类型如下。

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

    struct pollfd {

            int fd;

            short events;

            short revents;

    };

    POLLIN There is data to read.

    POLLOUT Writing now will not block.

    POLLRDNORM Equivalent to POLLIN.

    POLLWRNORM Equivalent to POLLOUT.

    poll的第一个参数是要监听的文件描述符集合,类型为指向struct pollfd的指针,struct pollfd有3个成员,fd是要监听文件描述符,events是监听的事件,revents是返回的事件。常见的事件有POLLIN、POLLOUT,分别表示设备可以无阻塞地读、写。POLLRDNORM和POLLWRNORM是在_XOPEN_SOURCE宏被定义时所引入的事件,POLLRDNORM通常和POLLIN等价,POLLWRNORM通常和POLLOUT等价。

    poll函数的第二个参数是要监听的文件描述符的个数,第三个参数的毫秒的超时值,负数表示一直监听,直到被监听的文件描述符集合中的任意一个设备发生了事件才会返回。函数返回值为-1表示失败,成功返回revents不为0的文件描述符个数。

    读应用程序如下所示,写程序前面的一样就不展示出来。

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. int main(int argc, char *argv[])
    9. {
    10. int fd;
    11. int ret;
    12. char buf1[32] = {0};
    13. struct pollfd fds[1];
    14. fd = open("/dev/qw", O_RDWR | O_NONBLOCK);
    15. if (fd < 0) {
    16. perror("open error");
    17. return fd;
    18. }
    19. fds[0].fd = fd;
    20. fds[0].events = POLLIN;
    21. while(1) {
    22. ret = poll(fds, 1, 5000);
    23. if (!ret) {
    24. printf("timeout\n");
    25. } else if (fds[0].revents == POLLIN) {
    26. read(fd, buf1, 32);
    27. printf("READ:%s\n",buf1);
    28. sleep(1);
    29. }
    30. }
    31. close(fd);
    32. return 0;
    33. }

    驱动中poll函数需要完成两点:1. 对可能引起设备文件状态变化的等待队列调用poll_wait函数,将对应的等待队列头添加到poll_table。2. 返回表示是否能对设备进行无阻塞读写访问的掩码。

    1. static __poll_t test_poll(struct file *filp, struct poll_table_struct *p)
    2. {
    3. struct devices *dev = (struct devices *)filp->private_data;
    4. __poll_t mask = 0;
    5. poll_wait(filp, &wqh, p);
    6. if (dev->flag = 1) {
    7. mask |= POLLIN;
    8. }
    9. return mask;
    10. }

    注意:poll_wait函数不会阻塞。

    驱动程序

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. struct devices {
    13. char buffer[32];
    14. int flag;
    15. };
    16. struct devices qw_dev;
    17. DECLARE_WAIT_QUEUE_HEAD(wqh);
    18. int test_open(struct inode *inode, struct file *filp)
    19. {
    20. filp->private_data = &qw_dev;
    21. printk("dev open!\n");
    22. return 0;
    23. }
    24. ssize_t test_read(struct file *filp, char __user *buf, size_t size, loff_t *offset)
    25. {
    26. struct devices *dev = (struct devices *)filp->private_data;
    27. if (filp->f_flags & O_NONBLOCK) {
    28. if (dev->flag != 1) {
    29. return -EAGAIN;
    30. }
    31. }
    32. wait_event_interruptible(wqh, dev->flag);
    33. if(copy_to_user(buf, dev->buffer, size)!=0) {
    34. printk("copy to user error\n");
    35. return -EFAULT;
    36. }
    37. return size;
    38. }
    39. ssize_t test_write(struct file *filp, const char __user *buf, size_t size, loff_t *offset)
    40. {
    41. struct devices *dev = (struct devices *)filp->private_data;
    42. if(copy_from_user(dev->buffer, buf, size)) {
    43. printk("copy from user error\n");
    44. return -EFAULT;
    45. }
    46. dev->flag = 1;
    47. wake_up_interruptible(&wqh);
    48. printk("WRITE: %s\n", dev->buffer);
    49. return size;
    50. }
    51. static __poll_t test_poll(struct file *filp, struct poll_table_struct *p)
    52. {
    53. struct devices *dev = (struct devices *)filp->private_data;
    54. __poll_t mask = 0;
    55. poll_wait(filp, &wqh, p);
    56. if (dev->flag == 1) {
    57. mask |= POLLIN;
    58. }
    59. return mask;
    60. }
    61. int test_release(struct inode *inode, struct file *filp)
    62. {
    63. printk("dev close!\n");
    64. return 0;
    65. }
    66. //声明操作函数集合
    67. struct file_operations wq_fops = {
    68. .owner = THIS_MODULE,
    69. .open = test_open,
    70. .read = test_read,
    71. .write = test_write,
    72. .poll = test_poll,
    73. .release = test_release,
    74. };
    75. //分配初始化miscdevice
    76. struct miscdevice misc_dev = {
    77. .minor = MISC_DYNAMIC_MINOR,//系统分配次设备号
    78. .name = "qw",//设备文件名
    79. .fops = &wq_fops,//操作函数集合
    80. };
    81. //加载函数
    82. int test_init(void)
    83. {
    84. int ret;
    85. //注册miscdevice
    86. ret = misc_register(&misc_dev);
    87. if (ret < 0) {
    88. misc_deregister(&misc_dev);
    89. return -1;
    90. }
    91. qw_dev.flag = 0;
    92. return 0;
    93. }
    94. //卸载函数
    95. void test_exit(void)
    96. {
    97. //注销miscdevice
    98. misc_deregister(&misc_dev);
    99. }
    100. //声明为模块的入口和出口
    101. module_init(test_init);
    102. module_exit(test_exit);
    103. MODULE_LICENSE("GPL");//GPL模块许可证
    104. MODULE_AUTHOR("xin");//作者
    105. MODULE_VERSION("2.0");//版本
    106. MODULE_DESCRIPTION("WQ driver!");//描述信息

    将驱动模块加载到内核中后,先运行读程序,发现程序不会阻塞,但是没有数据且每5s打印超时。然后另开一个终端运行写程序,读程序才会打印出写程序的数据。

    信号驱动IO

    在信号驱动IO中,进程使用系统调用 sigaction() 来注册一个信号处理函数,该函数会在IO事件就绪时被内核调用。当进程调用 sigaction() 注册一个信号处理函数时,它需要指定一个描述符和一个事件,内核在检测到该描述符上发生指定事件时,会向进程发送指定信号。进程可以通过捕获该信号并执行信号处理函数。

    与其他IO多路复用技术相比,信号驱动IO也避免了轮询机制的开销,从而减少了 CPU 的占用。但是,信号驱动IO也存在一些缺点。首先,它对信号的处理需要一定的时间,因此它不适合高速IO操作。其次,由于信号是不可靠的,因此在使用信号驱动IO时需要考虑到信号可能会丢失的情况。

    在应用程序使用信号驱动IO,需要完成以下几步:1. 注册信号处理函数,应用程序使用 sigaction()来注册一个信号处理函数。2. 设置能接受这个信号的进程。 3. 开启信号驱动IO,通常是使用fcntl函数的F_SETFL命令打开FASYNC标志。

    fcntl系统调用可以用来对已打开的文件描述符进行各种控制操作以改变已打开文件的的各种属性。

    函数原型:

    1. #include
    2. #include
    3. int fcntl(int fd, int cmd);
    4. int fcntl(int fd, int cmd, long arg);
    5. int fcntl(int fd, int cmd ,struct flock* lock);

    fcntl函数功能依据cmd的值的不同而不同。参数对应功能如下:

    读应用程序如下所示,写程序前面的一样就不展示出来。 

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. int fd;
    9. char buf1[32] = {0};
    10. static void test_func(int signal)
    11. {
    12. read(fd, buf1, 32);
    13. printf("READ:%s\n",buf1);
    14. }
    15. int main(int argc, char *argv[])
    16. {
    17. int flags;
    18. fd = open("/dev/qw", O_RDWR | O_NONBLOCK);
    19. if (fd < 0) {
    20. perror("open error");
    21. return fd;
    22. }
    23. signal(SIGIO, test_func); //注册信号处理函数test_func
    24. fcntl(fd, F_SETOWN, getpid()); //设置当前进程接收SIGIO信号
    25. flags = fcntl(fd, F_GETFD); //获得文件描述符标志
    26. fcntl(fd, F_SETFL, flags | FASYNC); //设置文件状态标志,在原来文件描述符标志上开启FASYNC标志
    27. while(1);
    28. close(fd);
    29. return 0;
    30. }

    当应用程序开启信号驱动IO时,会触发驱动程序中的fasync函数,而fasync函数会调用fasync_helper函数去初始化fasync_struct结构体。 当数据到达时调用kill_fasync函数用来通知应用程序,然后应用程序执行信号处理函数,它的参数是被传递的信号(常常是 SIGIO)和 band 。band:可读时设置成POLLIN,可写时设置成POLLOUT。 

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. struct devices {
    13. char buffer[32];
    14. int flag;
    15. struct fasync_struct *fasync;
    16. };
    17. struct devices qw_dev;
    18. DECLARE_WAIT_QUEUE_HEAD(wqh);
    19. int test_open(struct inode *inode, struct file *filp)
    20. {
    21. filp->private_data = &qw_dev;
    22. printk("dev open!\n");
    23. return 0;
    24. }
    25. ssize_t test_read(struct file *filp, char __user *buf, size_t size, loff_t *offset)
    26. {
    27. struct devices *dev = (struct devices *)filp->private_data;
    28. if (filp->f_flags & O_NONBLOCK) {
    29. if (dev->flag != 1) {
    30. return -EAGAIN;
    31. }
    32. }
    33. wait_event_interruptible(wqh, dev->flag);
    34. if(copy_to_user(buf, dev->buffer, size)!=0) {
    35. printk("copy to user error\n");
    36. return -EFAULT;
    37. }
    38. return size;
    39. }
    40. ssize_t test_write(struct file *filp, const char __user *buf, size_t size, loff_t *offset)
    41. {
    42. struct devices *dev = (struct devices *)filp->private_data;
    43. if(copy_from_user(dev->buffer, buf, size)) {
    44. printk("copy from user error\n");
    45. return -EFAULT;
    46. }
    47. dev->flag = 1;
    48. wake_up_interruptible(&wqh);
    49. kill_fasync(&dev->fasync, SIGIO, POLLIN);
    50. printk("WRITE: %s\n", dev->buffer);
    51. return size;
    52. }
    53. static __poll_t test_poll(struct file *filp, struct poll_table_struct *p)
    54. {
    55. struct devices *dev = (struct devices *)filp->private_data;
    56. __poll_t mask = 0;
    57. poll_wait(filp, &wqh, p);
    58. if (dev->flag == 1) {
    59. mask |= POLLIN;
    60. }
    61. return mask;
    62. }
    63. static int test_fasync(int fd, struct file *filp, int on)
    64. {
    65. struct devices *dev = (struct devices *)filp->private_data;
    66. return fasync_helper(fd, filp, on, &dev->fasync); //对fasync_struct结构体进行初始化
    67. }
    68. int test_release(struct inode *inode, struct file *filp)
    69. {
    70. printk("dev close!\n");
    71. return 0;
    72. }
    73. //声明操作函数集合
    74. struct file_operations wq_fops = {
    75. .owner = THIS_MODULE,
    76. .open = test_open,
    77. .read = test_read,
    78. .write = test_write,
    79. .poll = test_poll,
    80. .fasync = test_fasync,
    81. .release = test_release,
    82. };
    83. //分配初始化miscdevice
    84. struct miscdevice misc_dev = {
    85. .minor = MISC_DYNAMIC_MINOR,//系统分配次设备号
    86. .name = "qw",//设备文件名
    87. .fops = &wq_fops,//操作函数集合
    88. };
    89. //加载函数
    90. int test_init(void)
    91. {
    92. int ret;
    93. //注册miscdevice
    94. ret = misc_register(&misc_dev);
    95. if (ret < 0) {
    96. misc_deregister(&misc_dev);
    97. return -1;
    98. }
    99. qw_dev.flag = 0;
    100. return 0;
    101. }
    102. //卸载函数
    103. void test_exit(void)
    104. {
    105. //注销miscdevice
    106. misc_deregister(&misc_dev);
    107. }
    108. //声明为模块的入口和出口
    109. module_init(test_init);
    110. module_exit(test_exit);
    111. MODULE_LICENSE("GPL");//GPL模块许可证
    112. MODULE_AUTHOR("xin");//作者
    113. MODULE_VERSION("2.0");//版本
    114. MODULE_DESCRIPTION("WQ driver!");//描述信息

    将驱动模块加载到内核中后,先运行读程序,发现程序会阻塞,这是因为用while(1)来模拟程序继续处理其他事情。然后另开一个终端运行写程序,读程序才会打印出写程序的数据。

    异步IO

    相对于同步IO,异步IO不是顺序执行。用户进程进行aio_read系统调用之后,无论内核数据是否准备好,都会直接返回给用户进程,然后用户态进程可以去做别的事情。等到设备数据准备好了,内核直接复制数据给进程,然后从内核向进程发送通知。IO两个阶段,进程都是非阻塞的。异步IO可以在空户空间的glibc库实现,不依赖内核就不举实例了。

    总结 

    阻塞IO实现简单,但是性能不佳,非阻塞IO虽然不需要阻塞线程,但是他需要轮询操作低效。

    IO多路复用有三种实现,select和poll都是使用了轮询的方式,监听文件描述符不能太多。epoll的实现使用了红黑树,增删改文件描述符效率高,并且使用了事件触发机制,不需要进行轮询。

    信号驱动IO依赖于信号机制,它对信号的处理需要一定的时间,因此它不适合高速IO操作。

    异步IO模型的实现比同步IO模型更加复杂,需要使用操作系统提供的通知机制来处理IO完成的事件,同时也需要考虑到异步IO可能会引入的竞争条件和死锁问题。

    以上就将5种I/O模型原理介绍完了,并且还用程序模拟实现了,如果有什么疑问或者建议欢迎在评论区中提出来嗷~。

    参考文章:深入理解Linux的五种IO模型linux五种IO模型

  • 相关阅读:
    为何海量计算机系毕业生“负债”报IT培训班?高校IT教育该如何变革?
    Python 进阶语法:JSON
    VS+Qt+C++ GDAL读取tif图像数据显示
    如何利用三极管实现电平转换
    云原生之快速使用Nacos Spring Cloud
    代码审查问题思考
    “第五十天” 机组--数据的表示
    LeetCode高频题:戈壁滩种树,一排n棵树,至少有k棵树存活时,最终形成的风景线有多少不同的情况
    PyQt5_寻找顶(底)背离并可视化
    块编辑器和双链笔记如何选择Notion vs Roam Research?FlowUs vs Obsidian
  • 原文地址:https://blog.csdn.net/qq_53221728/article/details/133310797