25

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.

rgaut
  • 3,159
  • 5
  • 25
  • 29

3 Answers3

27

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.

user2864740
  • 60,010
  • 15
  • 145
  • 220
  • 1
    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? – rgaut Sep 13 '14 at 02:44
  • 1
    @rgaut Basically, yes. (File descriptors are also isolated across processes, but can be inherited by children/forks.) – user2864740 Sep 13 '14 at 04:02
  • 1
    @user2864740 : 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! – aspen100 Dec 03 '14 at 22:41
  • 2
    At least on Linux, sockets do have inode numbers, which you can see with `netstat -e`, `cat /proc/net/tcp`, or `ls -l /proc//fd`. – Tim Jun 27 '16 at 17:17
  • 2
    _"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](https://unix.stackexchange.com/questions/384488/why-do-special-device-files-have-inodes)? – GIZ Aug 11 '17 at 10:25
  • 1
    @direprobs Thanks for the note. I hope the update eliminates corrects the misinformation. – user2864740 Aug 11 '17 at 20:38
19

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.

GIZ
  • 4,409
  • 1
  • 24
  • 43
3

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.

xyz
  • 115
  • 3
  • 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? – JohnyTex Feb 23 '22 at 10:16