The last two sections covered the select and poll functions. Check the man manual:
SELECT(2) Linux Programmer's Manual SELECT(2)
NAME
select, pselect, FD_CLR, FD_ISSET, FD_SET, FD_ZERO - synchronous I/O multiplexing
Copy the code
POLL(2) Linux Programmer's Manual POLL(2)
NAME
poll, ppoll - wait for some event on a file descriptor
SYNOPSIS
#include <poll.h>
Copy the code
Synchronous I/O multiplexing, so SELECT is a synchronous I/O multiplexing mode. Unix has five I/O models:
- Blocking I/O
- Non-blocking I/O
- I/O multiplexing
- Signal drive
- Asynchronous I/O
Signal-driven and truly asynchronous I/O are not commonly used, so let’s focus on the first three.
The concepts of blocking and non-blocking describe how user threads invoke kernel I/O operations: blocking means that the I/O operation is not returned to user space until it has completely completed; Non-blocking means that the STATUS value is returned to the user immediately after the IO operation is invoked, without waiting for the IO operation to complete
Blocking I/O
ServerSocket server = new ServerSocket(8080);
while (true) {
Socket socket = server.accept();
System.out.println("Link port:" + socket.getPort());
InputStream inputStream = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String str = null;
while((str = reader.readLine()) ! =null) {
System.out.println("Accept:" + str);
socket.getOutputStream().write("ok\n".getBytes());
socket.getOutputStream().flush();
if ("over".equals(str)) {
System.out.println("It's going to close.");
socket.close();
break;
}
}
System.out.println("= = = = = = = = = = =");
}
Copy the code
When data is retrieved, the user thread releases the CPU until the data is processed by the kernel, which blocks the user thread.
The user thread calls the kernel IO operation and waits for the IO to complete before returning to user space, thus blocking the IO
Non-blocking I/O
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.bind(new InetSocketAddress(8888));
serverSocketChannel.configureBlocking(false);
while (true) {
SocketChannel socketChannel = serverSocketChannel.accept();
if (socketChannel == null) {
System.out.println("No link");
continue;
}
System.out.printf("New link, port is %s", ((InetSocketAddress) socketChannel.
getRemoteAddress()).getPort());
ByteBuffer ds = ByteBuffer.allocate(10);
socketChannel.read(ds);
System.out.println("Accept data");
}
Copy the code
The IO operation returns a status value to the user immediately after it is invoked, without waiting for the IO operation to complete. Therefore, it is non-blocking. Instead of waiting for the I/O to arrive, the user thread needs to keep asking the kernel if there is any data available. Get the data when it’s ready.
Add a GIF (refresh to view) :
I/O multiplexing
Non-blocking IO requires user threads to poll the IO state on each IO path to determine whether there is any data to process. If the readable and writable events of a connection are separated, use a separate thread to manage them. Multiple I/O channels use this manager to manage socket state. This is called I/O multiplexing.
Multiplexing in the kernel provides select, poll, epoll three ways:
select
Principle of signal
The characteristics of
Only a limited number of sockets (different system parameters: 1024/2048) can be processed
Select cannot accurately tell the user which socket has available data, so polling is required
poll
Principle of signal
The characteristics of
It’s the same as select
Using linked list implementation, cancel the limit of the number of files
epoll
Principle of signal
Epoll_wait checks if the list is empty to see if a file descriptor is ready
When an event on a FD occurs, its corresponding callback function is called to add the FD to the list, while other events in the “idle” state are not added
Epoll retrieves the FD with the event from the above list
Epoll is an I/O event notifier:
EPOLL(7) Linux Programmer's Manual EPOLL(7)
NAME
epoll - I/O event notification facility
SYNOPSIS
#include <sys/epoll.h>
Copy the code
The characteristics of
There is no maximum connection limit
The user can be directly told which application and which connection has data
Epoll detail can refer to my album mp.weixin.qq.com/mp/homepage… Redis source Code series
The concept of asynchrony
The concepts of synchronous and asynchronous describe how user threads interact with the kernel:
Synchronization means that the user thread initiates an I/O request and waits for or polls the kernel to complete the I/O operation before continuing. Asynchronous means that the user thread continues to execute the I/O request. When the kernel completes the I/O operation, it notifies the user thread or invokes the callback function registered by the user thread.
So blocking I/O, non-blocking I/O, AND I/O multiplexing are synchronous calls. Only AIO that implements a special API is called asynchronously, which will be explained in a separate article.
AIO(7) Linux Programmer's Manual AIO(7)
NAME
aio - POSIX asynchronous I/O overview
DESCRIPTION
The POSIX asynchronous I/O (AIO) interface allows applications to initiate one or more I/O operations that are performed asyn‐
chronously (i.e., in the background). The application can elect to be notified of completion of the I/O operation in a variety of
ways: by delivery of a signal, by instantiation of a thread, or no notification at all.
Copy the code
A series of
NIO: Linux/IO fundamentals
NIO sees and says (2) – The two BIO in Java
NIO sees and says (3) — different IO models
NIO: Java NIO
NIO also said that (v) : Do it, do it today, understand Buffer
Pay attention to my
If you are reading on wechat, please click the link to follow me. If you are reading on PC, please scan the code to follow me. Welcome to communicate with me and point out mistakes at any time.
Copyright notice: This article is originally published by xiaoyan. Please contact the author for republication. It is published on InfoQ and public account.