没有合适的资源?快使用搜索试试~ 我知道了~
本文档,是我本人翻译的一篇介绍linux系统同步异步阻塞非阻塞的知识的,你在网上看到的百分之八十的知识,可能都以讹传讹,让你看的一知半解,因为网上很多该类博客,没有讲解清楚,举得例子:如老王烧水,小王银行排队等,都是瞎扯。看完本文档,如果你还不明白,请私信我。或者给我发邮件:tccymm@hotmail.com 注:linux接口使用部分,我没有翻译,我觉得这部分应该自己做。文中很多括号部分的介绍,是我想让看的人更明白,从机器指令角度解释的。欢迎指点。
资源推荐
资源详情
资源评论
Boost application performance using asynchronous IO(1)(11-14-28)
Boost application performance using asynchronous I/O
使用异步I / O提升应用程序性能 (url: https://www.ibm.com/developerworks/linux/library/l-async/ )
Learn when and how to use the POSIX AIO API
了解何时以及如何使用POSIX AIO API
M. Tim Jones
Published on August 29, 2006
>Introduction to AIO
Linux asynchronous I/O is a relatively recent addition to the Linux kernel. It's a standard feature of the 2.6 kernel, but you can find patches for 2.4.
The basic idea behind AIO is to allow a process to initiate a number of I/O operations without having to block or wait for any to complete. At some
later time, or after being notified of I/O completion, the process can retrieve the results of the I/O.
Linux异步I/O是Linux内核的一个相对较新的内容。 这是2.6内核的一个标准特性,但是您可以找到2.4的补丁。 AIO背后的基本思想是允许
进程启动大量I/O操作,而不必阻塞或等待任何完成。 在稍后或者在被通知I/O完成之后,该过程可以检索I/O的结果。
>I/O models
Before digging into the AIO API, let's explore the different I/O models that are available under Linux. This isn't intended as an exhaustive review, but
rather aims to cover the most common models to illustrate their differences from asynchronous I/O. Figure 1 shows synchronous and asynchronous
models, as well as blocking and non-blocking models.
在深入研究AIO API之前,我们来探讨一下Linux下可用的不同I/O模型。 这不是一个详尽的评论,而是旨在涵盖最常见的模型,以说明它
们与异步I/O的区别。 图1显示了同步和异步模型,以及阻塞和非阻塞模型。
Figure 1. Simplified matrix of basic Linux I/O models
图1.基本Linux I / O模型的简化矩阵
*******
Each of these I/O models has usage patterns that are advantageous for particular applications. This section briefly explores each one.
这些I/O模型中的每一个都具有对特定应用有利的使用模式。 本节简要介绍每一个。
>Synchronous blocking I/O
I/O-bound versus CPU-bound processes
A process that is I/O bound is one that performs more I/O than processing. A CPU-bound process does more processing than I/O. The Linux 2.6
scheduler actually favors I/O-bound processes because they commonly initiate an I/O and then block, which means other work can be efficiently
interlaced between them.
I/O绑定与CPU绑定的进程
I/O绑定的进程是比处理执行更多I/O的进程。 CPU限制进程比I/O执行更多的处理。 Linux 2.6调度器实际上支持I/O绑定的进程,因为它们
通常启动一个I/O然后阻塞,这意味着其他工作可以在它们之间有效地交织。
One of the most common models is the synchronous blocking I/O model. In this model, the user-space application performs a system call that
results in the application blocking. This means that the application blocks until the system call is complete (data transferred or error). The calling
application is in a state where it consumes no CPU and simply awaits the response, so it is efficient from a processing perspective.
最常见的模型之一是同步阻塞I/O模型。 在此模型中,用户空间应用程序执行导致应用程序阻塞的系统调用。 这意味着应用程序会阻塞,
直到系统调用完成(数据传输或错误)。 调用应用程序处于不消耗CPU且仅等待响应的状态,因此从处理角度来看它是高效的。
Figure 2 illustrates the traditional blocking I/O model, which is also the most common model used in applications today. Its behaviors are well
understood, and its usage is efficient for typical applications. When the read system call is invoked, the application blocks and the context switches
to the kernel. The read is then initiated, and when the response returns (from the device from which you're reading), the data is moved to the user-
space buffer. Then the application is unblocked (and the read call returns).
图2说明了传统的阻塞I/O模型,它也是当今应用中最常用的模型。 它的行为很好理解,对于典型应用来说它的使用效率很高。 读取系统
调用时,应用程序块和上下文切换到内核。 然后启动读取,并且当响应返回时(来自您正在读取的设备),数据将移至用户空间缓冲区。
然后应用程序被解除阻塞(并且读取调用返回)。
Figure 2. Typical flow of the synchronous blocking I/O model
图2.同步阻塞I/O模型的典型流程
*******
From the application's perspective, the read call spans a long duration. But, in fact, the application is actually blocked while the read is multiplexed
with other work in the kernel.
从应用程序的角度来看,调用 read 的持续时间很长。 但是,实际上,应用程序实际上被阻塞,而读取与内核中的其他工作复用。
>Synchronous non-blocking I/O
A less efficient variant of synchronous blocking is synchronous non-blocking I/O. In this model, a device is opened as non-blocking. This means
that instead of completing an I/O immediately, a read may return an error code indicating that the command could not be immediately satisfied
(EAGAIN or EWOULDBLOCK), as shown in Figure 3.
同步阻塞的低效率变体是同步非阻塞I/O。 在这个模型中,设备被打开为非阻塞状态。 这意味着read可能不会立即完成一个I/O,而是会返
回一个错误代码,指示该命令不能立即满足(EAGAIN或EWOULDBLOCK),如图3所示。
Figure 3. Typical flow of the synchronous non-blocking I/O model
图3.同步非阻塞I/O模型的典型流程
*******
The implication of non-blocking is that an I/O command may not be satisfied immediately, requiring that the application make numerous calls to
await completion. This can be extremely inefficient because in many cases the application must busy-wait until the data is available or attempt to do
other work while the command is performed in the kernel. As also shown in Figure 3, this method can introduce latency in the I/O because any gap
between the data becoming available in the kernel and the user calling read to return it can reduce the overall data throughput.
非阻塞的含义是I/O命令可能不会立即得到满足,要求应用程序进行多次调用以等待完成。 这可能非常低效,因为在很多情况下,应用程
序必须忙于等待数据可用或在内核中执行命令时尝试执行其他工作。 如图3所示,这种方法可能会在I/O中引入延迟,因为内核中可用数据
与用户调用read 以返回数据之间的任何间隙都会降低总体数据吞吐量。
>Asynchronous blocking I/O
Another blocking paradigm is non-blocking I/O with blocking notifications. In this model, non-blocking I/O is configured, and then the blocking
select system call is used to determine when there's any activity for an I/O descriptor. What makes the select call interesting is that it can be used to
provide notification for not just one descriptor, but many. For each descriptor, you can request notification of the descriptor's ability to write data,
availability of read data, and also whether an error has occurred.
另一个阻塞模式是阻塞通知的非阻塞I/O。 在此模型中,配置了非阻塞I/O,然后使用阻塞选择系统调用来确定何时有I/O描述符的活动。
使select(系统调用,如select, poll, epoll(linux) )调用感兴趣的是,它可以用来为不仅一个描述符提供通知,而且可以提供很多通知。 对于每
个描述符,可以请求通知描述符写入数据的能力,读取数据的可用性以及是否发生错误。
Figure 4. Typical flow of the asynchronous blocking I/O model (select)
图4.异步阻塞I/O模型的典型流程(以 select 调用为例)
*******
The primary issue with the select call is that it's not very efficient. While it's a convenient model for asynchronous notification, its use for high-
performance I/O is not advised.
select调用的主要问题是效率不高。 虽然它是异步通知的便捷模式,但不建议将其用于高性能I/O。
剩余11页未读,继续阅读
资源评论
tccymm_1
- 粉丝: 6
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功