Link, unlink, close, and fclose in Linux

Source: Internet
Author: User
Tags touch command

Each file can obtain the file information through a struct stat struct. One of the member st_nlink represents the number of links to the file.

Struct stat {unsigned longst_dev;/* device. */unsigned longst_ino;/* file serial number. */unsigned intst_mode;/* file mode. */unsigned intst_nlink;/* link count. */unsigned intst_uid;/* User ID of the file's owner. */unsigned intst_gid;/* Group ID of the file's group. */unsigned longst_rdev;/* Device number, if device. */unsigned long _ pad1; longst_size;/* size of file, in bytes. */intst_blksize;/* Optimal Block Size for I/O. */INT _ pad2; longst_blocks;/* Number 512-byte blocks allocated. */longst_atime;/* time of last access. */unsigned longst_atime_nsec; longst_mtime;/* time of last modification. */unsigned longst_mtime_nsec; longst_ctime;/* time of last status change. */unsigned longst_ctime_nsec; unsigned int _ unused4; unsigned int _ unused5 ;};

When you run the shell touch command orProgramWhen you open a non-existent file with o_creat, the number of links to the file is 1.

Generally, opening an existing file does not affect the number of links of the file. The function of open is only to establish an access relationship between the calling process and the file, that is, the FD is returned after open. The Calling process can read, write, ftruncate, and perform a series of file operations through FD.
Close () is to eliminate the access relationship between the calling process and the file. Naturally, the number of links to the file is not affected. When close is called, the kernel checks the number of processes that open the file. If this number is 0, the kernel further checks the number of links to the file. If this number is also 0, the file content will be deleted.

The link function creates a new directory item and adds a number of links.
The unlink function deletes directory items and reduces the number of links. If the number of links reaches 0 and no process opens the file, the file content will be deleted. If the file is not closed before unlilnk, the file content can still be accessed.

To sum up, the operations that really affect the number of links are:Link, unlink, and open Creation.
The true meaning of deleting a file is that the number of links to the file is 0, and the essence of this operation is Unlink. Close can delete the file content, because there is an unlink operation before close.

A simple example: use shell touch test.txt
1. Stat ("test.txt", & BUF );
Printf ("1. Link = % d \ n", Buf. st_nlink); // Number of test links before opening a file

2. FD = open ("test.txt", o_rdonly); // open the existing file test.txt.
Stat ("test.txt", & BUF );
Printf ("2. Link = % d \ n", Buf. st_nlink); // Number of test links

3. Close (FD); // close the test.txt file.
Stat ("test.txt", & BUF );
Printf ("3. Link = % d \ n", Buf. st_nlink); // Number of test links

4. Link ("test.txt", "test2.txt"); // create a hard chain connection test2.txt
Stat ("test.txt", & BUF );
Printf ("4. Link = % d \ n", Buf. st_nlink); // Number of test links

5. Unlink ("test2.txt"); // Delete test2.txt
Stat ("test.txt", & BUF );
Printf ("5. Link = % d \ n", Buf. st_nlink); // Number of test links

6. Repeat Step 2 // re-open test.txt

7. Unlink ("test.txt"); // Delete test.txt
Fstat (FD, & BUF );
Printf ("7. Link = % d \ n", Buf. st_nlink); // Number of test links

8. Close (FD); // This step can be left blank because the opened files are automatically closed when the process ends.

Perform the above eight steps sequentially. The results are as follows:
1. Link = 1
2. Link = 1 // open does not affect the number of links
3. Link = 1 // close does not affect the number of links
4. Link = 2 // link plus 1
5. Link = 1 // The number of links after unlink minus 1
2. Link = 1 // The number of re-opened links remains unchanged.
7. link = 0 // Unlink and then minus 1. Here we use the fstat function instead of stat function, because unlilnk has deleted the file name, so it cannot be accessed through the file name, But FD is still open, the file content has not been deleted. You can still use FD to obtain the file information.
Perform Step 8 to delete the file ....

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.