【Notes5】 IO


1. IO multiplexing select/poll/epoll: bF uses o

Hard disk and network card (IO). As follows A, B. . All are the client, the box is the server. First of all, I thought of coping with concurrency and writing a multi-threaded program. Every request that comes up is a thread. Now many rpc frameworks use this method. Multi-threading has drawbacks: cpu context switching, so multi-threading is not the best solution. , Switch back to single thread. The following while(1)...for...is single-threaded.
Insert picture description here
Below the dotted line above is the preparation of fds and max (how many bits max is used to card bitmap). The bitmap is 1024 bits, covering all the information in the fds. The above if (whether there is data in fdx) is judged by the program to judge whether it is the kernel in the select. The efficiency of the kernel judgment is higher than the user mode judgment, because the user mode judgment also asks the kernel. There is one 用户态和内核态切换. We judge that every time we need to use the user mode and Kernel mode switching.

When there is data, the kernel inserts a flag for the FD that has data. The FD in the FD setting refers to the corresponding bit in rset, not the element in the real fds. After the select returns (the last 5 lines below), it traverses the 5 fd in the fd set and judges 哪一个fd被set置位了. The data in the set is read out and puts (processing), which is the same as the program above to judge whether there is data in fd and then process the idea. . rset被内核set置位了需要每次while回来FD_ZERO赋空值,再FD_SET将fd赋到rset中.
Insert picture description here
systemIt is a C/C++ library function and selecta system call function. The following are the 4 shortcomings of select:
1. There is a limit to the number of fd that can be monitored by a single process (the size of the port that can be monitored is limited)
3. One needs to maintain a large amount of storage The data structure of fd makes the copy overhead of user space and kernel space large when transferring the structure.
4. When scanning the socket, it is a linear scan, that is, a polling method is used, which is low in efficiency.
Insert picture description here
As pollfds数组there are five pollfd结构体struct, poll all around pollfd improve the structure is deployed, the use of pollfds数组alternative bitmap (1024 far more than the size of the array).
Insert picture description here
Insert picture description here
During the execution of the epoll_wait function, epfd (whiteboard) is a shared memory and does not need to switch from user mode to kernel mode. There is no revevts flag bit set in epoll, it will be 有数据的fd(即触发了POLLIN事件)put to the top. epoll_wait is different from the previous select and poll, and has a return value. Finally, only nfds is traversed, no polling is required, and the time complexity is O(1). Epoll solves select 1, 2, 3, 4. Redis, nginx, javaNIO (linux) all use epoll. Multiplexing io takes advantage of the hardware advantage DMA.
Insert picture description here

2. Hard disk, network port, socket: inode

Insert picture description here
Insert picture description here
The upper part is the hard disk (block, sector, the content recorded by the inode in the file management), and the lower part is the network card.
Insert picture description here
Insert picture description here
In addition to tcp sockets (the client has a random port to connect to the server ip and designated port, the general server port is specifically designated for accessing Baidu webpage https default port 443), there are many unix underlying sockets, such as mysql local connection The 3306 of localhost does not actually use the tcp socket, but the mysql.sock file in the bottom mysql directory, and the file is directly connected through the unix bottom socket, so the database is the local database access speed faster .

3. IO related system call: strace

There is no magic in programming languages, all rely on the support of the operating system os, of which the most powerful support is system calls.
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
As shown above, java has more system calls than C language, because java has to start the jvm virtual machine, and jvm has to read the lib library of jdk and many other operations. The above did not find the open...xml operation, because the java program mainly starts the jvm process, and the jvm process may start a lot of threads to actually run the main function, so add -f.
Insert picture description here

4. BIO in Java: blocking, multithreading

Insert picture description here
Insert picture description here
Insert picture description here

package com.itheima.com;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
// 服务端两个socket,一个用于listen,一个用于connect
// 客户端一个socket用于connect  // bind,accept,read
public class qqserver {
    
    
    static byte[] bytes = new byte[1024];
    public static void main(String[] args) {
    
    
        try {
    
    
            ServerSocket serverSocket = new ServerSocket();
            serverSocket.bind(new InetSocketAddress(8080));
            while(true) {
    
      // 一直接受客户端
                System.out.println("wait conn");

//11111111111111111111111111111111111111111111111111111111111111111111111111111111111111
                Socket socket = serverSocket.accept(); //阻塞           
                // 不考虑多线程,BIO无法处理并发
                // 下面全部代码放thread1线程中,不影响上面主线程accept
                // 1000万线程里只有200万活跃,800万不活跃,线程上下文切换耗资源
                // 所以在服务端不活跃多,考虑单线程
                System.out.println("connect success");
                System.out.println("wait data");

//1111111111111111111111111111111111111111111111111111111111111111111111111111111111111                                   
                socket.getInputStream().read(bytes); //阻塞  read读了多少字节,如果第一个客户端不发消息,则一直停留在这
                System.out.println("data success");                
                String content = new String(bytes); //将bytes字节转为字符串
                System.out.println(content);
            }
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }
}
package com.itheima.com;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;

public class client {
    
    
    public static void main(String[] args) {
    
    
        try {
    
    
            Socket socket=new Socket("127.0.0.1",8080);
            Scanner scanner = new Scanner(System.in);
            String txt = scanner.next();
            socket.getOutputStream().write(txt.getBytes());
            // socket.getOutputStream().write("111".getBytes());
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }
}

Insert picture description here

5. NIO in Java: buffer is an array

Insert picture description here
I rarely write NIO myself, but I have been using NIO, because basic network frameworks such as netty and tomcat are all using NIO. Before 1.7, it was NIO, and after 1.7, it was AIO or NIO2. There are 8 types of buffers in java, such as int buffer, etc. channel就是一入口If we generate a channel from a file, it means that I want to read the file, read the contents of the file into the buffer, and then read the contents of the file after taking out the data from the buffer.
Insert picture description here
The following is 文件nio, the selector is not used :
Insert picture description here
The following is 网络nio, the selector is used: each channel is like a tcp client connection, 3 channels are registered in the selector when the entire socketserver is created, and the selector continuously scans the channels below. Once the channel has data It will be processed, like io multiplexing programming.
Insert picture description here
The above is NioTest1.java. Let's look at the system call and find that there are epoll_create and epoll_wait in j2.out.
Insert picture description here
Station B/Zhihu/WeChat Official Account: Code Farming Programming Record
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_43435675/article/details/109986346