The difference between bio, nio, aio difference select, poll, epoll of

First understand some basic concepts, what is the socket? What is the I / O operation
  • unix (like) world, everything is a file, the file and what is it? File is just a bunch of binary stream, regardless of socket, or FIFO, pipelines, terminals, for us, everything is a file, everything flows

  • During the information exchange, we are those transmitting and receiving operation of data streams, referred to as I / O operations (input and output)

  • Computer, there are so many streams, how do I know which stream to operate it? Pair, where the file descriptors, known as fd, fd a is an integer, so that the operation of the integer, is to this file (stream). We create a socket, through the system call returns a file descriptor, then the rest of the operation of the socket will be converted to operate on this descriptor

Then take a look at several concepts
  1. BIO: synchronous blocking IO, a client connection, corresponds to a server thread

  2. BIO There is also a variant, the IO pseudo asynchronous, when there is access to a new client, the client will be packaged into a socket of the Task, processing thread thrown into the pool. Follow-up to optimize the way of processing threads

  3. NIO: synchronous non-blocking IO

  4. AIO: asynchronous non-blocking IO (must be non-blocking asynchronous)

Look at the following difference
  • For synchronous and asynchronous applications, the concern is the collaborative relationship among the program

  • Blocking and non-blocking is more concerned about the state of the implementation of a single process

Look at the process I / O processing
  • Data arrives kernel, ready data through the gateway

  • Write data to the cache buffer from the kernel

And then say something about it synchronous asynchronous clear
  • Synchronization: Whether BIO, NIO, or IO multiplexing, the user writes from kernel cache buffer must be read by a user thread data itself, the data processing

  • Asynchronous: Data is written to the kernel and user threads on a specified buffer, the thread notifies the user after the change is completed

  • Blocking: the kernel writes data from the gateway, if not written, the thread has been waiting for

  • Non-blocking: the total data gateway writes kernel thread with a poll to see if all the data is ready (I / O multiplexing, multiple listening socket)

Let's look at three forms I / O multiplexing
  • select: know that there are I / O event occurs, but do not know what that several streams (there may be one, more, or even all), we can only undifferentiated poll all streams, identify read data stream, or write data, to operate them. So having a select polling undifferentiated complexity O (n), the more the process stream at the same time, the longer time polling undifferentiated

  • poll: select and essentially no difference, the user would copy the array passed to the kernel space, and then query the status of each device corresponding to fd, but it does not limit the maximum number of connections, because it is based on a linked list to store

  • epoll (Linux kernel specific): can be understood as event poll, different from the busy polling and non-discriminatory poll, epoll which flow will occur how the I / O event let us know. So we actually say epoll event-driven (each event associated with fd), and at this time we are operating in these streams are meaningful. (Complexity is reduced to O (1)) (Epoll biggest advantages is that it just you "active" connection, but nothing to do with the total number of connections, so in a real network environment, the efficiency will be much higher than the select Epoll and poll)

  • Note: On the surface epoll best performance, but fewer in number of connections and connections are very active situation, select and poll performance may be better than epoll, epoll after the notification mechanism needs a lot of function callbacks

To achieve a final look back java core of NIO
  • Buffer Buffer

  1. It is actually a buffer array, encapsulating the data structure of read and write access, and maintenance information such as the location

  2. NIO in the library, all data are buffer-treated, when data is read, read directly into the buffer. When data is written directly into the write buffer. Data access at any time in the NIO, are operated by a buffer

  3. The most common buffer is ByteBuffer. Most Java basic type A buffer in all correspondence

  • Channel channel

  1. Channel is a channel through which data can be written and read. InputStream and OutputStream each operate only in one direction

  2. Channel is full duplex, so that the underlying map may be better than the flow api

  • Selector multiplexer

  1. NIO Selector is a program of the foundation. The multiplexer is ready to provide selected task capability

  2. Selector will continue to poll registers on the Channel, if the above channel has a new TCP connection, read or write events, this channel is ready, it will be polled Selector. You can then get ready by the set of Channel SelectionKey collections, IO operations carried out

  3. A plurality of Channel Selector can be simultaneously polled, since the use of the epoll JDK () instead of the traditional select achieved, there is no limitation of the maximum connection handle 1024/2048. This means that only one thread is responsible Selector poll, you can access thousands of clients

  • FIG NIO server sequence

     

  • NIO customer terminal sequence of FIG.

  • Simple version of the interactive map

Guess you like

Origin www.cnblogs.com/eryun/p/12040508.html