VFS overview

insight VFS

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.

  1. 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
  2. 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
  3. 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

File Sharing

  • 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.

  1. 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.
  2. 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.

open("tmp.txt", O_RDWR|O_CREAT|O_APPEND)
  • pread, pwrite
#include <unistd.h>

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

results matching ""

    No results matching ""