The underlying technology supporting Java NIO and NodeJS
As we all know, some support for Java NIO and NIO2 has been added to recent versions of Java. At the same time, one of the most praised advantages of the NodeJS technology stack is its high-performance IO. So the topic we are going to discuss today is the underlying technologies that support these technologies.
One question to ask before starting is:
Why didn't NodeJS and Java NIO2 appear earlier?
Answer: I personally think that the underlying support technology is not yet mature.
So, what does the underlying technology mean? Yes, I think many people have guessed that it is operating system technology. The two concepts proposed in this article, Java NIO2 and NodeJS, are all user-state technologies or application-layer technologies, and these application-layer technologies run on OS. At the same time, with the advancement of the operating system, the programming models that can be supported are also richer. It can be said that these two technologies are completely evolved to apply the dividends brought by operating system progress. Generally speaking, the technology that first enjoys this bonus must be C/C++, because most of the latest advancements in OS provide system calls, and C/C++ is the most convenient to apply these system calls, but it is also the most complex. In order to obtain the same performance, other platforms must constantly evolve and package so that users can use these dividends. Once that platform stagnates and updates, it is time for this platform to decline. The more convenient it is to encapsulate, the more friendly it is to the user, and the more people it may use it. Although many people can quickly write code based on these platforms, they often do not understand the essence because they are essentially not understanding the motivations and principles of these technologies. The technologies we are discussing below are the underlying technologies related to these two technologies.
No matter which OS design, the following 5 IO models are essential.
1. Blocking I/O
2. nonblocking I/O
3. I/O multiplexing (select, poll and epoll)
4. signal drive I/O (SIGIO)
5. asynchronous I/O (the POSIX aio_ functions)
1. Blocking I/O
As shown in the figure, the advantage of this IO model is that it is simple to program and is also one of the earliest IO models supported by OS. The disadvantage is that system calls block users' dynamic thread execution, resulting in wasted CPU time and low IO efficiency.
2. nonblocking I/O
As shown in the figure, an improvement of this IO model is that IO is non-blocking, but it requires long polling, which also wastes CPU clock cycles.
3. I/O multiplexing (select, poll and epoll)
As shown in the figure, this IO model is the most stable IO model provided by OS today. Most mainstream applications are built on this IO model, such as NodeJS. However, these platforms often add a layer of encapsulation to this model to directly support AIO.
4. signal drive I/O (SIGIO)
As shown in the figure, the data records that this IO model has no performance advantages in comparison model 3 and is rarely used by designers due to instability in the system support.
5. asynchronous I/O (the POSIX aio_ functions)
As shown in the figure, this IO model is the most perfect AIO, and the programming model is also the simplest, but there are very few OSes that can perfectly support the model. Online information shows that Linux is making efforts in this regard. Once the OS makes progress in this aspect, the programming framework, platform, and programming model may still need to be greatly simplified.
Although this model is rarely supported by OS, it does not mean that there is no such AIO model now. Many frameworks have done this aspect and simulated AIO in the user mode, allowing users to pay more attention to business logic code.
6. Synchronous asynchronous, blocking and non-blocking
Synchronous and asynchronous are for the interaction between the application and the kernel. The data is read and then the return is synchronous, and the return is asynchronous. Blocking and non-blocking are for processes and threads. The read or write thread is waiting in blocking mode, rather than blocking mode, the read or write thread immediately returns a status value.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.