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: through the shell touch test.txt
1, stat ("Test.txt", &buf);
printf ("1.link=%d\n", buf.st_nlink);//test the number of links before opening the file
2, Fd=open ("Test.txt", o_rdonly);//Open existing file Test.txt
Stat ("Test.txt", &buf);
printf ("2.link=%d\n", buf.st_nlink);//Test link count
3, close (FD);//Close File Test.txt
Stat ("Test.txt", &buf);
printf ("3.link=%d\n", buf.st_nlink);//Test link count
4, Link ("Test.txt", "test2.txt");//Create a hard link test2.txt
Stat ("Test.txt", &buf);
printf ("4.link=%d\n", buf.st_nlink);//Test link count
5, unlink ("test2.txt");//Delete Test2.txt
Stat ("Test.txt", &buf);
printf ("5.link=%d\n", buf.st_nlink);//Test link count
6. Repeat step 2//Reopen Test.txt
7, unlink ("test.txt");//delete test.txt
Fstat (FD,&BUF);
printf ("7.link=%d\n", buf.st_nlink);//Test link count
8, close (FD);//This step can not be written out, because the open file is automatically closed when the process ends.
Perform the above 8 steps sequentially, with the following results:
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 after link number plus 1
5.link=1//unlink post-link number minus 1
2.link=1//re-open link number unchanged
7.link=0//unlink after minus 1, here we use Fstat function instead of stat, because Unlilnk has deleted the file name, so it can not be accessed by the file name, but FD is still open, the file content has not been really deleted, You can still get file information using FD.
Perform step 8, file contents are deleted ....
Linux in Link,unlink,close,fclose detailed