Linux System Programming---file I/o thought

Source: Internet
Author: User
Tags parent directory

1. How files are stored in Linux

Everything in Linux can be represented by a file, or through special files.

A file consists of a catalog item, an inode, and a data block.

The minimum storage unit for a hard disk is called "Sector" (Sector), with each sector storing 512 bytes (equivalent to 0.5KB).

When the operating system reads the hard disk, it does not read one sector at a time, so the efficiency is too low, but it reads multiple sectors at once, that is, one time to read a block. This "block", composed of multiple sectors, is the smallest unit of file access. "Block" size, the most common is 4KB, that is, eight consecutive sector to form a block.

The content data block of a file is stored in several "blocks".

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/74/52/wKiom1YY9XeRpYy0AAElAGXvN6g228.jpg "title=" 1.png " alt= "Wkiom1yy9xerpyy0aaelagxvn6g228.jpg"/>

Inode is a collection of metadata information about a file, including size, read and write permissions, file block location, UID, GUID, and so on.

The structure of the catalog file is very simple, which is a list of catalog items. Each directory entry consists of two parts: the file name of the included file, and the inode number that corresponds to the file name.

The file name and inode number are--correspondence.

The process of getting the contents of a file: file name + path---> file corresponding to the inode---> Location of the file, and other metadata---> operation.

2. Hard Links

In general, the file name and inode number are the "one by one correspondence" relationship, and each inode number corresponds to a file name. However, the Linux system allows multiple filenames to point to the same inode number. This means that the same content can be accessed with different file names, and changes to the contents of the file affect all file names, but deleting a file name does not affect access to another file name. This is referred to as a "hard link".

You can think of the file name as a pointer, and inode as the value pointed to by the pointer. That is, multiple pointers point to the inode.

3. Soft Connection

The inode number for file A and file B is different, but the content of file A is the path to file B. When you read file A, the system automatically directs the visitor to file B. Therefore, regardless of which file you open, the final read is file B. At this point, file A is referred to as the "soft link" of File B (soft link) or "Symbolic link" (symbolic).

Can be understood as a shortcut under Windows.

4.API

4.1. Open file, create file

int open (const char* name, int flags); int open (const char* name, int flags, mode_t mode);

Parameter description:

Name: The filename of the file, either an absolute path or a relative path.

Flags

/tr> TD width= "199" valign= "top" when the data is written to disk, the operation is completed
o_rdonly, O_wronly, O_rdwr read, write, read/write
o_append
o_nofollow
o_noblock Open in nonblocking mode, only for FIFO
o_sync
o_trunc< /span> If the file exists and is regular and readable, the file is truncated to 0
o_creat
o_excl

The Mode:mode parameter is useful when creating a file, and other situations are ignored. So when using o_create Yes, the mode parameter must be used.

When you create a file, the permissions for the file are not only determined by this parameter, but are also related to umask. File Permissions =mode&~umask

return value:

int file descriptor. Upon successful execution, 1 is returned and the errno is set to the appropriate error value

Btw

int open (name, O_wronly | O_creat | O_trunc, mode); = = = Int creat (name, mode);


This article is from the "Rotten Pen" blog, please make sure to keep this source http://cuiyiyi.blog.51cto.com/4080254/1702218

Linux System Programming---file I/o thought

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.