File Descriptor Layer
Concept
Everything is a file
A cool aspect of the Unix interface is that most resources in Unix are represented as files, including devices such as the console, pipes, and of course, real files,
Each process tracks its open file
Xv6 gives each process its own table of open files, or file descriptors.
Global table
All the open files in the system are kept in a global file table. The file table can allocate new file, create a duplicate reference, release a reference, and read and write data.
Concurrent Access
If multiple processes open the same file independently, the different instances will have different I/O offsets (Use dup)
File Data Structure
struct file {
enum { FD_NONE, FD_PIPE, FD_INODE, FD_DEVICE, FD_SOCK } type;
int ref; // reference count
char readable;
char writable;
struct net *net; // FD_NET
struct pipe *pipe; // FD_PIPE
struct inode *ip; // FD_INODE and FD_DEVICE
struct sock *sock; // FD_SOCK
uint off; // FD_INODE and FD_DEVICE
short major; // FD_DEVICE
short minor; // FD_DEVICE
};Read and Write
Reading a file begins with checking permission. Ex: pipe has 2 files, one for read, one for write. Based on the file type, it reads differently.
Last updated
Was this helpful?