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
    # CH2 Service ## 一、中斷 (Interrupt) :::spoiler [TOC] ::: :::success 中斷是指處理器接收到外圍硬體或軟體的信號,導致處理器通過一個執行 context switch 並處理該事件。 ::: :::info Interrupt 的處理流程: 暫停目前 process 執行並保存此 process 當時執行狀況 OS 根據 Interrupt ID 查尋 Interrupt vector 並取得 ISR (Interrupt Service Routine) 起始位址 ISR 執行 執行完成,恢復先前 process 執行狀況 回到原先中斷前的執行 ::: ![](https://i.imgur.com/TZnpNJW.png) :::info 中斷的種類: - 外部中斷(External Interrupt): CPU 外的週邊元件所引起的。(I/O Complete Interrupt, I/O Device error) - 內部中斷(Internal Interrupt): 不合法的用法所引起的,CPU 本身所引發。(Debug、Divide-by-zero、overflow) - 軟體中斷(Software Interrupt):使用者程式在執行時,若需要 OS 提供服務時,會藉由 System Call 來呼叫 OS 執行對應的 ISR,完成服務請求後,再將結果傳回給使用者程式。 Trap = Internal Interrupt + Software Interrupt ::: :::warning **[補充] 現代電腦系統架構** - 每一個裝置控制器(device controller)負責一個特定型態的裝置。 - I/O 裝置和 CPU 可以同時執行並競爭記憶體。 - 每一個裝置控制器有自已的緩衝區。 - 發生一個事件時,由硬體或軟體產生中斷來通知。 - 近代的作業系統是中斷驅動 (Interrupt Driven) 。 ::: ![](https://i.imgur.com/jh3w612.png) ## 二、I/O 的運作方式 :::success 三種將資料在 I/O 間傳送的方法: - Polling:最簡單的方式讓 I/O 裝置與 CPU 溝通。I/O 裝置只要將資訊放進狀態暫存器,CPU 會周期性的檢查並取得資訊來得知需要服務的裝置。 Interrupt-driven I/O:利用 interrupt 的機制,當一個 I/O 裝置需要服務時,會發出 interrupt 來通知 CPU (非同步I/O)。 - DMA (Direct Memory Access):提供一個裝置控制器,讓 I/O 裝置能夠直接在記憶體做資料的傳輸,不需要 CPU 的參與。 - cycle stealing: DMA 向 CPU 竊用機器週期,而直接向記憶體存取資料,因此 CPU 可與週邊裝置的工作同時進行。萬一 CPU 與 DMA 對記憶體存取發生衝突,則給 DMA 較高的優先權 (執行消耗小的先執行)。 ::: :::info I/O 架構: - 同步 I/O 架構 (Synchronous):一段時間內只有一個 I/O 請求產生。 - 非同步 I/O 架構 (Asynchronous):必須有一個表以記錄各種裝置 I/O 的位址和使用狀況。 ::: ## 三、Dual-Mode :::success OS 會把影響重大的指令設為特權指令 (Privileged Instruction),以保護重要的資源,需要 Kernel mode 才能執行。Dual-Mode 需要硬體的額外支援,用一個 Mode Bit 記錄權限。 - User Mode (Mode Bit==1):一般模式 - Kernel Mode (Mode Bit==0):在此模式下才有權利執行特權指令 ::: ![](https://i.imgur.com/1LnJeps.png) :::info 特權指令的種類: 1. I/O指令 2. 與記憶體管理有關的暫存器之修改指令 3. 與 Timer 設定有關的指令 4. Enable/Disable 指令 5. 系統停止指令 6. 從 user mode 改變到 kernel mode 的指令 ::: ## 四、System Calls :::success Programming interface to the services provided by the OS. Mostly accessed by programs via a high-level API rather than direct system call use (because of portability and simplicity issue). ::: :::info Three most common APIs are : - Win32 API for Windows - POSIX API for POSIX-based systems (including virtually all versions of UNIX, Linux, and Mac OS X) - Java API for the Java virtual machine (JVM) ::: ![](https://i.imgur.com/S82J8M8.png) :::warning **[補充] System Call的參數如何傳遞給OS?** 用暫存器儲存這些參數。 將參數用表儲存在記憶體中,同時用一個暫存器記錄此表起始位址並傳給 OS。 利用 System Stack。要存放參數時將之 Push 到 Stack 再由 OS 從 Stack 中 Pop 取出參數。 ::: :::danger **[murmur]** Most users'view of the OS is defined by system programs, not the actual system calls (such as ls, cp, mv) ::: ## 五、OS 結構設計 :::info 1. **Simple structure**:早期的structure,例如MS-DOS - Pros : simple - cons : poorly structured. No protection. MS-DOS is left no choice because the CPU 8088 has no hardware support of user-kernel modes. ::: :::info 2. **Monolith**:開始分 kernel mode 和 user mode,例如 UNIX - Pros : efficient - Cons : not scalable ::: :::info 3. **Layered**:概念好但不存在,因為完全分層設計困難 - Pros : more structured, easy to debug and to scale - Cons : difficult to comply with, very hard to define layers ::: :::info 4. **Modules**:Most modern operating systems implement kernel modules, e.g. linux. ::: :::warning **Overall, similar to layers but with more flexible.** ::: :::info 5. **Microkernel**:核心功能能省則省,把很多功能都放到核外,例 如 Mach (Mac OS X) - Pros : Robustness, Scalability, and Security - Cons : inefficient. Because of message passing. ::: ## 六、虛擬機 :::success 虛擬機 (**Virtual Machine, VM**) 透過軟體模擬的機制,創造一份與底層硬體一模一樣的功能介面。對真正實際的系統而言,VM 上的 OS 只相當於一個執行中的 User Program,開發 OS 時較為安全且方便。 ::: :::info 優點: - 對於 OS 的測試與開發是一個很好的平台工具。 - 因為 VM 是孤立的,所以在OS的開發或測試過程中所引起的不當錯誤,也不會對實體系統造成危害。 - 測試 OS 可在 VM上執行,而其它 User Program 仍可在其它的 VM 上正常運行不會中止。 - VM 具有安全性。 - 同一部機器上可執行多套不同的 OS,是實作 time-sharing system 的簡單方法。 缺點: - 製作極為困難 - VM 之間非常的孤立,所以不易溝通及資源共享。 - 效能沒有真正的系統好 ::: ### 1. Instruction Type :::success - Privilege instruction:會進 trap 的指令 - Sensitive instruction:修改到硬體的指令 - Innocuous Instruction:Those that are not control or behavior sensitive ::: :::danger ==**[用心去感覺] Virtualizable**== If **sensitive instruction** a subset of **privileged instructions**, the VM can be build efficiently (**Virtualizable**). In **practice, Sensitive instructions** are patched with trap instructions. ::: ### 2. Java Virtual Machine :::success 高階語言如 Java,會先透過 Java compiler (javac),將 Java 程式會先被轉成跨平台的虛擬機器碼(byte-codes),再透過 Interpreter 便可在跨平台系統上執行。 ::: :::info **Just-in-time compiler** 將傳統 Interpreter 替換成 JIT compiler,當 Java 執行 runtime 環境時,即時編譯器(混合靜態編譯與動態直譯)會針對程式熱區(hotspot)進行編譯優化成精簡的原生型指令碼,然後保存在記憶體中加速執行。 :::

    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