<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 文件系統,第 9 部分:磁盤塊示例 > 原文:<https://github.com/angrave/SystemProgramming/wiki/File-System%2C-Part-9%3A-Disk-blocks-example> ## 正在施工?? ## 請問您能解釋一下文件內容如何存儲在簡單的基于 i 節點的文件系統中的簡單模型嗎? 當然!要回答這個問題,我們將構建一個虛擬磁盤,然后編寫一些 C 代碼來訪問其內容。我們的文件系統將可用字節劃分為 inode 的空間和磁盤塊的更大空間。每個磁盤塊將為 4096 字節 - ```c // Disk size: #define MAX_INODE (1024) #define MAX_BLOCK (1024*1024) // Each block is 4096 bytes: typedef char[4096] block_t; // A disk is an array of inodes and an array of disk blocks: struct inode[MAX_INODE] inodes; block[MAX_BLOCK] blocks; ``` 請注意,為了清楚起見,我們不會在此代碼示例中使用“unsigned”。我們的固定大小的 inode 將包含文件的大小(以字節為單位),權限,用戶,組信息,時間元數據。與問題最相關的是它還包括十個指向磁盤塊的指針,我們將使用這些指針來引用實際文件的內容! ```c struct inode { int[10] directblocks; // indices for the block array i.e. where to the find the file's content long size; // ... standard inode meta-data e.g. int mode, userid,groupid; time_t ctime,atime,mtime; } ``` 現在我們可以弄清楚如何讀取文件偏移量`position`的字節: ```c char readbyte(inode*inode,long position) { if(position <0 || position >= inode->size) return -1; // invalid offset int block_count = position / 4096,offset = position % 4096; // block count better be 0..9 ! int physical_idx = lookup_physical_block_index(inode, block_count ); // sanity check that the disk block index is reasonable... assert(physical_idx >=0 && physical_idx < MAX_BLOCK); // read the disk block from our virtual disk 'blocks' and return the specific byte return blocks[physical_idx][offset]; } ``` 我們的 lookup_physical_block 的初始版本很簡單 - 我們可以使用 10 個直接塊的表格! ```c int lookup_physical_block_index(inode*inode, int block_count) { assert(block_count>=0 && block_count < 10); return inode->directblocks[ block_count ]; // returns an index value between [0,MAX_BLOCK) } ``` 這種簡單的表示是合理的,只要我們可以用十個塊表示所有可能的文件,即最多 40KB。大文件怎么樣?我們需要 inode 結構始終具有相同的大小,因此將現有的直接塊數組增加到 20 將大約是我們的 inode 大小的兩倍。如果我們的大多數文件需要少于 10 個塊,那么我們的 inode 存儲現在是浪費的。為了解決這個問題,我們將使用一個名為 _ 間接塊 _ 的磁盤塊來擴展指針數組。我們只需要這個文件&gt; 40KB ```c struct inode { int[10] directblocks; // if size<4KB then only the first one is valid int indirectblock; // valid value when size >= 40KB int size; ... } ``` 間接塊只是一個 4096 字節的常規磁盤塊,但我們將使用它來保存指向磁盤塊的指針。在這種情況下我們的指針只是整數,所以我們需要將指針強制轉換為整數指針: ```c int lookup_physical_block_index(inode*inode, int block_count) { assert(sizeof(int)==4); // Warning this code assumes an index is 4 bytes! assert(block_count>=0 && block_count < 1024 + 10); // 0 <= block_count< 1034 if( block_count < 10) return inode->directblocks[ block_count ]; // read the indirect block from disk: block_t* oneblock = & blocks[ inode->indirectblock ]; // Treat the 4KB as an array of 1024 pointers to other disk blocks int* table = (int*) oneblock; // Look up the correct entry in the table // Offset by 10 because the first 10 blocks of data are already // accounted for return table[ block_count - 10 ]; } ``` 對于典型的文件系統,我們的索引值是 32 位,即 4 字節。因此,在 4096 字節中,我們可以存儲 4096/4 = 1024 個條目。這意味著我們的間接塊可以引用 1024 * 4KB = 4MB 的數據。通過前十個直接塊,我們可以容納最大 40KB + 1024 * 4KB = 4136KB 的文件。對于小于此值的文件,某些后續表條目可能無效。 對于更大的文件,我們可以使用兩個間接塊。然而,有一個更好的選擇,這將允許我們有效地擴展到大型文件。我們將包含一個雙間接指針,如果這還不夠三重間接指針。雙重間接指針意味著我們有一個 1024 個條目表到磁盤塊,用作 1024 個條目。這意味著我們可以參考 1024 * 1024 個磁盤數據塊。 ![inode disk blocks for data](https://img.kancloud.cn/05/f9/05f93bc354b5fef6ee1ffdca8264bd96_459x549.jpg) (來源: [http://uw714doc.sco.com/en/FS_admin/graphics/s5chain.gif](http://uw714doc.sco.com/en/FS_admin/graphics/s5chain.gif) ) ```c int lookup_physical_block_index(inode*inode, int block_count) { if( block_count < 10) return inode->directblocks[ block_count ]; // Use indirect block for the next 1024 blocks: // Assumes 1024 ints can fit inside each block! if( block_count < 1024 + 10) { int* table = (int*) & blocks[ inode->indirectblock ]; return table[ block_count - 10 ]; } // For huge files we will use a table of tables int i = (block_count - 1034) / 1024 , j = (block_count - 1034) % 1024; assert(i<1024); // triple-indirect is not implemented here! int* table1 = (int*) & blocks[ inode->doubleindirectblock ]; // The first table tells us where to read the second table ... int* table2 = (int*) & blocks[ table1[i] ]; return table2[j]; // For gigantic files we will need to implement triple-indirect (table of tables of tables) } ``` 請注意,使用 double indirect 讀取字節需要 3 個磁盤塊讀取(兩個表和實際數據塊)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看