OS file I/O

File I/O category:

  • Buffered/Unbuffered I/O
  • Direct/Indirect I/O
  • Blocking/non-blocking I/O
  • synchronous/asynchronous I/O

Buffered/Unbuffered I/O

  • Buffered I/O: Access files through the cache of the standard library, and the standard library accesses the file through system calls
  • Unbuffered I/O: Access files through system calls without going through the standard library cache

Cache purpose: reduce the number of system calls, reduce CPU context switching

Direct/Indirect I/O

  • Direct I/O : Access disk through file system
  • Indirect I/O: When reading, copy from the kernel cache to the user program; when writing, copy from the user program to the kernel cache, and the kernel decides when to write to the disk

One of the conditions for kernel cache to be written to disk:

  • When writing, when the kernel cache is too much, it will be written to the disk
  • sync, will be flushed to disk
  • Flash to disk when memory is low
  • When the kernel cache time > a certain time, flush to disk

blocking I/O

The read thread will block until the kernel prepares the data and copies the data from the kernel buffer to the application buffer before returning

  • Two points of blocking waiting: the kernel data is ready, and the data is copied from the kernel state to the user state

image.png

non-blocking I/O

The read request returns when the data is not ready, and polls the kernel to see if the data is ready. When the data is ready, wait for the kernel to copy the data to the application buffer before the read returns the result

  • Synchronization (waiting): data in the kernel state is copied to the buffer area of ​​the user program
  • Disadvantages: Polling consumes more CPU

image.png

select I/O multiplexing

I/O multiplexing: When the kernel data is ready, the application operation is notified through IO events

  • Advantages: threads can handle multiple socket IO requests

image.png

Blocking I/O, non-blocking I/O, and multiplexing based on non-blocking I/O are all synchronous calls

  • When reading, the kernel copies the data from the kernel space to the application space, all waiting

Extra step I/O

Asynchronous I/O: The kernel data is ready, and the data is copied from the kernel state to the user state without waiting

  • aio_read returns immediately, automatically copies the data from the kernel space to the application space, and notifies

image.png

Guess you like

Origin blog.csdn.net/qq_44226094/article/details/131710441