Five kinds of I / O model

Please look at: synchronous, asynchronous, blocking, non-blocking

Blocking I / O

Application to initiate a process I / O requests go through two states

  • Waiting to enter the kernel space data
  • Copy data from kernel space to user space
    in two phases user process is blocked, first of all, waiting for the data ready this time, the user space do not do anything, just wait for the data ready, when the data is ready, users copy data phase the process is still in the blocked state, which is blocking I / O model
    Here Insert Picture Description
// 服务端代码
//创建套接字,绑定并监听指定端口
ServerSocket serverSocket = new ServerSocket(20000);
//请求未到达之时,线程阻塞于此
client = serverSocket.accept();
// 客户端连接成功,输出提示
System.out.println("客户端连接成功");
// 启动一个新的线程处理客户端请求
new Thread(new ServerThread(client)).start();

// 子线程中处理客户端的输入
class ServerThread implements Runnable {
    .....
    @Override
    public void run() {
        boolean flag = true;
        while (flag) {
            // 读取客户端发送来的数据
            String str = buf.readLine();
            // 回复给客户端 get 表示收到数据
            out.println("get"); 
        }
    }
}
//客户端代码
Socket client = new Socket("127.0.0.1", 20000);
boolean flag = true;
while (flag) {
    // 读取用户从键盘的输入
    String str = input.readLine();
    // 把用户的输入发送给服务端
    out.println(str);
    // 接受到服务端回传的 get 字符串
    String echo = buf.readLine();
    System.out.println(echo);
    }
}

Seen from the code, whenever a client requests a connection, the server will open a thread for this mode the server is a lot of pressure.
When calling readline will be blocked when readline () when the data is not read, would have been blocked, the server in order to execute multiple threads at the same time, you have to create a large number of threads.

Non-blocking I / O

Blocking I / O is an improvement over non-blocking I / O when waiting for data to enter the kernel space user process can not block, but returns a 0 to the user process, user thread can shop and go to some other things, in the dry other things in the process, the user process will continue to send the same request, until the data arrives.
Copy data: copy data to the user space will still be blocked.
Here Insert Picture Description

I / O multiplexer

Non-blocking I / O data Although there is no obstruction, but it has been kept polling to see if the data is ready, will still spend a lot of time off the cpu.
Since whether the client will not because the data is not ready blocked, then we have no need to assign each client a thread to polling data to determine readable, you can choose a thread to listen to all clients, and then From this thread a round robin to determine whether the data is ready, if the data is readable, notifies the other threads. This approach is I / O multiplexing.
Here Insert Picture Description

Signal-driven I / O

Signal-driven IO refers to the process in advance to inform the kernel, when an event is sent on a descriptor, the kernel uses signals to inform the relevant processes. IO and signal-driven asynchronous not true, because after the notification to the process, the process is still done by the IO operation.
Here Insert Picture Description

异步 I / O

In front of the four forms in general are synchronous I / O, data in the copy process, the user process is always blocked.
Asynchronous I / O request is initiated by the user, regardless of whether the data is ready to return to the user, the user can shop and go thing, when data is ready, the kernel directly copy the data to the user, and then notify the user click on OK. In this mode, waiting for a copy of the data and the data are not blocked.
Here Insert Picture Description

Reference blog
Road Netty learning framework (a) - Java network IO model
five kinds IO model (Image + Detailed examples)

Guess you like

Origin blog.csdn.net/Alyson_jm/article/details/84579352