disk drive being divided into one or more partitions. Each partition can contain a file system. The i-nodes are fixed-length entries that contain most of the information about a file.
Insight i-nodes and data blocks
insight i-nodes and directory blocks
insight i-node , data blocks and directory blocks.
We use the `/usr/lib/foo` as example to illustrate the insights
Process & VFS
The kernel uses three data structures to represent an open file, and the relationships among them determine the effect one process has on another with regard to file sharing.
- Every process has an entry in the process table. Within each process table entry is a table of open file descriptors, which we can think of as a vector, with one entry per descriptor. Associated with each file descriptor are a. The file descriptor flags b. A pointer to a file table entry
- The kernel maintains a file table for all open files. Each file table entry contains a The file status flags for the file, such as read, write, append, sync, and nonblocking b The current file offset c A pointer to the v-node table entry for the file
- Each open file (or device) has a v-node structure that contains information about the type of file and pointers to functions that operate on the file.
Kernel data structures for open files
- Two independent process have the same file open
- Parent duplicated all open files in the child.
Many differences between two independent process and parent-child process share the same open files.
- Two independent process maintain independent file table for the same open file, and each file table has an offset value. If these two process concurrent write the same file descriptor, without any form of synchronization, the output will be overwritten.
- Paren-child process share the same file table, If these two process concurrent write the same file descriptor, without any form of synchronization, the output will be intermixed.
How to avoid two independent process overwrite the same file?
- atomic operation
Add `O_APPEND`flag, the kernel will make the write operation be sequential. But as i test, this work fine on linux platform, however, OSX will raise error and prompt can't open the same file.
- pread, pwrite
ssize_t pread(int fd, void *buf, size_t nbytes, off_t offset); pwrite(int fd, const void *buf, size_t nbytes, off_t offset);
A symbolic link is an indirect pointer to a file, unlike the hard links described in the previous section, which pointed directly to the i-node of the file. Symbolic links were introduced to get around the limitations of hard links.
- Hard links normally require that the link and the file reside in the same file system.
- Only the superuser can create a hard link to a directory (when supported by the underlying file system).
The essential difference between hard link and symbolic link.
hard link, bar -> foo , the source and destination are identical and indistinguishable
symbolic link, bar -> foo