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 New
    • 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 Note Insights Versions and GitHub Sync 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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Chapter 13 – I/O Systems :::danger :::spoiler [TOC] ::: # Overview - 計算機的兩個主要工作 - I/O and Computation - ==I/O devices== - tape,HD,mouse,joystick搖桿,network card, screen,flash disks,etc - ==I/O subsystem== - 控制所有I/O devices的方法 - Two conflicting trends - 統一(unified)及標準的HW/SW interfaces - I/O devices有很多種類 - Device drivers - a uniform device-access ==interface to the I/O subsystem== - 在OS最下層要有個driver layer - 相似於apps與OS之間的==system calls== - Device 分類 -- 不同類型去設計不同的interface - Storage devices:disks, tapes - Transmission devices:network cards, modems - Human-interface devices:keyboard, screen, mouse - Specialized devices:joystick, touchpad # I/O Hardware ## Port - A ==connection point== between I/O devices and the host - E.g.:USB ports ## Bus - 一組==wire==和定義好的==protocol==規定messages如何透過這些wire做傳送 - E.g.:PCI bus ## Controller - A collection of electronics可以操控port,bus,device - controller可以有他自己的==processor和memory==, etc. (E.g.: SCSI controller) ## Typical PC Bus Structure - 北橋晶片控制比較快的controller,bw、freq高 - 南橋晶片接傳統ide,比較慢的 ![](https://i.imgur.com/ki5HrHq.png) # I/O Methods ## Port-mapped I/O - 每個I/O port (device)被指定一個獨特的==port address==,一開始就燒死與軟體無關 - 每個I/O port由==四個registers (1~4Bytes)組成== - Data-in register:read by the host to get input - Data-out register:written by the host to send output - Status register:read by the host to check I/O status - Control register:written by the host to control the device - Program與I/O port的互動透過==special I/O instructions== - i/o instruction裡頭一定有port address ![](https://i.imgur.com/wZLaycS.png) ## I/O Methods Categorization - Depending on ==how to address a device== - ==Port-mapped I/O== - 與memory用不一樣的address space - 透過special I/O instruction (e.g. IN, OUT)做存取 - ==Memory-mapped I/O== - 保留特別的memory space給device - 透過標準的data-transfer instruction (e.g. MOV)做存取 - 寫到memory等於寫到某個device的buffer,要事先register好 - 優點:對大量的memory I/O是更有效率的(e.g. graphic card) - 缺點:Vulnerable to accidental modification, error - Depending on ==how to interact with a device== - ==Poll (busy-waiting)==:processor週期性的檢查device的status register - ==Interrupt==:device通知processor I/O的完成 - 若是資料量大,選擇用interrupt - Depending on ==who to control the transfer== - ==Programmed I/O==:transfer由CPU控制 - CPU會一直接收到interrupt,速度變慢 - ==Direct memory access== (DMA) I/O:由DMA controller控制(一個有特別目的的controller) - 由I/O controller負責搬資料,不屬於任何一個I/O device - 設計給大量資料傳輸 - 通常搭配使用memory-mapped I/O和interrupt I/O method - ![](https://i.imgur.com/5bCUyVQ.png) ## Interrupt ### Interrupt Vector Table Intel Pentium Processor ![](https://i.imgur.com/6sZkA0N.png) ### Interrupt Prioritization - Maskable interrupt - 參照IRQ - 透過設定中斷遮罩暫存器(interrupt mask register; IMR)內的位元遮罩(bit-mask)值來==讓它可被系統忽略== - 比當前較低優先度的interrupt不被認定直到pending interrupt完成 - Non-maskable interrupt (NMI):highest-priority, never masked - ==無法被系統忽略的== - NMI通常使用於計時器(timers) - Often used for power-down, memory error ### Interrupt-Driven I/O ![](https://i.imgur.com/DFdbC2V.png) ## CPU and device Interrupt handshake 1. Device asserts ==interrupt request (IRQ)== 2. CPU在每個instruction cycle的開始檢查==interrupt request line== 3. ==Save== the status and address of ==interrupted process== 4. CPU認知道了interrupt,接著在interrupt vector table搜尋interrupt handler routines 5. CPU從==interrupt handler routine==抓下一個 instruction 6. 在完成執行interrupt handler routine後恢復interrupted process ## Note - PnP(Plug and Play)技術可以協助解決 IRQ 線路衝突的問題 - PnP,簡化來說,就是讓周邊設備自己發出訊號去和所有其他支援 PnP 設計的周邊設備**協商,找到尚未被佔據的 IRQ 線路** - USB 使得許多種不需要太快傳輸速度的周邊設備,例如:鍵盤、滑鼠、印表機、數據機、軟碟機、掃瞄機等等 (注意硬碟機不在此列,因為硬碟機需要越快越好的傳輸速度), 可以串聯在一起,而且共用一個 IRQ 線路 # Kernel I/O Subsystem ## I/O Subsystem - I/O Scheduling - 藉由對I/O queue中的job排順序提昇系統效能 - e.g. disk I/O order scheduling - Buffering - 當要在I/O devices之間做transfer,將data存到memory(e.g. disk to network card) - 調節I/O devices之間的速度 - 原因 - devices之間的==速度不一樣== - Devices with ==different data-transfer sizes== - Support copy semantics - 要做多次的data copy - Caching – fast memory that holds copies of data - 永遠只是個copy - Key to ==performance==,增加access的速度 - Spooling – holds **output** for a device - 先load到spooling的空間去,再一次送到printer - e.g. printing (cannot accept interleaved files) - Error handling – when I/O error happens - e.g. SCSI devices returns error information - I/O protection - Privileged instructions ## UNIX I/O Kernel Data Structure - Linux treats all I/O devices like a ==file== - ![](https://i.imgur.com/JbSBrrA.png) ## Device-status Table ![](https://i.imgur.com/svWH4h9.png) ## Blocking and Nonblocking I/O - Blocking - process被中斷直到I/O完成 - Easy to use and understand - Insufficient for some needs - Use for ==synchronous== communication & I/O - Nonblocking - 透過==multi-threading==實作 - Returns quickly with count of bytes read or written - Use for ==asynchronous== communication & I/O - e.g. call interrupt告訴你他做完了,有write, write call為一組 ## Life Cycle of An I/O Request ![](https://i.imgur.com/PoTFItF.png) # Performance ## I/O在系統效能上是一個主要的因素 ### 原因 - 讓CPU執行==device driver code== - 每call一層driver,每一個subsystem都是不同的process,需要==context switches== - 因為有一堆layer,controllers、buffer、 cache有一堆的==data copy== - ==Interrupt handling== is a relatively expensive task - 當I/O的時間少,Busy-waiting可以比interrupt-driven表現的更以效率 ## Intercomputer Communications - Network traffic could cause high context switch rate - Interrupt generated during keyboard & network I/O - Context switch occurs between prog. & kernel (drivers) - ![](https://i.imgur.com/a78aIQS.png) ## Improving Performance - 減少==context switches==的次數 - 減少==data copying== - 藉由large transfers,smart controllers,polling減少==interrupts== - Use ==DMA==,CPU可以空出來 - Balance CPU,memory,bus,and I/O performance for highest throughput # Application Interface ## A Kernel I/O Structure ### Device drivers - a uniform device-access ==interface to the I/O subsystem== - ==hide the differences== among **device controllers** from the **I/O sub-system of OS** - ![](https://i.imgur.com/uwjTRFM.png) ## Characteristics of I/O Devices ![](https://i.imgur.com/RG9y1k8.png) ## I/O Device Class - Device class is fairly standard across different OS - Block I/O - Char-stream I/O - Memory-mapped file access - Network sockets - Clock & timer interfaces - ==Back-door== interfaces (e.g. ioctl() ),難以歸類到以上的類別 - call system calll:ioctl()完後,可以去操作任何的I/O device - 使application可以任意access被device driver實作函式,而不用透過system call ## Block & Char Devices - Block devices:disk drives - system calls:read( ), write( ), seek( ),一整個memory buffer的概念 - Memory-mapped file can be layered on top - 大的,用interrupt做I/O - Char-stream devices:mouse, keyboard, serial ports - system calls:get( ), put( ),以bytes為單位 - Libraries layered on top allow line editing - 小的,用pulling做I/O ## Network Devices - 有很多種形式(block、character...),對於個別有自己的interface - System call: send(), recv(), select() - select() returns which socket is waiting to send or receive, eliminates the need of busy waiting - Many other approaches - pipes, FIFOS, STREAMS, message queues

    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