anonymous inode

本文深入探讨了unlink函数如何影响文件系统中文件的管理,包括如何删除文件名及文件的存在状态,以及函数对不同文件类型的影响。通过示例代码和gdb调试,展示了一个文件在被unlink后,文件名被删除但inode依然存在的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

其实,就是,一个文件,在打开之后,调用unlink, 这会导致文件的directory entry (文件名)被删除掉;这样的话,在文件系统中按照文件名就找不到它了,所以其它进程无法再打开这个文件了。 但是因为当前进程在打开这个文件,不会导致文件被删除。在打开文件的进程推出后,这个文件也就不存在了。

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc, char **argv)
{
    int fd = open( "/tmp/file", O_CREAT | O_RDWR, 0666 );
    unlink( "/tmp/file" );  
    return 0;
}

下面用gdb调试。

(gdb) r
Starting program: /home/charles/anonymous_inode 

Breakpoint 1, main (argc=1, argv=0xbfffefe4) at anonymous_inode.c:8
8		int fd = open( "/tmp/file", O_CREAT | O_RDWR, 0666 );
(gdb) n
9		unlink( "/tmp/file" );	

 ls /tmp/file  -li
6029376 -rw-rw-r-- 1 charles charles 0 May 18 01:07 /tmp/file

再执行一步(unlink):

$ ls /tmp/file  -li
ls: cannot access '/tmp/file': No such file or directory

但是,此时inode还是存在的:

$ lsof | grep "anonymous"
lsof: WARNING: can't stat() tracefs file system /sys/kernel/debug/tracing
      Output information may be incomplete.
anonymous 4469               charles  cwd       DIR        8,8     36864    8519682 /home/charles
anonymous 4469               charles  rtd       DIR        8,8      4096          2 /
anonymous 4469               charles  txt       REG        8,8      8356    8541543 /home/charles/anonymous_inode
anonymous 4469               charles  mem       REG        8,8   1786484     146275 /lib/i386-linux-gnu/libc-2.23.so
anonymous 4469               charles  mem       REG        8,8    147688     146267 /lib/i386-linux-gnu/ld-2.23.so
anonymous 4469               charles    0u      CHR      136,3       0t0          6 /dev/pts/3
anonymous 4469               charles    1u      CHR      136,3       0t0          6 /dev/pts/3
anonymous 4469               charles    2u      CHR      136,3       0t0          6 /dev/pts/3
anonymous 4469               charles    3u      REG        8,8         0    6029376 /tmp/file (deleted)

最后,我们看一下 man里面对 unlink函数的解释:

ESCRIPTION
       unlink()  deletes a name from the filesystem.  If that name was the last link to a file and no processes have the file open, the file is deleted and the space
       it was using is made available for reuse.

       If the name was the last link to a file but any processes still have the file open, the file will remain in existence until the last file descriptor referring
       to it is closed.

       If the name referred to a symbolic link, the link is removed.

       If the name referred to a socket, FIFO, or device, the name for it is removed but processes which have the object open may continue to use it.


Transaction isolation: TRANSACTION_REPEATABLE_READ Beeline version 3.1.2 by Apache Hive 0: jdbc:hive2://bigdata1:10000> create table if not exists ods.customer_inf(customer_inf_id int,customer_id int,cuwtomer_name string,identity_card_type int,identity_card_no STRING,mobile_phone INT,customer_email STRING,gender STRING,customer_point INT,register_time TIMESTAMP,birthday TIMESTAMP,customer_level INT, customer_money FLOAT, modified_time TIMESTAMP)PARTITIONED BY (etl_date STRING) STORED AS ORC TBLPROPERTIES ("orc.compress"="SNAPPY"); Error: Error while processing statement: FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. MetaException(message:Got exception: org.apache.hadoop.security.AccessControlException Permission denied: user=anonymous, access=EXECUTE, inode="/user":root:supergroup:drwx------ at org.apache.hadoop.hdfs.server.namenode.FSPermissionChecker.check(FSPermissionChecker.java:399) at org.apache.hadoop.hdfs.server.namenode.FSPermissionChecker.checkTraverse(FSPermissionChecker.java:315) at org.apache.hadoop.hdfs.server.namenode.FSPermissionChecker.checkPermission(FSPermissionChecker.java:242) at org.apache.hadoop.hdfs.server.namenode.FSPermissionChecker.checkPermission(FSPermissionChecker.java:193) at org.apache.hadoop.hdfs.server.namenode.FSPermissionChecker.checkTraverse(FSPermissionChecker.java:606) at org.apache.hadoop.hdfs.server.namenode.FSDirectory.checkTraverse(FSDirectory.java:1804) at org.apache.hadoop.hdfs.server.namenode.FSDirectory.checkTraverse(FSDirectory.java:1822) at org.apache.hadoop.hdfs.server.namenode.FSDirectory.resolvePath(FSDirectory.java:677) at org.apache.hadoop.hdfs.server.namenode.FSDirStatAndListingOp.getFileInfo(FSDirStatAndListingOp.java:114) at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.getFileInfo(FSNamesystem.java:3131) at org.apache.hadoop.hdfs.server.namenode.NameNodeRpcServer.getFileInfo(Name
最新发布
03-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值