What is the difference between inode number and file descriptor?
Asked Answered
G

3

26

I understand file descriptors are kernel handle to identify the file , while inode number of a file is pointer to a structure which has other details about file(Correct me if I am wrong). But I am unable to get the difference between them.

Georg answered 13/9, 2014 at 2:19 Comment(0)
K
29

An inode is an artifact of a particular file-system and how it manages indirection. A "traditional *ix" file-system uses this to link together files into directories, and even multiple parts of a file together. That is, an inode represents a physical manifestation of the file-system implementation.

On the other hand, a file descriptor is an opaque identifier to an open file by the Kernel. As long as the file remains open that identifier can be used to perform operations such as reading and writing. The usage of "file" here is not to be confused with a general "file on a disk" - rather a file in this context represents a stream and operations which can be performed upon it, regardless of the source.

A file descriptor is not related to an inode, except as such may be used internally by particular [file-system] driver.

Kelle answered 13/9, 2014 at 2:23 Comment(6)
So File descriptor will be maintained by kernel and every time a process open a file corresponding entry (generated by kernel) will be made in that table. Is that true?Georg
@Georg Basically, yes. (File descriptors are also isolated across processes, but can be inherited by children/forks.)Kelle
@Kelle : What is the relationship then (if any) between a file descriptor and a file's inumber (inumber being the unique number for a file on a file system, that is mapped to a file name in the directory structure and is used to track hard links to a file)? Does the inode also contain the file's inumber, in addition to other metadata? Thanks!Rabon
At least on Linux, sockets do have inode numbers, which you can see with netstat -e, cat /proc/net/tcp, or ls -l /proc/<pid>/fd.Desdee
"A number of commonly opened files (e.g. sockets and special devices) do not even have inodes!" This is incorrect. Why do special files have inodes?Astonish
@direprobs Thanks for the note. I hope the update eliminates corrects the misinformation.Kelle
A
21

The difference is not substantial, both are related to the abstract term called "file". An inode is a filesystem structure that represents files. Whereas, a file descriptor is an integer returned by open syscall. By definition:

Files are represented by inodes. The inode of a file is a structure kept by the filesystem which holds information about a file, like its type, owner, permissions, inode links count and so on.

On other the hand, a file descriptor

File Descriptors: The value returned by an open call is termed a file descriptor and is essentially an index into an array of open files kept by the kernel.

The kernel doesn't represent open files by their names, instead it uses an array of entries for open files for every process, so a file descriptor in effect is an index into an array of open files. For example, let's assume you're doing the following operation in a process:

read(0, 10)

0 denotes the file descriptor number, and 10 to read 10 bytes. In this case, the process requests 10 bytes from the file/stream in index 0, this is stdin. The kernel automatically grants each process three open streams:

Descriptor No. 
      0  --->          stdin
      1  --->          stdout
      2  --->          stderr

These descriptors are given to you for free by the kernel.

Now, when you open a file, in the process via open("/home/myname/file.txt") syscall, you'll have index 3 for the newly opened file, you open another file, you get index 4 and so forth. These are the descriptors of the opened files in the process:

 Descriptor No. 
      0  --->           stdin
      1  --->           stdout
      2  --->           stderr
      3  --->           /home/user100/out.txt  
      4  --->           /home/user100/file.txt

See OPEN(2) it explains what goes underneath the surface when you call open.

Astonish answered 11/8, 2017 at 11:9 Comment(0)
I
4

The fundamental difference is that an inode represents a file while a file descriptor (fd) represents a ticket to access the file, with limited permission and time window. You can think an inode as kind of complex ID of the file. Each file object has a unique inode. On the other hand, a file descriptor is an "opened" file by a particular user. The user program is not aware of the file's inode. It uses the fd to access the file. Depending on the user's permissions and the mode the user program choses to open the file (read-only for example) a fd is allowed a certain set of operations on the file. Once the fd is "closed" the user program can't access the file unless it opens another fd. At any given time, there can be multiple fds accessing a file in the same or different user programs.

Izaguirre answered 29/9, 2020 at 19:44 Comment(1)
So an fd is pointing to the actual memory on disk, while a filename is pointing to the inode pointing to the memory on disk, right? So I guess you could mv the file(name) to to another file(name), but in the meantime the fd still points to the same place in memory, right?Overstrain

© 2022 - 2025 — McMap. All rights reserved.