How are ordinary IO operations performed at the OS level? (Does not use zero copy)

How are ordinary IO operations performed at the OS level? (Does not use zero copy)

Insert image description here

File file = new File("xxx.txt");        
RandomAccessFile raf = new RandomAccessFile(file, "rw");         

byte[] arr = new byte[(int) file.length()];        
// read读取数据,用户态切换内核态
raf.read(arr);   

Socket socket = new ServerSocket(8081).accept();        
socket.getOutputStream().write(arr);
  • When using read to read data, the jvm process will switch from user mode to kernel mode, that is to say, it switches from the user perspective to the kernel perspective for execution. At this time, the data on the disk is copied to the kernel buffer based on the DMA engine. ;
  • Switch from kernel mode to user mode and copy the data in the kernel buffer to the user buffer based on the CPU.
  • The write method of the Socket output stream is called. At this time, it switches from the user state to the kernel state, and at the same time, the data in the user buffer is copied to the Socket buffer based on the CPU.
  • Then there will be an asynchronous process, based on the DMA engine copying the data from the Socket buffer to the network protocol engine and sending it out.
  • After everything is completed, switch from kernel mode back to user mode.

Therefore, from reading data from the local disk to sending it through the network, there are 4 switches between the user state and the kernel state. This is one; second, after the data is taken out from the disk, it has to go through a total of 4 times. Copy; therefore, these 4 switches and 4 copies make ordinary IO operations have lower performance.

PS: Study notes, architecture notes from Hushan

Guess you like

Origin blog.csdn.net/qq_31686241/article/details/125740927