Asynchronous programming from the programmer's perspective

Until now, we have seen how I/O works from a hardware and software perspective. We mentioned that it is possible to have our process working while waiting for the I/O, but how do we do it?

The kernel has some things to help us with this. In the case of Linux, it has the epoll() system call, which lets the kernel know that our code wants to receive some information from an I/O interface, but that it doesn't need to lock itself until the information is available. The kernel will know what callback to run when the information is ready, and meanwhile, our program can do a lot of computations.

This is very useful, for example, if we are processing some data and we know that in the future we will need some information from a file. We can ask the kernel to get the information from the file while we continue the computation, and as soon as we need the information from the file, we won't need to wait for the file reading operation—the information will just be there. This reduces the disk read latency a lot, since it will be almost as fast as reading from the RAM instead of the disk.

We can use this approach for TCP/IP connections, serial connections, and, in general, anything that requires I/O access. This epoll() system call comes directly from the Linux C API, but in Rust we have great wrappers that make all of this much easier without overhead. Let's check them out.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.81.43