How is event-based programming implemented?

I watched how twisted and node.js frameworks work, and I'm trying to understand how the operating system supports I / O using callbacks.

I understand this well because we need fewer threads, because we do not need to block threads waiting for I / O. But something should call a callback after I / O is complete.

How is this implemented by the operating system?

+6
asynchronous io networking nonblocking event-driven
source share
2 answers

One approach is to associate the OS with information about someone waiting for a callback to the corresponding data structure, for example, in the kernel, the equivalent of the file descriptor you are expecting to read about. When something happens to this file descriptor, the OS scans the waiters to see if they need to be notified. If so, then it is. You can read about one implementation of this in Lemon Paper that introduces the FreeBSD kqueue mechanism . See, In particular, section 6, “Implementation,” subsections 3 and 4, “Activity at the source of events” and “Delivery.”

+5
source share

This is solved in the OS using the "I / O event notification tools / interfaces", for example epoll , polling, kqueue or select.

Take a look at deft , and especially its io / event for a specific example of how the “notification systems” mentioned above are used. ( java.nio.channels.Selector is a java nio way to provide an abstraction for this.)

disclaimer: im a clever committer

+3
source share

All Articles