Unix system file tables
Asked Answered
E

2

31

I am confused about Unix system file tables.

  • When two or more processes open a file for reading, does the system file table create separate entries for each process or a single entry?

  • If a single entry is created for multiple processes opening the same file, will their file offsets also be the same?

  • If process 1 opens file1.txt for reading and process 2 opens the same file file1.txt for writing, will the system file table create one or two entries?

Eulaheulalee answered 7/1, 2013 at 4:36 Comment(2)
Sir how can be this a duplicate? he is asking about parent child and fork stuff, my question is differentEulaheulalee
Your question is about open files and processes; the other question is about open files and fork. Processes are created by the fork system call. Thus, they are essentially the same question.Corse
C
60

There are three "system file tables": There is a file descriptor table that maps file descriptors (small integers) to entries in the open file table. Each entry in the open file table contains (among other things) a file offset and a pointer to the in-memory inode table. Here's a picture: enter image description here (source: rich from www.cs.ucsb.edu now on archive.org)

So there is neither just one file table entry for an open file nor is there just one per process ... there is one per open() call, and it is shared if the file descriptor is dup()ed or fork()ed.

Answering your questions:

  1. When two or more processes open a file for reading, there's an entry in the open file table per open. There is even an entry per open if one process opens the file multiple times.

  2. A single entry is not created in the open file table for different processes opening the same file (but there is just one entry in the in-memory inode table).

  3. If file1.txt is opened twice, in the same or two different processes, there are two different open file table entries (but just one entry in the in-memory inode table).

Corse answered 7/1, 2013 at 4:51 Comment(11)
I am asking about System file table.Eulaheulalee
There is the open file table and there is the in-memory inode table ... both of these are "system" file tables since they are maintained by the system (OS). Your question is about opening files, so it must be about the former, not the latter.Corse
As far as I know there as three tables (at least). File descriptor table, System file Table and Memory Inode tableEulaheulalee
The file descriptor table is per-process and points to entries in the open file table. There isn't anything that is just called the "system file table". Try googling these terms or searching SO and you'll see. Here's a useful picture: cs.ucsb.edu/~rich/class/cs170/notes/FileSystem/…Corse
this picture shows that if two different processes open a file, the counter increments in open file table (which I suppose is System file table). So I can think that System file table behaves the sameEulaheulalee
@Eulaheulalee No, it does not show that. As I said, entries are shared when they are dup'ed or the process is forked (that's how you get two processes pointing to the same open file table entry) -- that is when the open file entry counter is incremented. I've given you the answer; if you think it's wrong, post your own answer to your question.Corse
@Eulaheulalee Note again that there is one open file table entry per open call, and one in-memory inode table entry per opened file ... if two processes open the same file, the latter is what gets its count incremented.Corse
@Alfred, also, again, there is no such thing as the "System file table" ... so your question is unclear because you haven't specified which table you mean. Perhaps citing or quoting some of the material you have read would help.Corse
this note (usna.edu/Users/cs/wcbrown/courses/IC221/classes/L09/Class.html) might clarify the confusing concepts. Each process has one file descriptor array for each connection; there is a system-wide open file table, each entry in this table tracks which underlying file the descriptor refers to, file status, the current offset, and relevant details; also the OS keeps an inode table. there is a pic in APUE 3.10 shows these data structures for open files.Au
@JimBalter Child process from fork() will share the FILE Table Entry with the Parent process. How to ensure the correctness if they modify at the same timeGaribald
@YongqiZ You should this question separately to SO. (Aside from this being a separate issue, I'm retired and not taking questions.)Corse
S
-1

The same file may be opened simultaneously by several processes, and even by the same process (resulting in several file descriptors for the same file) depending on the file organization and filesystem. Operations on the descriptors like moving the file pointer, or closing it are independent (they do not affect other descriptors for the same file). Operations of the file (like a write) can be seen by operations on the other descriptors (a posterior read can read the written data).

This is from the open(System call) wiki page

Salve answered 15/5, 2014 at 16:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.