The most common questions asked in the interview recently are the IO field. Some of them have been answered, while some of them have not answered well. Recently, I have begun to learn more about some of these things and want to summarize them.
Front point
1. User space system space
The Linux system will divide a process into two spaces, user space and system space. For example, our normal encoding is all user space. If we need to call system functions, such as copying system files, we need to call system components, obtain kernel services, and complete operations.
Two stages of IO
The IO execution process is divided into two stages, waiting for ready to execute copy
Waiting ready: We know that when we perform IO operations, the data may come from other applications or networks. If there is no data, the operating system will wait for it. At this time, the application may also wait for copying: copy the data to the application workspace
Blocking and non-blocking, synchronous and asynchronous
Let’s talk about synchronization and asynchronousness first. The easiest thing is to see whether a thread or process is started to complete the IO. When synchronizing IO, the system will stop and wait for this to finish before doing other things. Asynchronous IO uses multi-threading method to start a new thread to do this, and you can do other things and wait for notification.
Let’s talk about blocking and non-blocking. These two things actually focus on the state of the program when waiting for the call result. Blocking means that when you get this result, you will hang until you wait for the complete result. Non-blocking means that when the process cannot get the result, there is no blocking thread. This is a bit tangled. There is a common explanation. If it is blocking, I want to get this thing. During the acquisition process, I lose the CPU. I will not get the CPU until the result. However, if I am non-blocking, I will continue to hold the CPU, and I can still check it.
Several IO models
There are 5 types currently
Blocking IO
The most traditional IO type, that is, reading and writing will block
Non-blocking IO
When the user initiates the read, it will not lose the CPU, it will keep checking. If it is not successful, an error will be returned. If it receives a successful signal, it will initiate a read operation to obtain the complete result.
Multiplexing IO
This is the core of Java NIO. There will be a thread managing the status of multiple sockets to check whether you are ready. Only when you find that you are really ready will you call the CPU to perform IO operations. This is more important
Signal drive IO
Use less, ignore
Asynchronous IO
Get a thread to execute