事件驱动编程是如何实现的?
我正在研究twisted 和node.js 框架是如何工作的,我是 尝试准确理解操作系统如何支持 I/O 使用回调的操作。
我知道这很好,因为我们需要更少的线程,因为我们不需要 需要阻塞线程等待 I/O 操作。但有些东西 I/O 完成后必须调用回调。
操作系统是如何实现这一点的呢?
I was looking on how the twisted and node.js frameworks work and I am
trying to understand exactly how the operating system supports I/O
operations using callbacks.
I understand it's good because we need less threads because we don't
need to have blocked threads waiting for I/O operations. But something
has to call the callback once the I/O is finished.
How is this implemented by the operating system?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种方法是让操作系统将任何等待回调的人的信息附加到相关数据结构,例如您正在等待读取通知的文件描述符的内核等效项。当该文件描述符发生问题时,操作系统会扫描服务员以查看是否有任何应通知的。如果他们应该这样做,那么它就会这样做。您可以在 Lemon 介绍 FreeBSD 的
kqueue 的论文
机制。特别参见第 6 节“实施”、第 3 和 4 小节“事件源上的活动”和“传递”。One approach is to have the OS attach information about anyone waiting for a callback to the relevant data structure, such as the in-kernel equivalent of the file descriptor you're waiting for read notification about. When something happens to that file descriptor, the OS scans the waiters to see if any should be notified. If they should, then it does so. You can read about one implementation of this in Lemon's paper introducing FreeBSD's
kqueue
mechanism. See in particular section 6, "Implementation", subsections 3 and 4, "Activity on Event Source" and "Delivery".这个问题在操作系统中通过使用“I/O事件通知设施/接口”来解决,例如epoll、投票、kqueue 或选择。
看看deft,尤其是它的io/event loop 有关如何使用上述“通知系统”的具体示例。 (java.nio.channels.Selector 是为此提供抽象的 java nio 方式。)
免责声明:我是一个灵活的提交者
This is solved in OS by using "I/O event notification facilities/interfaces", e.g epoll, poll, kqueue or select.
Take a look at deft, and especially its' io/event loop for a concrete example how the "notification systems" mentioned above are used. (java.nio.channels.Selector is the java nio way to provide an abstraction for this.)
disclaimer: im a deft committer