IO model and epoll principle

Traditional BIO is blocking IO, here is a picture:

insert image description here
One BIO thread handles one connection. If the connection is established without sending data, it will always be blocked. Even if multiple threads process multiple services, they are blocked. There are two blocking points: waiting for the connection. Waiting for input.
Thread Thread is a lightweight process of the operating system. Every connection in the BIO creates a thread corresponding to it. How does it work?
Every time an operating system is connected, the clone method of the kernel will be called to copy the connected thread, and the thread will have a corresponding thread ID number to establish monitoring. With the corresponding log, the newly opened thread can recv (read client information) , and then the main thread continues to wait for the connection, and so on. The main thread loop continues to block waiting for connections. As the number of client connections increases, there will be more threads in the clone. The transformation of NIO is to reduce the development of threads, but the kernel must be transformed.
insert image description here

NIO:

newIO in Java, nonblockIO in OS.
insert image description here

The problems of select and poll of the multiplexer:
1. The kernel must transmit a large amount of data [client number] every time the system calls (the kernel is stateless and does not record the context information of each call)

2. Every time the program calls the kernel , a new round of traversal will be triggered , and then the client state will be returned to the kernel, which is also synchronized here.
How to solve it?
The kernel opens up space, saves each client information, and asynchronously marks the client data status.
EPOLL solves the above problems, here is a picture
insert image description here
BIO NIO multiplexers are all synchronous IO models
Synchronization: the program reads data by itself (synchronous and asynchronous, synchronous needs to read data by itself, asynchronous data is pushed by itself)
——— ———————
system call:

系统调用和中断机制(程序的交替执行),中断有以下几种情况:
	情况1:系统自己调度实现中断
		网卡晶振(一直断点,每个断点为切换执行的过程),大概流程:
			>晶振后CPU将终止当前执行的程序,将CPU程序计数器(执行位置)存到程序的程序计数器中,以便于下次顺序执行
			>触发CPU进程调度,利用算法找出即将执行的程序
			>找中断向量表(键值,键代表某个程序,值为回调函数【将程序中要执行的区域搬到内存】)
			>将待执行程序的程序计数器加载到CPU自己的寄存器中,每执行一步从CPU程序计数器获取下一条指令继续执行
			>等待晶振打断
	情况2:外部设备实现中断
		如晃动鼠标,敲击键盘等

The interrupt concept of the CPU is very important. This is the basis for multi-program operation. If there is no interrupt, it is equivalent to only one program in the computer.
For example :
Uninterrupted : In a classroom, teachers and students are one-to-one, which is an uninterrupted program, which means that the teacher manages this child uninterrupted. Interrupted: In
a classroom, teachers and students are one-to-many, and teachers Switch management between multiple students, no matter how you switch, each execution is one-to-one, but all students are managed by the teacher callback
function : when a certain condition is triggered, the student makes a mistake, the teacher comes A slap, a slap is a callback function, and the student's fault is the trigger condition

IO involves hardware, from user mode to kernel mode.
System calls and function calls (kernel state switching is an interrupted process)
system calls: go through the kernel state, cpu switching
function calls: CPU does not switch execution, jump from A function to B function execution

Guess you like

Origin blog.csdn.net/GiantCrocodile/article/details/117364619