A brief introduction to Io-one under Linux

Source: Internet
Author: User

What is file IO

IO is input/output, input/output. File IO means to read and write files.

Main interface API for file operations

What is the operating system API

(1) APIs are functions that are supported by Linux systems and used by application-level programs.

(2) The Application layer program calls the API to invoke various functions in the operating system to work.

(3) Learning an operating system is actually learning to use the API of this operating system. Using a Linux system to read and write files is a way to learn about Linux system APIs and file IO.

Linux common file IO interface (1) Open, close, write, read, Lseek

General steps for file operations

(1) in the Linux system to operate a file, usually open a file, get a file descriptor, and then read and write to the file (or other operations), and finally close the file can be

(2) Emphasize one point: when we operate the file, we must first open the file, open the success before going to the operation (if the opening itself fails, the latter will not operate); After the final reading and writing is finished, you must close the file, otherwise it may cause file corruption.

(3) The file is usually present in the file system of the block device, we call this file static file. When we go to open a file, the Linux kernel does the following: The kernel creates an open file data structure in the process, records the file we open, the kernel requests a memory in memory, and the contents of the static file are read from the block device to the memory specific address management (called the dynamic file).

(4) After opening the file, the subsequent read and write operation of this file is for the memory of this dynamic file, but not for static files. When we read and write to the dynamic file, the dynamic files in memory and the static files in the block device are out of sync, and when we close the dynamic file, the close internal kernel updates the contents of the dynamic file in memory to update (synchronize) the static files in the block device.

(5) Some common phenomena: the first: When opening a large file is slower the second: we write half of the file, if there is no point to save the direct shutdown/power down, the file content is lost after reboot.

(6) Why do you design this? Think that the block device itself has read and write restrictions (memory Nnadflash, SD and other block device read and write characteristics), the block device itself is very inflexible operation. The memory can be manipulated in bytes, and can be manipulated randomly (memory is called ram,random), very flexible. So the kernel design file is designed to work.

Important Concepts: File descriptors

(1) The file descriptor is actually a number, this number in a process to express a specific meaning, when we open a file, the operating system in memory to build some data structures to represent the dynamic file, and then return to the application a number as a file descriptor, This number is tied to the data structures in our in-memory maintenance of this dynamic file, which we need to differentiate with this file descriptor later if we want to manipulate this dynamic file.

(2) A word about a file descriptor: A file descriptor is used to distinguish between multiple files opened by a program.

(3) The scope of the file descriptor is the current process, out of the current process this file descriptor is meaningless.

A brief introduction to Io-one under Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.