-1

I have an interest in Operating Systems. So I am reading the xv6 book to understand Operating Systems. This is my first book on the subject. I read a line I couldn't understand.

Internally, the xv6 kernel uses the file descriptor as an index into a per-process table, so that every process has a private space of file descriptors starting at zero.

I thought that file descriptors represent data streams that can be written into or read from. How does that tie into the process table? Isn't the file descriptor table a part of the process's memory representing its open file resources?

Thanks in advance!

  • 3
    It says "**per-process** table", not "process table". "per-process table" == "separate tables specific to each process" – muru Jan 14 '20 at 06:52

1 Answers1

3

Your understanding of is correct. The file descriptor table is part of the individual process's memory (well the indexes into the table is available to the process, while the table itself is a kernel structure not directly accessible from the user-space process; but the table, or part of the table, is still specific to the process though).

This is also what the text says:

[...] uses the file descriptor as an index into a per-process table [...]

This is another way of saying "uses a file descriptor as an index into a table that is specific to each process".

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
  • To nitpick: the file descriptor table is not part of the process's memory, or at least not the part that is visible from user space. This is of course a bit of an implementation specific detail, but the thing Unix-like operating systems have in common is that open file handling is a kernel internal thing. The file descriptor table is not accessible from user space; the kernel hands out a small integer for each open file, and the file is referred to using this integer. In the Linux kernel, a file descriptor table is allocated using `kmalloc` for each process. – Johan Myréen Jan 14 '20 at 08:24
  • @JohanMyréen Thanks. I added a parenthesis about this. Does that sound correct to you? – Kusalananda Jan 14 '20 at 10:21
  • 1
    Looks good to me. – Johan Myréen Jan 14 '20 at 12:07
  • @JohanMyréen FWIW, the Q is about [xv6](https://en.wikipedia.org/wiki/Xv6), a didactical rewrite a Unix version 6, obviously quite different from Linux or any modern Unix system. There are other questions about xv6 (with answers which badly miss the mark -- eg. [this](https://unix.stackexchange.com/a/72025/308316): there's no `/dev/tty` in xv6). An `xv6` tag would probably be needed, but my experience with trying to add tags has not been the best ;-) –  Jan 14 '20 at 17:31
  • And no, the file descriptor table is __not__ part of a process's memory, but of the kernel's memory (in both linux or xv6). It's just referenced from the process struct, which itself lives in kernel, not in user memory. –  Jan 14 '20 at 17:43
  • @mosvy How are the file descriptors an "index" into the per-process table? – Vishal Dalwadi Jan 16 '20 at 10:53
  • @VishalDalwadi the per-process table is `proc->ofile` which is an array. A file descriptor is used as an index in that array -- search for `curproc->ofile[fd]`. –  Jan 16 '20 at 10:57