Linux in Link,unlink,close,fclose detailed

Source: Internet
Author: User
Tags touch command

Each file can obtain the file information through a struct stat structure, where one member St_nlink the number of links representing the file.
When using the shell's Touch command or open a nonexistent file with O_creat in the program, the number of links to the file is 1.

Usually open an existing file does not affect the number of links to the file. The function of open is simply to make an access relationship between the calling process and the file, that is, to return FD after open, the calling process can use FD to read, write, ftruncate and so on a series of operations on the file.
Close () is to eliminate the access relationship between this calling process and the file. Naturally, the number of links to the file will not be affected. When Close is called, the kernel checks the number of processes that open the file, and if this number is 0, further checks the number of links to the file, and if the number is 0, delete the file contents.

The link function creates a new catalog entry and adds a number of links.
The unlink function deletes a catalog entry and reduces the number of links. If the number of links reaches 0 and no process has opened the file, the contents of the file are actually deleted. If you do not have close before Unlilnk, you can still access the contents of the file.

The action that really affects the number of links is the creation of link, unlink, and open.
The real meaning of deleting a file's content is that the number of links to the file is 0, and the essence of the operation is unlink. Close is able to implement the deletion of the contents of the file, necessarily because there is a unlink operation before close.

For example, a simple explanation:

#include <stdio.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<unistd.h>intMain () {intFD; //Char buf[32];    structstat buf;//number of front-line test links without open file    if(Stat ("ABC", &buf)! =0) printf ("stat file fail \ n"); printf ("1. Link =%d \ n", Buf.st_nlink); //Open an already existing ABCFD = open ("ABC", o_rdonly); Stat ("ABC", &buf); printf ("2. Link =%d \ n", Buf.st_nlink); //Close File Test link countClose (FD); Stat ("ABC", &buf); printf ("3. Link =%d \ n", Buf.st_nlink); //Create a hard link, test the number of links    if(Link ("ABC","ABC1")) printf ("link ABC fail\n"); Stat ("ABC", &buf); printf ("4. Link =%d \ n", Buf.st_nlink); //Delete ABC1, test link Count    if(Unlink ("ABC1") ==0) printf ("\tdelete abc1\n"); Stat ("ABC", &buf); printf ("5. Link =%d \ n", Buf.st_nlink); //Repeat step 2FD = open ("ABC", o_rdonly); Stat ("ABC", &buf); printf ("2. Link =%d \ n", Buf.st_nlink); //Delete ABC    if(Unlink ("ABC") ==0) printf ("\tdelete abc\n"); Fstat (FD,&buf); printf ("7. Link =%d \ n", Buf.st_nlink);    Close (FD); return 0;}

To ensure that there is ABC, first execute: Touch ABC, then compile and run:

1. link =12. link =13. link =14. link =2     Delete ABC15. link =12. link =1      Delete ABC 7. link =0

Linux in Link,unlink,close,fclose detailed

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.