Getting the kernel to control the I/O

When a new TCP/IP connection gets established, or when a new key is pressed on the keyboard, the kernel must know about it so that it can act accordingly. There are two ways of doing this—the kernel could be looking to those ports or memory addresses once and again to look for changes, which would make the CPU work for nothing most of the time, or the kernel could be notified by a CPU interrupt.

As you can imagine, most kernels decide to go for the second option. They are idle, letting other processes use the CPU until there's a change in some I/O port or address. This makes the CPU interrupt at a hardware level, and gives control to the kernel. The kernel will check what happened and decide accordingly. If there is some process waiting for that interrupt, it will wake that process and let it know that there is some new information for it.

It may be, though, that the process waiting for the information is already awake. This happens in asynchronous programming. The process will keep on performing some computations while it's still waiting for the I/O transaction. In this case, the process will have registered some callback function in the kernel, so that the kernel knows what to call once the I/O operation is ready.

This means that while the I/O operation was being performed, the process was doing useful things, instead of being blocked and waiting for the kernel to return the result of the I/O operation. This enables you to use the CPU almost all the time, without pausing the execution, making your code perform better.

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

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