Cody Chen
    • 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
    • Make a copy
    • 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 Make a copy 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
    OS === ###### tags: `關於想成為SRE學習路上的那些趣事` # Syllabus # Ch01 : Introduction ## 什麼是作業系統? * 簡單來說是使用者與電腦硬體間的溝通媒介 * 目的: * 執行使用者的程式並協助解決使用者的問題 * 讓電腦系統易用性提高 * 有效使用電腦硬體 * 功能: * 資源分配者 * 監控使用者執行的程式,防止錯誤或不當使用行為 ## 電腦系統架構(組織) 1. 硬體 2. 作業系統: * 根據使用需求有不同設計模式,像是個人電腦的OS與嵌入式系統的OS設計就不同 * 面向使用者需求,因此multi-programming/multi-tasking的發展逐漸成熟 * 可以簡單分為Kernel跟系統程式(system program)、應用程式(application program) 3. 應用程式 4. 使用者 * 補充:開機程式(bootstrap program) ## Interrupt(中斷事件) * 概述:硬體發送訊號給CPU(通常透過系統匯流排,也就是system bus)時,會觸發**中斷**事件;當CPU被中斷時,會立即停止正在執行的程序(process),並把執行權移交處理中斷事件。中斷服務工作(Interrupt service routine, ISR)執行完成後,CPU會回到被中斷的運算位置重新運作。 * 如下圖: ![](https://i.imgur.com/lD6G8BN.png) * 中斷事件發生時,其位址(address,指向中斷事件發生的位址)會被記錄在陣列內(可以視為array of pointers),並存放在low memory。要記錄其位址才能讓OS跟CPU知道要去哪個記憶體位置執行事件。 * 實作:詳細解釋中斷事件時作業系統與各電腦元件做的事 * CPU硬體上有實體的wire,稱為interrupt-request line,在執行每個instruction後用來偵測中斷事件,當在line上讀取到interrupt number時(代表有中斷事件發生),CPU根據該number移交執行權給interrupt-handler routine處理對應的中斷程序 * interrupt-handler根據interrupt vector中的index處理對應的中斷事件,處理過程中會記錄任何操作造成的狀態變更、決定中斷造成的影響並執行對應運算;最終執行`return_from_interrupt` instruction return給CPU。 * **可以總結為:device controller _raises_ an interrupt by asserting a signal on the interrupt request line, the CPU _catches_ the interrupt and dispatches it to the interrupt handler, and the handler _clears_ the interrupt by servicing the device.** * ![](https://i.imgur.com/DFZPU5T.png) ## Storage Structrue * CPU只能從記憶體中讀取instructions,所以程式要執行必須先載入到記憶體中。以一般電腦來說,記憶體指的是主記憶體或隨機存取記憶體(RAM),通常以半導體製成,稱為動態隨機存取記憶體(DRAM)。 * RAM是揮發性的,斷電資料即消失。 * 開機程式(bootstrap program)存放在EEPROM(electrically erasable programmable read-only memory)以及韌體上;上述兩種儲存裝置不常被寫入、非揮發性且讀寫速度較慢,因此主要存放不常使用的資料或程式,例如裝置序號或硬體資訊等。 - http://wiki.csie.ncku.edu.tw/embedded/Flash ## I/O Structure # CH02 : System Structures ## 作業系統的設計與實作 0. 如何設計: * 視需求而定 * 視硬體而定 * 基本上分為兩面向,從user goal或是system(developer)goal出發。也因此可以發現不同的需求決定不同的作業系統型態,例如即時系統、嵌入式系統與常見的Windows作業系統設計方式就大相逕庭。**沒有特定解答,因此掌握下列的設計原則就相當重要**。 2. 原則 * 關注點分離 * Policy: What will be done? * Mechanism: How to do it? 3. 實作 * 早期多以組合語言 * 中期以系統程式語言如Algol, PL/1為主 * 現在以C/C++為主流 * 實際上多由各種語言組成,視需求決定 * 底層:組合語言 * 主體:C * 系統程式:C/C++, PERL, Python, Shell Script等 * Emulation允許OS執行非原生之硬體 4. 架構 * 並沒有良好的分離式設計 # CH03 : Process Concept - 正在執行中的program就稱為process,一個process需要一定的資源,包括CPU time, 記憶體, 檔案, I/O裝置來幫助其完成任務。當process被執行時,上述資源才會被分配給process。 - OS執行systme code, 使用者執行user code。可以併行(concurrently)執行。 - 重點: - Process概念 - Process的幾種特性、功能,包括排程、creation & termination、溝通模式 - Process間的溝通模式 : shared memory和message passing - 在client-server系統中的溝通模式 ## Process概念 - Process的狀態會被記錄在program counter的值和processor的registers內。 - Process的記憶體layout通常被區分成下列幾個sections: - Text : 可執行的程式碼區段 - Data : 全域變數 - Heap : 隨program執行而動態分配的記憶體空間 - Stack : 暫時性的資料儲存位置(例如觸發function時的parameters、return address、local變數等) - 如圖示 ![](https://i.imgur.com/GT0y6Tl.png) - text和data的大小固定,不隨程式執行改變;heap和stack則是動態成長或減少。OS會確保head和stack的記憶體區塊不重疊。 - Program(passive) v.s. Process(active) - Program是一種檔案,包含instructions並儲存在硬碟 - Process則是執行中的程序,program counter會指定下一個要執行的instruction以及所需要的系統資源組合。 - Process可以是另一個process的執行環境,例如:Java program在JVM(Java Virtual Machine)上執行。 ### Process State - Process隨執行改變其狀態,其狀態包含: - New : 被create - Running : instruction正在被執行 - Waiting : 等待其餘事件完成(例如:I/O事件或signal的接收) - Ready : 等待被分配給processor - Terminated : 完成執行 ![](https://i.imgur.com/orpp75n.png) - **一個processor core(核心)一次只能執行一個process,所以總是會有很多processes處在ready和waiting的狀態。** ### Process Control Block - 在OS,各個process由PCB來表示,PCB記錄特定process的資訊,包含: - Process state : new/ready/running/waiting/halted等 - Program counter : 指定下個instruction的位址 - CPU registers : 視電腦架構而定 - CPU-scheduling information : 記錄Process優先度、排隊pointer、排程參數(params) - Memory-management information - Accounting information - I/O status information ### Threads - 目前為止討論的process model都是用**process是以single thread執行的program**來描述與討論 - 現代OS通常已允許一個process可用multiple threads執行方式 - 可以平行執行(can run in parallel) - PCB擴展為記錄多個threads的資訊 - Ch04會更詳細介紹threads的概念 ## Process排程 - 原則 : 一個核心僅能執行一個process - 當process數多於core數,勢必有process要等待有core執行完上一個process並重新排程 - 當前在記憶體中的process數為degree of multiprogramming - 為平衡multiprogramming和time sharing的需求,考慮process的特性去排程 - I/O-bound:花更多時間做I/O - CRU-bound:花更多時間於CPU運算 ### Scheduling Queues - 當process進入排隊,首先會被放在ready queue中 - ready queue通常以linked list的方式儲存,linked list的header指向list中的第一個PCB,每個PCB皆包含一個指向下個PCB的pointer。 - Process在執行時有可能因下列原因中斷: - I/O request : 放回I/O wait queue - Create a new child process : 放回wait queue直到child termination - Be interrupted : 在interrupt wait queue中等待interrupt事件結束,再回到ready queue - Time slice expired : 強制放回ready queue ### CPU Scheduling (more on 9th edition) - 又稱為**short-term scheduler**,至少每 100 ms (通常更頻繁,因為有 I/O-bound process)執行一次,選擇下一個執行的 process 。 - 另外,**long-term scheduler** (job scheduler) 選擇哪個 process 要進入 ready queue ,通常每秒甚至分鐘執行一次,可調配 degree of multiprogramming - Swapping的概念 : 當記憶體位置不足時,會將 process 記錄當前狀態並移除(swapped out),記憶體有足夠空間時可重新讀入記憶體中(swapped in)。由**medium-term scheduler執行之** ### Context Switch - 使多個process可分享單一CPU運算資源的方法。 - 當 CPU Context Switch 到另一個 process,系統會儲存舊 process 的狀態並載入新 process 的狀態。Context switch 的時間是 overhead,耗時根據硬體配備而不同,OS或PCB越複雜耗時越多。 - 交換時機有下列幾種: - 多工 - interrupt - user-kernel and system kernel switch ![](https://i.imgur.com/iN9NdGY.png) ## Process的操作 - 多數系統中process可並行運行,因此常會動態 created 或 deleted - 系統必須提供 creation 和 termination 的機制 ### Process Creation - New process時,兩種可能的address-space分配 1. child process 是 parent process 的複製 : 和parent相同 2. child process 有其他的program載入 - New process時,兩種可能的執行方式 1. parent 和 child 同時執行 2. parent 等 child 執行完成 - System call - `fork()` : create a new process - `exec()` : after fork(), replace the process' memory space with a new program ### Process Termination 1. child 執行完所有 instruction 後,call `exit()` 要OS (parent) 回收自己,parent call `wait()` 2. parent call `abort()` 終止執行中的child process(原因:可能是 child 使用超過初始分配的資源上限) 3. parent 比 child 先終止,child可能由 OS 接收 (init/systemd) 或是 cascading termination - Orphan & Zombie - Orphan : 當parent terminate 但 child 還沒 -> 變orphan -> 由系統接收或一併終止 - Zombie : parent還沒call `wait()` -> child 變 zombie (直到 parent call `wait()`) ## Process間的溝通方式 - Shared memory - Message passing ## IPC系統範例 ## Client-Server系統溝通

    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