FANFNA
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    :::spoiler [TOC] ::: # 一、*File System Interface* ![](https://i.imgur.com/0J88il8.png) :::success 檔案系統是一套實現了資料的儲存、分級組織、存取和取得等操作的抽象資料類型(Abstract data type)。 檔案系統使用檔案和樹形目錄的抽象邏輯概念代替了硬碟和光碟等物理裝置使用資料塊的概念,用戶使用檔案系統來儲存資料不必關心資料實際儲存在硬碟(或者光碟)的位址為多少的資料塊上,只需要記住這個檔案的所屬目錄和檔案名。 ::: :::info 一個好的檔案系統須具有: 1. 良好的空間管理 2. 有效的資料存取 3. 好的容錯能力 ::: :::info 完整的檔案系統包含三個部分: 檔案 目錄結構:檔案分門別類地儲存於硬碟 分割:將一個實體硬碟分成數個邏輯上的硬碟 ::: ## 檔案屬性 (Metadata):檔案儲存於硬碟中,系統同時儲存跟檔案相關的資料稱為檔案屬性值 :::success - 檔案名稱 **(Name)**:給人看 - **ID**:給機器看 - 型態 **(Type)**:以辨識不同資料型態的檔案,包含目錄、裝置、普通檔案 - 位置 **(Location)**:指向硬碟實體儲存位置的指標 - 大小 **(Size)**:儲存於硬碟中的儲存空間 - 保護 **(Protection)**:安全上的考量 - 擁有者 **(user id)** - 日期資訊 **(time&Date)**:檔案建立、修改的日期時間 ::: ![](https://i.imgur.com/28MnTfb.png) ## 檔案操作(File Operation):使用者透過系統呼叫來完成檔案存取。 :::success - 建立檔案 fopen()`create` - 寫入檔案 (附加於檔後、覆蓋檔案) fwrite()`write` - 讀取檔案 fread()`read` - 重置擋案 fseek()`repositioning` - 刪除檔案 unlink():標記為刪除,資料還在。`delet` - 屬性更改 fturn():更改檔名、權限及擁有者。`turncating` - [delet & turn 比較](https://byron0920.pixnet.net/blog/post/85759990) ::: ## file type ![](https://i.imgur.com/O674F3Z.png) ## open file table :::success - file pointer - records the current position in the file, for the next read or write access - file open count - How many times has the current file been opened ( simultaneously by different processes) and not yet closed? When this counter reaches zero the file can be removed from the table.(`dynamic open table`) - Disk location - 檔案位置 - Access right - 權限 ::: ## file locking ### share lock 寫鎖 :::info `reading only.` (r)寫鎖 ::: ### exclusive lock 讀鎖 :::info ` for writing as well as reading`(wr)讀鎖 ::: ### advisory lock 建議鎖 :::success 一般的鎖是建議鎖(`advisory lock`),每個執行緒在存取對應資源前都需取得鎖的資訊,再根據資訊決定是否可以存取。若存取對應資訊,鎖的狀態會改變為鎖定,因此其他執行緒此時不會存取該資源,當資源結束後,會恢復鎖的狀態,允許其他執行緒的存取。 ::: ### mamndatory lock 強制鎖 :::info 強制鎖(mandatory lock),若有未授權的執行緒想要存取鎖定的資料,在存取時就會產生異常 ::: ### example [issue] `Binary` or `Text`? :::info Textmode : ``` bash= fopen("abc.txt","r+t"); Translate Ctrl-Z into EOF, Translate \r\n into \n. #location e.g. .html ``` Binary mode : ``` bash= fopen("xyz.mp3","rb"); Raw input e.g. .doc ``` ::: # 二、Access Methods ## Sequential access :::success - Read/write next (block) - Reset - 重置file pointer到file的起始點 - Skip/rewind n records ![](https://i.imgur.com/R2aecRJ.png) - 因為磁頭的關係,若是random,移動磁頭花時間 - 邏輯上的連續,檔案儲存實體硬碟位置不一定要連續 ::: ## Direct (relative) access :::success - 可以access在任意位置的element - File operations需要block #作為參數 - Often use ==random access== to refer the ==access pattern== from direct access - 檔案由多個==大小固定的區塊==所組成,可直接將檔案指標移動到某個特定的區塊,並一次讀取整個區塊 ::: ![](https://i.imgur.com/3JsW1He.png) ## record access • file is array of fixed- or variable-sized records ## Index Access - Index - 包含pointers to blocks of a file - To find a record in a file: - 搜尋index file $\rightarrow$ 找到pointer - 使用pointer去直接存取record - 若是一個large file $\rightarrow$ index可能會變很大 ![](https://i.imgur.com/XZcZSpw.png) [access](https://en.wikipedia.org/wiki/Access_method) # 三、Directory issue ## Single-Level Directory :::info - 所有的file都在同一個directory - 檔案名稱必須要==唯一== - 當檔案的數量增加,locating file的效率差 ::: ![](https://i.imgur.com/Q3WXDM2.png) ## Two-Level Directory :::info - directory被分給每個使用者 - path = user name + file name - single-level dir的問題依然存在在每個使用者 - 缺點 - 不同使用者不能共享檔案與目錄 ::: ![](https://i.imgur.com/wcE6JB5.png) ## Tree-Structured Directory :::info - 絕對路徑 - 從root開始 - 相對路徑 - 從directory開始 ::: ![](https://i.imgur.com/ylFwAEc.png) ## Acyclic-Graph Directories ![](https://i.imgur.com/izWzvaL.png) ### Aliasing(Soft/hard) - Two methods for using alias in UNIX 1. Soft link:`ln -s`,檔案名稱代換(f1 是檔名代換到 f2)軟連結要再透過硬連結來存取硬碟上的資料,可跨網路。[symbolic link](https://zh.wikipedia.org/wiki/%E7%AC%A6%E5%8F%B7%E9%93%BE%E6%8E%A5) 2. Hard link:`ln`,分不出來誰是本尊(連結直接發生在硬碟) 硬連結直接透過分割區上的配置表來對應資料,只能在本機硬碟使用 - Three problems with aliasing 1. Duplicated files:happened when Backup 2. Dangling pointer:happened when Delete 3. Cycle - 使用==links==去分享files或directories - UNIX-like:ln <新連結位置> <原被連結位置> - 一個file可以有==多條絕對路徑== - 何時是一個檔案真正被刪除? - Problem - 刪除link但沒刪file - 刪除file但遺留link $\rightarrow$ dangling pointer - Good - 刪除檔案直到==reference counters is 0== ![](https://i.imgur.com/EUF5WEn.png) ## General-Graph Directory - 可能會有cycles - Reference count不再work - E.g. self-referencing file - 如何處理cycle - ==Garbage collection== 1. traverse整個圖和標記accessible files和directories 2. collect and free任何沒有被標記的 $\rightarrow$ 在百萬以上的檔案會有很差的performance - 當有link被create的時候,使用==cycle-detection algorithm== ![](https://i.imgur.com/Mjd9Nxv.png) ## 掛載(Mounting): :::success File System Mounting - 一個file system必須要被mount到目錄樹的目錄後,才能被行程存取 - file system must be mounted before it can be accessed A unmounted file - system is mounted at a mount point - In disk partition : to check the filesystem type and to find the superblock - In filesystem naming space : to specify the mounting point. ``` bash = e.g. mount –t ext4 /users /dev/hda1 ``` ::: :::info - ==Mount point== - the ==root path== that file system將被掛載的地方 - Mountg時機 - boot time - automatically at run-time - usb自動偵測 - manually at run-time ::: ![](https://i.imgur.com/UIwz6iRm.png) A # 四、File sharing ## Consistency Semantics : Consistency semantics specify how multiple users are to access a shared file ## Unix file system (UFS) implements : :::success Writes to an open file visible immediately simultaneously to other users of the same open file Sharing file pointer to allow multiple users to read and write concurrently ``` ex : 訂票系統 (互斥) ``` ::: :::info AFS has session semantics : Writes only visible to sessions starting after the file is closed ex : 報名表 Immutable(不可改變) semantic : ex: 公告文件 ::: :::info File sharing protection, Types of access : Read Write Execute Append (regards to disk space) Delete List ::: ## 控制用戶對檔案的權限 :::success chmod 是一條在 Unix 系統中用於控制用戶對檔案的權限的命令,由 9 個權限位來控制,每三位為一組,分別是 User, Group, Other,而每組內皆能控制 RWX (讀、寫、執行,r = 4, w = 2, x = 1, - = 0) 。 ::: :::warning 例如: - 所有者的權限用數字表達:屬主的那三個權限位的數字加起來的總和。如rwx ,也就是4+2+1 ,應該是7。 - 用戶組的權限用數字表達:屬組的那個權限位數字的相加的總和。如rw- ,也就是4+2+0 ,應該是6。 - 其它用戶的權限數字表達:其它用戶權限位的數字相加的總和。如r-x ,也就是4+0+1 ,應該是5。 - 則可以輸入以下指令 : chmod 765 myfile ::: # 參考資料 [PPT影片1](https://www.csie.ntu.edu.tw/~d92005/OS%202008/ch11.pdf) [wiki](https://zh.wikipedia.org/wiki/%E9%94%81_(%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%A7%91%E5%AD%A6) [教授](https://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/11_FileSystemInterface.html)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully