我有一个关于Linux的一般性问题。如果我创建一个FIFO(命名管道)、管道或套接字,那么inode会被创建吗?
/proc/<PID>/fd
目录获取答案。引用/proc
文档(man 5 proc):
For file descriptors for pipes and sockets, the entries will be symbolic links whose content is the file type with the inode. A readlink(2) call on this file returns a string in the format:
type:[inode]
For example, socket:[2248868] will be a socket and its inode is 2248868. For sockets, that inode can be used to find more information in one of the files under /proc/net/.
让我们验证一下:
$ bash -c 'true | ls -l /proc/self/fd/0'
lr-x------ 1 user user 64 Sep 13 03:58 /proc/self/fd/0 -> 'pipe:[54741]'
那么管道和套接字会有一个inode吗?是的!那FIFO呢?我们可以猜测,由于它们有文件名,它们也有inode(我认为没有inode的目录条目是不存在的)。但让我们来验证一下:
$ mkfifo foobar.fifo
$ ls -i foobar.fifo
1093642 foobar.fifo
pipefs
虚拟文件系统}},它被挂载在内核空间而不是用户空间。它管理管道和FIFO,因此您在/proc
示例中看到的inode编号是这些文件系统的属性,而不是磁盘上的文件系统。匿名管道和匿名套接字在磁盘文件系统上没有inode,因为没有文件名和字节(尽管数据可能会被缓存,并且实际上旧的Unix将管道缓存到磁盘上)。但是,FIFO和Unix域套接字在文件系统上有文件名,因此在foobar.fifo
示例中,该inode属于磁盘文件系统。匿名管道或套接字不会创建inode,因为inode是文件系统的属性,它们都不作为文件系统实体而存在(它们没有文件路径),只有文件描述符。
但是,对于命名管道(也称为FIFO),会创建inode,因为它作为文件系统实体存在。
unix(7)
。 - sarnold/proc/
、/dev/
、/sys
,以及内核空间中的文件系统,实际上也使用了 inode 号。请参见我在该主题上的回答:https://dev59.com/5VjUa4cB1Zd3GeqPULwX#52302763。 - Sergiy Kolodyazhnyy