Avatar of Maarten Bruins
Maarten Bruins

asked on 

The structure of the file descriptor "table"?

Actually the file descriptor table is not a real table. It's just an array of pointers to the "open file table" (struct file). But let's say we will see it as a table. What are the columns? For example:

FD   | Pointer to "open file table"
...  | ...

In short, that's the question. I see a lot of different figures on the internet, but they are all different. For example, see:

There they have a column "fd flags" (read/write), but I would think that this column is part of the "open file table" and not part of the "file descriptor table". See for example: http://man7.org/linux/man-pages/man2/open.2.html

       A call to open() creates a new open file description, an entry in the
       system-wide table of open files.  The open file description records
       the file offset and the file status flags (see below).  A file
       descriptor is a reference to an open file description; this reference
       is unaffected if pathname is subsequently removed or modified to
       refer to a different file.  For further details on open file
       descriptions, see NOTES.

       The argument flags must include one of the following access modes:
       O_RDONLY, O_WRONLY, or O_RDWR.  These request opening the file read-
       only, write-only, or read/write, respectively.

So this creates a file descriptor and an entry in the open file table. A file descriptor is only an integer, so then I would conclude that O_RDONLY/O_WRONLY/O_RDWR is part of the open file table and not of the file descriptor table. They also say this:

A file descriptor is a reference to an open file description; this reference is unaffected if pathname is subsequently removed or modified to refer to a different file.

If write/read would be part of the file descriptor table, then the above can not be true in my opinion.

Another example:

Here they also have a column "flags". Do they also mean read/write et cetera with "flags"? Or what exactly do they mean by flags in this case?

Here they don't have the column flags at all.

I think for the answer we have to look at https://elixir.bootlin.com/linux/v3.18/source/include/linux/fdtable.h

 * Open file table structure
struct files_struct {
   * read mostly part
      atomic_t count;
      struct fdtable __rcu *fdt;
      struct fdtable fdtab;
   * written part on a separate cache line in SMP
      spinlock_t file_lock ____cacheline_aligned_in_smp;
      int next_fd;
      unsigned long close_on_exec_init[1];
      unsigned long open_fds_init[1];
      struct file __rcu * fd_array[NR_OPEN_DEFAULT];

Although they write "open file table structure", probably this is what other people call the "file descriptor table". These structures are still a bit abracadabra for me, but I don't see something like read/write flags.

So how I have to see it? Which columns does the file descriptor "table" have? (if we would see it as a table)
LinuxCLinux OS DevUnix OSLinux Distributions

Avatar of undefined
Last Comment
Maarten Bruins

8/22/2022 - Mon