非阻塞 IO 是如何实现的? [英] How is Non-Blocking IO implemented?

查看:89
本文介绍了非阻塞 IO 是如何实现的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Java 或 C# 或其他一些语言中,有非阻塞 IO 设施,例如用于套接字.

In Java or C# or some other languages, there are non-blocking IO facilities, e.g., for sockets.

所以我可以将我的回调函数提供给非阻塞 IO,一旦非阻塞 IO 接收到任何东西,它就会调用我的回调.

So I can give my callback functions to the non-blocking IO and once the non-blocking IO receives anything, it will call my callbacks.

我想知道它们是如何实现的.如果我在幕后创建非阻塞 IO,Java 或 C# 是否只是为它们创建后台线程?或者底层操作系统对它们有原生支持?

I am wondering how they are implemented. If I create non-blocking IO, behind the scene, does Java or C# just create background threads for them? or the OS underlying has native support for them?

推荐答案

在 Windows 上,底层操作系统支持非阻塞 I/O,Microsoft 的 CLR 就利用了这一点.其他 CLR 实现(单声道)可能也是如此,但我不确定.在 Microsoft CLR 上执行异步 I/O 时,挂起的异步 I/O 操作与等待这些 I/O 操作完成的线程(或至少是托管线程)之间没有一对一的相关性.

On Windows there is underlying OS support for non-blocking I/O, and Microsoft's CLR takes advantage of that. Other CLR implementations (mono) probably do as well, but I don't know for sure. When performing async I/O on the Microsoft CLR there is not a 1-to-1 correlation between pending async I/O operations and threads (or at least managed threads) waiting for those I/O operations to complete.

参见 http://msdn.microsoft.com/en-us/library/windows/desktop/aa365683(v=vs.85).aspx 有关 Win32 层详细信息的一些详细信息.还有关于 I/O 完成端口的信息:http:///msdn.microsoft.com/en-us/library/aa365198(VS.85).aspx

See http://msdn.microsoft.com/en-us/library/windows/desktop/aa365683(v=vs.85).aspx for some details on the Win32-layer details. Also the information on I/O completion ports here: http://msdn.microsoft.com/en-us/library/aa365198(VS.85).aspx

我的理解是:

  1. 我在某个应用线程上开始异步 I/O 操作.
  2. 如果还没有,则会创建一个队列(嗯,实际上是一个称为 I/O 完成端口的内核级构造,它与我的应用程序内核空间中的队列相关联).在 .NET 世界中,一个称为 I/O 完成端口线程的特殊指定线程将开始等待该队列上的 I/O 完成通知.这里需要注意的重要一点是,我可以发出任意数量的异步 I/O 请求,而无需增加 I/O 完成端口的数量.
  3. 操作系统将在 I/O 完成时通过将 I/O 完成消息排入队列来通知应用程序.I/O 完成端口线程随后将通过调用我的 .NET 应用程序中的 I/O 完成回调来处理该消息.同时,如果其他 I/O 完成,其结果将排在当前正在处理的结果之后.

以上注意事项:

  1. 我确定我弄错了一部分,但我相信它的整体要点是正确的.埃里克或其他人可以进来纠正我的错误.

  1. I am sure I got part of this wrong, but I believe the overall gist of it is correct. Eric or someone can come in and correct me where I'm off.

在 .NET 中有多个 I/O 完成端口线程.我不知道异步 I/O 请求是如何在各种 I/O 完成端口之间分配的.这可能是操作系统功能(其中 I/O 可能会在应用程序打开的任何端口上返回).

In .NET there are multiple I/O completion port threads. I have no idea how async I/O requests are allocated amongst the various I/O completion ports. This may be an operating system feature (wherein I/O may come back on any port that the application has open).

对于 Java,我确信它取决于 JVM 实现和特定的操作系统.我对它的了解还不够深入,无法对此进行推测.

For Java I am sure it depends on the JVM implementation and the particular OS. I don't know it nearly well enough to speculate beyond that.

历史更新,更多细节这里

这篇关于非阻塞 IO 是如何实现的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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