File descriptors are an index into a file descriptor table stored by the kernel. The kernel creates a file descriptor in response to an open call and associates the file descriptor with some abstraction of an underlying file-like object, be that an actual hardware device, or a file system or something else entirely. Consequently a process’s read or write calls that reference that file descriptor are routed to the correct place by the kernel to ultimately do something useful.

image

image Ref: The UNIX Time- Sharing System by DM Ritchie Β· 1974

File Descriptor

image

Ref: https://www.cs.fsu.edu/~baker/opsys/notes/unixfiles.html

File Descriptors

A file descriptor represents an open file. It is a unique number assigned by the operating system to each file. It is an πšπ›π¬π­π«πšπœπ­π’π¨π§ for working with files. We need to use file descriptors to read from or write to files in our program.

Each process maintains its own file descriptor table.

πŸ”Ή In User Space When we open a file called β€œfileA.txt” in Process 1234, we get file descriptor fd1, which is equal to 3. We can then pass the file descriptor to other functions, to write data to the file.

πŸ”Ή In Kernel Space In Linux kernel, there is a 𝐩𝐫𝐨𝐜𝐞𝐬𝐬 π­πšπ›π₯𝐞 to maintain the data for the processes. Each process has an entry in the table. Each process maintains a file descriptor table, with file descriptors as its indices.

The file pointer points to an entry in the 𝐨𝐩𝐞𝐧 𝐟𝐒π₯𝐞 π­πšπ›π₯𝐞, which has information about open files across all processes. Multiple file descriptors can point to the same file table entry. For example, file descriptor 0,1 and 2 point to the same open file table entry.

Since different open file table entries can represent the same file, it is a waste of resources to store the file static information so many times. We need another abstraction layer called β€˜vnode table’ to store the static data.

image

image

image

Virtual File System

image

image

Memory Types

image

File permissions

image

image

image

image