aio_write()和O_NONBLOCK write()之间的区别 [英] Difference between aio_write() and O_NONBLOCK write()

查看:449
本文介绍了aio_write()和O_NONBLOCK write()之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

aio_write()和O_NONBLOCK write()之间有什么区别?
此外,我使用O_NONBLOCK函数使用文件描述符对文本文件使用write(),并通过在函数之前和之后放置一个计时器来比较aio_write()的性能。

What is the difference between aio_write() and O_NONBLOCK write()? Also, I use write() to a text file using file descriptor using the O_NONBLOCK function and compare the performance with the aio_write() by putting a timer before and after the function.

当字符串长度增加但是aio_write()仍然保持大约相同的时间时,write()函数需要更长的时间写入文件。

It seems that the write() function would take longer time to write to file when the length of the string increases but aio_write() still maintain around the same time.

为什么会这样? NONBLOCK和异步之间有什么区别?

Why is it so? What is the difference between NONBLOCK and asynchronous?

感谢

推荐答案

使用O_NONBLOCK write(),write()调用将接受(即复制到内核中的缓冲区)所有,一些或没有传递给它的数据(如果一些字节被接受,write() s返回值将指示它接受了多少字节...如果没有接受,write()将返回-1,并且errno将被设置为EWOULDBLOCK)。 write()接受的字节数将取决于目前在其内核缓冲区中可用的空间大小。 write()返回后,你有责任记住它接受的字节数,然后调用select()(或者poll()或者其他机制),这样当缓冲区中有更多可用空间时,你会被通知。当更多的空间可用时(即在将来的某个时候),你可以再次调用write()来传递更多的字节到缓冲区。

With the O_NONBLOCK write(), the write() call will accept (that is, copy to an in-kernel buffer) all, some, or none of the data you passed to it (if some bytes were accepted, write()'s return value will indicate how many bytes it accepted... if none were accepted, write() will return -1 and errno will be set to EWOULDBLOCK). The number of bytes that write() accepts will depend on how much space it has available in its in-kernel buffer at the moment. After write() returns, it's your responsibility to remember how many of your bytes it accepted, and then call select() (or poll() or some other mechanism) so that you will be notified when there is more space available in the buffer. When more space becomes available (i.e. at some time in the future) you can then call write() again to pass more bytes to the buffer.

aio_write另一方面,将获取所有权的数据传递到函数,并在其后完成写出数据通知您。使用aio_write(),您不必担心调用仅接受部分数据缓冲区;它将接受整个事情,或错误。这将使你的应用程序的I / O逻辑在这方面更简单;然而我认为异步I / O有自己的一套复杂因素,所以它可能不总是一个胜利。 (我没有使用aio _ *()自己,所以我不能给细节)

aio_write(), on the other hand, will "take ownership" of the data you pass in to the function, and notify you later on when it has finished writing out the data. With aio_write(), you don't have to worry about the call accepting only part of your data buffer; it will either accept the entire thing, or error out. That will make your app's I/O logic a bit simpler in that respect; however I think asynchronous i/o has its own set of complicating factors so it might not always be a win. (I haven't used aio_*() myself, so I can't give details)

至于为什么write()函数似乎没有采取更多的时间作为数据写入的长度增加...这是因为非阻塞write()只复制(无,或一些或所有)传递到缓冲区的数据,然后立即返回;它实际上并不等待数据进入磁盘。将一个(相对较小的)字节序列从应用程序的缓冲区复制到内核缓冲区将总是很快,复制的字节数将永远不会大于内核缓冲区中当前可用的空空间大小,因此即使每个write()复制的字节数有界/小。

As for why the write() function doesn't seem to be taking more time as the length of data written increases... that's because a non-blocking write() only copies (none, or some, or all) of the data you pass to it into a buffer, and then returns immediately; it doesn't actually wait for the data to get onto the disk. Copying a (relatively small) sequence of bytes from your app's buffer to an in-kernel buffer will always be fast, and the number of bytes copied will never be greater than the amount of empty space currently available in the in-kernel buffer, so even the number of bytes copied per write() is bounded/small.

这篇关于aio_write()和O_NONBLOCK write()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆