posts - 403, comments - 310, trackbacks - 0, articles - 7
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

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

          Posted on 2007-08-20 22:39 ZelluX 閱讀(647) 評論(0)  編輯  收藏 所屬分類: LinuxC/C++
          內(nèi)核使用三種數(shù)據(jù)結構表示一個打開的文件,他們之間的關系決定了進程間對于共享文件的作用。
          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.


          上圖為這三種數(shù)據(jù)結構及其相互聯(lián)系
          其中v-node主要用于提供單個操作系統(tǒng)上的多文件系統(tǒng)支持,Sun把它稱為Virtual File System
          linux中沒有v-node,使用了一個generic i-node代替,盡管使用了不同的實現(xiàn)方式,v-node在概念上與generic i-node相同。

          下面來討論兩個獨立的進程打開同一文件的情況
          這種情況下兩個進程擁有不同的file table,但其中的兩個指針指向了同一個v-node。

          知道了這些數(shù)據(jù)結構的情況以后,我們可以更精確的知道某些操作的結果
          1) 每次write操作結束后,當前文件的offset增加,如果這個操作導致當前的offset超出了文件長度,則i-node表中記錄的文件大小會被修改為改動后的大小。
          2) 使用O_APPEND方式打開文件后,每次調用write函數(shù)時,當前文件的offset都會被設置為i-node表中的該文件大小,從而write函數(shù)只能在文件尾部追加。
          3) lseek 函數(shù)只改變在file table中的當前文件offset,并不會產(chǎn)生io操作

          注意file descriptor flag和file status flag的作用域差別,前者屬于某個單獨進程的單獨的file descriptor,后者則適用于任意進程中指向給定file table entry的所有descriptor。fcntl函數(shù)可以修改這兩種flag

          主站蜘蛛池模板: 得荣县| 涿鹿县| 白水县| 桃源县| 宁蒗| 康保县| 商城县| 高唐县| 偃师市| 保靖县| 苍南县| 霍山县| 福安市| 恩施市| 桃源县| 四平市| 开封县| 阳朔县| 石狮市| 积石山| 道孚县| 苏州市| 德令哈市| 金沙县| 瑞昌市| 红桥区| 郑州市| 天镇县| 姚安县| 杨浦区| 泸西县| 杭锦后旗| 康马县| 邮箱| 宁阳县| 灵石县| 邵阳市| 米脂县| 馆陶县| 邢台县| 东港市|