posts - 403, comments - 310, trackbacks - 0, articles - 7
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

APUE - File I/O (3) - File Sharing

Posted on 2007-08-20 22:39 ZelluX 阅读(578) 评论(0)  编辑  收藏 所属分类: LinuxC/C++
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
1) The file descriptor flags (close-on-exec; refer to Figure 3.6 and Section 3.14)
2) A pointer to a file table entry

The kernel maintains a file table for all open files. Each file table entry contains
1) The file status flags for the file, such as read, write, append, sync, and nonblocking
2) The current file offset
3) 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. For most files, the v-node also contains the i-node for the file. This information is read from disk when the file is opened, so that all the pertinent information about the file is readily available. For example, the i-node contains the owner of the file, the size of the file, pointers to where the actual data blocks for the file are located on disk, and so on.

其中v-node主要用于提供单个操作系统上的多文件系统支持,Sun把它称为Virtual File System
linux中没有v-node,使用了一个generic i-node代替,尽管使用了不同的实现方式,v-node在概念上与generic i-node相同。

这种情况下两个进程拥有不同的file table,但其中的两个指针指向了同一个v-node。

1) 每次write操作结束后,当前文件的offset增加,如果这个操作导致当前的offset超出了文件长度,则i-node表中记录的文件大小会被修改为改动后的大小。
2) 使用O_APPEND方式打开文件后,每次调用write函数时,当前文件的offset都会被设置为i-node表中的该文件大小,从而write函数只能在文件尾部追加。
3) lseek 函数只改变在file table中的当前文件offset,并不会产生io操作

注意file descriptor flag和file status flag的作用域差别,前者属于某个单独进程的单独的file descriptor,后者则适用于任意进程中指向给定file table entry的所有descriptor。fcntl函数可以修改这两种flag