Kipper
    • 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
    # OS筆記-Chapter 13: I/O Systems ###### tags: `OS` --- #### 目錄 * 總論 [Chapter 1: Introduction](https://hackmd.io/NoZq3J7IQvOQpcbo_tctjA) [Chapter 2: Operating-System Structures](https://hackmd.io/OKykRLBESI6v9a13HgS35A) * 行程管理 [Chapter 3: Processes](https://hackmd.io/HOqN-iQ3RIKIC-NB9QjBIQ) [Chapter 4: Threads](https://hackmd.io/qzAIHeSASmKuecdkqidmHw) [Chapter 5: CPU Scheduling](https://hackmd.io/IT5g2wHzTdOtMSDXPVEpOw) [Chapter 6: Process Synchronization](https://hackmd.io/rv-PNe3ESxi08PElyUTc4Q) [Chapter 7: Deadlocks](https://hackmd.io/Uu0jDK-rSyKNKq690y146g) * 記憶體管理 [Chapter 8: Main Memory](https://hackmd.io/4KS_yPkBQzGZfHDisPciog) [Chapter 9: Virtual Memory](https://hackmd.io/yirxZFn8Rz2wT56AAR7Sxw) * 儲存裝置 [Chapter 10: File-System Interface](https://hackmd.io/aNPWKsFhTlGc-WFgQ__KRg) [Chapter 11: File System Implementation](https://hackmd.io/bFcrlmefQsGp6hZdbI1MHQ) [Chapter 12: Mass-Storage Systems](https://hackmd.io/9Y7Qo0OERda6htK7OOI36Q) <font color="red">Chapter 13: I/O Systems</font> * 保護和安全 [Chapter 14: Protection](https://hackmd.io/izkd4JwXRwub_ZmhSMTlNw) [Chapter 15: Security](https://hackmd.io/ofyvDidvQf-PxLMMZYhtsg) --- ### 概觀 * I/O裝置在功能與速度方面變化極大(滑鼠、硬碟、光碟機),因此需要許多不同的功能來控制 * I/O裝置目前存有兩個相互衝突的方展方向 * 軟體與硬體介面逐漸趨於標準化,有助於我們將裝置整合至電腦 * 更多I/O裝置出現,有些新裝置與舊裝置差異極大,將裝置整合至電腦變的很有挑戰 * 裝置驅動程式(device driver):代表一個與I/O子系統相通知統一裝置存取介面 ### I/O硬體 * 阜(port):裝置和機器藉此互通訊息 * 匯流排(bus):由一組纜線組成,並使用嚴謹定義之協定,規定一組可在纜線上傳送的訊息組成 * 菊花鏈(daisy chain):裝置A有纜線連接裝置B,裝置B有纜線連接裝置C,裝置C有纜線連接電腦阜,通常以匯流排方式操作 * PCI匯流排(PCI bus):負責處理處理器-記憶體(processor-memory)子系統與快速裝置的連接 * 擴充匯流排(expansion bus):則連接像鍵盤及序列阜、USB阜類的慢速裝置 * SCSI(Small Computer System Interface)匯流排:最大部份的應用是在儲存裝置上 * PCIe(PCI Express):吞吐量達到每秒16GB * HyperTransport:吞吐量達到每秒25GB ![](https://i.imgur.com/Q49ElJ0.png) * 控制器(controller):可操控連接阜、匯流排或裝置的電子零件,擁有一個或多個暫存器 * 處理器如何完成I/O * 使用特殊I/O指令來傳輸要送往連接阜位址的資料 * 讓控制器暫存器支援記憶體映對I/O(memory-mapped I/O) * PC的I/O連接阜位址 ![](https://i.imgur.com/SpftEqy.png) * I/O連接阜包含四個暫存器 * 資料輸入暫存器(data-in register):讀取輸入 * 資料輸出暫存器(data-out register):由主機寫入要輸出資料 * 狀態暫存器(status register):包含可被主機讀取的位元資料,這些位元指出是否可讀、裝置錯誤的狀態 * 控制暫存器(control registe):用以起始指令或改變裝置模式 * 輪詢(polling)/忙碌等待(busy-waiting) * 使用兩個位元來協調控制器與主機 * 控制器:忙碌(busy)位元 * 主機:指令就緒(command-ready)位元 * 握手程序: 1. 主機重複讀取Busy位元,直到位元值被清除,表示控制器可用 2. 主機將位元組寫入data-out暫存器,且設定command暫存器中的指令 3. 主機設定command-ready位元 4. 當控制器發現command-ready位元已經設定,即設定busy位元 5. 控制器讀取指令暫存器並發現指令,執行I/O 6. 控制器清除command-ready位元,並清除狀態暫存器中的錯誤位元,表示I/O成功;再清除忙碌位元以便表示動作完成 * CPU為了完成I/O動作。一再重複輪詢各裝置,顯得沒有效率 * 中斷(interrupt) * 允許裝置通知CPU * 中斷要求管線(interrupt-request line):CPU硬體擁有的纜線,控制器發出中斷要求至此 * 中斷處理器常式(interrupt-handler routine):CPU捕捉中斷並分派給中斷處理器,中斷處理器執行必要的處理,並自中斷返回 ![](https://i.imgur.com/dJrhrRF.png) * 中斷控制器硬體提供三項特點 * 在關鍵處理(critical processing)的時刻,我們需要延遲中斷處理器 * 為裝置分配到適當的中斷處理器,而不需要先查詢所有裝置,以便得知是哪個裝置引發中斷 * 需要多層(multilevel)中斷,使得有高低優先權 * 大部分的CPU提供兩種中斷要求管線 * 無遮罩中斷(nonmaskable interrupt):最高優先權的中斷,不會被其它的中斷打斷 * 遮罩中斷(maskable interrupt):使得某些中斷雖然具備了中斷條件,但仍不會進行處理動作,只將中斷的來源保留,直到遮罩解除後,才去回應原先的中斷 * 大部分的架構中,中斷機制接受一個位址,此位址為中斷向量(interrupt vector)表格的偏移位址 * 中斷向量的目的為不需搜尋所有中斷發生的可能來源 * 中斷串鏈(interrupt chain):電腦擁有遠較中斷向量表中多的裝置,使用中斷串鏈指向中斷處理器串列之前端 ![](https://i.imgur.com/x5B5R5V.png) * 直接記憶體存取(DMA,Direct Memory Access) * 用來避免一次一位元組的程式化I/O(programmed I/O) * CPU將DMA指令區段寫入記憶體,DMA控制器直接操作記憶體匯流排,不需要CPU的輔助即可執行傳送 * DMA控制器與裝置控制器之間握手程序 * DMA-要求(DMA-request):裝置控制器發出要求 * DMA-確認(DMA-acknowledge):DMA控制器發出 * 裝置控制器接收到DMA-確認後,將資料傳輸到記憶體,清除DMA-要求 ![](https://i.imgur.com/uiZc094.png) ### 應用I/O介面(Application I/O Interface) * 介面(interface):將一般性特徵由一組標準化功能加以存取 ![](https://i.imgur.com/eV7Qv7R.png) * 每種作業系統都有自己適用的裝置驅動器介面 * 一個特定裝置可被多種裝置驅動器驅動 ![](https://i.imgur.com/BYur2BG.png) * 大部分的作業系統有跳脫(escape)/後門(backdoor)可以原封不動的將來自某應用程式之任意指令傳給裝置驅動器 * 區段與字元裝置 * 區段裝置介面(block-device interface) * 包括存取磁碟機與所有區段導向裝置 * 原始I/O(raw I/O):將區段裝置當作簡單的線性區段陣列使用 * 直接I/O(direct I/O):允許作業系統在不能緩衝和上鎖的檔案系統操作 * 字元串列介面(character stream interface) * 這介面的基本系統呼叫可以讓應用程式取得或輸出一個字元 * 允許使用緩衝與編輯服務建立一次存取一行的程式庫 * 如:滑鼠、鍵盤 * 網路裝置(Network devices) * 插座(socket)介面:可以與遠端位址相連 * 時鐘與計時器(Clocks and Timers) * 三種基本功能的時鐘與計時器 * 紀錄目前時間(current time) * 記錄經過時間(elapsed time) * 設定計時器,在時間T啟動操作X() * 可程式化計時器(programmable interval timer):測量經過時間和觸發操作的硬體 * 阻隔與非阻隔I/O(Blocking and Nonblocking I/O) * 阻隔系統呼叫:懸置應用程式,直到系統呼叫完成 * 非阻隔系統呼叫 * 透過多執行緒(multithread)實現 * 快速傳回資料值,指出已經有多少位元組傳輸成功之資訊 * 同步(synchronous)與非同步(asynchronous) * 非同步:不須等待I/O完成即可立刻回傳 ![](https://i.imgur.com/TqLjL6e.png) * 向量I/O(vectored I/O) * 允許一個系統呼叫可以對多個位置執行I/O操作 * 分散-集中的方法(scatter-gather) ### 核心I/O子系統(Kernel I/O Subsystem) * I/O排班程式 * 找出好的順序以便執行 * 當核心支援非同步I/O時,必須能同一時間記錄許多I/O需求 * 裝置狀態表(device-status table) ![](https://i.imgur.com/AlmMbxK.png) * 緩衝(buffer) * 使用緩衝的原因 * 為了解決生產者和消費者速度不相等 ![](https://i.imgur.com/85Ek80h.png) * 在不同資料傳輸大小的裝置之間做調整 * 提供應用I/O的複製語法 * 快取(cache) * 持有資料的快速記憶體 * 緩衝與快取的不同 * 緩衝器可能只有持有資料項的現在備份 * 快取是持有一個存在於其他位置之項目的複製在快速儲存體 * spooling 和裝置預約 * spoolilng 是一個用來保留裝置輸出的緩衝器 * 它不能接收交錯的資料串列 * 作業系統採用攔截輸出的方式來解決 * 裝置預約(Device reservation) * 提供對設備的互斥存取 * 必須注意死結問題 * 錯誤處理(error handle) * 作業系統可以從錯誤中回復 * 大部分I/O送回一位元的資訊,以便說明此呼叫的執行狀態為成功或失敗 * 系統錯誤日誌(error log)記錄錯誤訊息 * I/O保護(I/O Protection) * 為了避免使用者執行非法I/O操作,我們定義所有I/O指令為特權指令,必須由作業系統發出 ![](https://i.imgur.com/hau9PdV.png) * 任何記憶體映對和I/O阜記憶體位置必須被保護 * 核心資料結構(Kernel Data Structures) * 開啟檔案紀錄,包含依其檔案類型指向適當常式的指標分配表 ![](https://i.imgur.com/Yjqncxn.png) ### 轉換I/O要求為硬體操作指令(I/O Requests to Hardware Operations) * 考慮一個行程從磁碟讀取一個檔案 * 判斷檔案存在 * 轉換檔名到裝置位址 * 讀取磁碟上資料到緩衝 * 使資料能被行程使用 * 將控制權交回行程 * I/O要求的生命週期 ![](https://i.imgur.com/JkY1GtU.png) ### STREAMS * 讓應用程式能夠動態組合驅動程式之程式碼的管線(pipeline) * 包含資料串列標頭(stream head)、驅動器尾端(driver end)、串列模組(stream module) * 每一個模組都包含一個寫入佇列與讀取佇列 * 佇列可支援流量控制(flow control) * 訊息傳送被用來在佇列間傳輸資料 ![](https://i.imgur.com/JTF9HL5.png) * STREAMS I/O是非同步 * STREAMS 提供一個架構以模組化漸進式的方法來撰寫裝置驅動模式和網路協定 ### 性能(Performance) * I/O是影響系統性能的主要原因 * CPU執行裝置驅動程式 * 核心I/O * 因中斷而需要的內容轉換 * 必須複製的記憶體資料 * 網路上的交通量 * 電腦內部通訊 ![](https://i.imgur.com/Lwmnf3q.png) * 改進I/O之執行效率 * 減少內容轉換 * 減少在裝置與應用程式間傳輸時,必須複製的記憶體資料 * 藉由使用大量傳輸、智慧型控制器、輪詢,降低中斷發生頻率 * 使用DMA * 將處理的基本功能一道硬體之中 * 平衡CPU、記憶體子系統、匯流排與I/O之性能 * 應用程式層開發彈性較高,但內容轉換可能造成額外的負擔 * 硬體層彈性較低,增加了開發成本 ![](https://i.imgur.com/Xd9LOsz.png)

    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