4

I'm starting to read Linux Systems Programming, 2nd Ed., and I was curious about the file table that is a "per-process list of open files." Is the file table like a table in a SQL db with the fds used as primary keys? If so, does this mean that the entries are repeated, or is it split into separate tables and normalized?

Or does it work entirely differently since we're dealing with straight C/assembly? If so, what data structures are used?

Where in the source is this subsystem defined? Most of the reason I'm doing this is to understand both C and Linux better. If I know where to find it, that would give me a better idea.

2
  • 2
    That would be Linux kernel code. No, there won't be any SQL stuff.
    – jordanm
    Jan 9, 2015 at 18:47
  • I would bet it is implemented as a double-linked-list in the kernel code. Haven't checked though. See lsof source code for access details.
    – user86969
    Jan 9, 2015 at 18:48

2 Answers 2

8

Since this is C, "table" is likely short for "array of structures".

You probably want to read "Understanding the Linux Kernel" or "Linux Kernel Development".

Or do it the hard way and read the source; good places to start might be:
include/linux/fdtable.h and
include/linux/fs.h.

3
  • This gives me a good place to start. the fdtable.h does have a struct for "fdtable" and one for the open file table named "files_struct." The C is a bit above my level, but it appears that the open file table is a files_struct that holds an fdtable (which in turn holds an array of fds), along with some state, safeguards, and limits.
    – labyrinth
    Jan 9, 2015 at 21:01
  • Something like that. If you want tu understand UNIX/Linux system programming (not necessarily just kernel programming) then you'll definitely need to have reasonable understanding of C - reading the code helps, but for starters something smaller, e.g. a well written network daemon/client, might do you a better service. nc (sometimes called netcat) comes to mind as reasonably simple.
    – peterph
    Jan 10, 2015 at 14:34
  • The links are broken. Can you update them?
    – Barmar
    Dec 13, 2023 at 20:56
1

per-process list of open files means each process represented as task_struct object has a member pointing to files_struct object for open-file information , and the files_struct holds fdtable which has :

  • an array of open file descriptors (in open_fds member)
  • an array of internal struct file objects (in fds member) which keeps useful information about low-level blocks e.g. struct inode

To better understand fdtable , you can read fdget() or other similar functions in the same C file, which looks for correct struct file object in the fdtable by given file descriptor (the integer in user application) , many of file I/O system calls invokes fdget() (e.g. fchown(),preadv(), or ftruncate() )

All links are the kernel code in version 6.0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .