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 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
    1
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # OS筆記-Chapter 2: Operating-System Structures ###### tags: `OS` --- #### 目錄 * 總論 [Chapter 1: Introduction](https://hackmd.io/NoZq3J7IQvOQpcbo_tctjA) <font color="red">Chapter 2: Operating-System</font> * 行程管理 [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) [Chapter 13: I/O Systems](https://hackmd.io/VNwXrhJPSo-l_t9tUBhYIg) * 保護和安全 [Chapter 14: Protection](https://hackmd.io/izkd4JwXRwub_ZmhSMTlNw) [Chapter 15: Security](https://hackmd.io/ofyvDidvQf-PxLMMZYhtsg) --- ### 作業系統服務(Operating System Services) * 作業系統提供執行程式的環境和對使用者有幫助的功能 * 使用者介面(UI,User Interface): * 命令行介面(CLI,Command-Line interface):使用文字命令和輸入指令的方法 * 批次介面(batch interface):控制命令的命令和指示輸入檔案中,並執行那些檔案 * 圖形使用者介面(GUI,Graphics User Interface):最普遍使用的 * 程式執行(Program execution):系統必須把程式載入記憶體並且執行。該程式必須有能力去結束本身的執行工作,無論是以正常或不正常的方式(指出錯誤) * I/O 作業(I/O operations):為了效率和保護,使用者通常無法直接控制I/O裝制,因此作業系統心須提供一些完成I/O工作的方法 * 檔案系統的使用(File-system manipulation):程式需要讀寫檔案和目錄,創建和刪除它們,搜尋它們,列出檔案資料,權限管理 * 通信(Communications):行程與行程交換資訓,可以藉由共用記憶體(shared memory)或訊息傳遞(message passing)。在訊息傳遞的方式中,預設格式的訊息封包(packet)經由作業系統在行程間移動 * 錯誤偵測(Error detection):作業系統必須不斷的偵側和錯誤,對於任何一類的錯誤,作業系統都應該採取適當的行動,以確定正確且持續的運算 * 資源分配(Resource allocation):當多個用戶或多個作業同時運行時,必須電腦資源分配給它們 * 記帳(Accounting):記錄哪些使用者使用了多少的電腦資源 * 保護和安全(Protection and security):在多使用者系統或網路電腦系統中存放資料的人可能希望控制該資料的使用。當一些互無關聯的行程同時執行時,行程不該互相干擾 * 保護確保所有系統資源的存取都在控制之中 * 安全性要求每一個使用者需要經過系統認證(authentication) ### 使用者與作業系統介面(User Operating System Interface) * 命令直譯程式(command interpreter): * 命令行列(CLI,Command-Line interface)或命令直譯程式允許使用者直接的鍵入命令 * 有多個命令直譯程式可選擇的系統上,這些命令直譯程式稱為外殼(shells) * 命令直譯程式的主要的功能是取得下一個使用者所指定的命令並執行它 * 命令的制做方法有兩種: 1. 命令直譯程式本身含有執行命令的程式碼 2. 使用命令來指定一個要載入記憶體及執行的檔案,例如:"rm file.txt",會去尋找rm這個檔案,將它載入記憶體,並將參數file.txt傳給它 * 第二種的命令直譯程式可以常小,增加新命令時也不必更改程內容 * 圖形使用者介面(GUI,Graphics User Interface): * 以滑鼠指標定位在桌面(desktop) * 按下圖像(icon)可以呼叫對應的程式、檔案或目錄 * 觸控螢幕則使用手勢(gesture) ### 系統呼叫(System Calls) * 系統呼叫提供一個由作業系統服務的介面,這類呼叫一般以C或C++寫成,通常系統每秒執行成千的系統呼叫 ![](https://i.imgur.com/sBBr8Wt.png) * 應用程式介面(Application Program Interface):指定一組可用的函數給應用程式開發人員設計程式 ![](https://i.imgur.com/5kHIxlT.png) * 為什麼應用程式設計師偏愛依照API寫程式而非呼叫真正的系統呼叫? * 增加程式可攜性,程式在任何支援相同API的系統上執行 * 真實的系統呼叫比API更繁雜與難以使用 * 系統呼叫介面(systen-call interface): * 扮演著作業系統提供之系統呼叫的連結 * 每個系統呼叫都與一個數字相關聯,而系統呼叫介面則根據這些數字去維護一個索引表格 * 系統呼叫介面啟動作業系統核心中指定的系統呼叫,並傳回系統呼叫狀態和回傳數值 ![](https://i.imgur.com/x3xgipX.png) * 傳遞參數至作業系統有三種方法: 1. 儲存於暫存器 2. 儲存在記憶體,再將區段位址存於暫存器 3. 置於堆疊(stack) ### 系統呼叫的類型(Types of System Calls) * 系統呼叫可概略的分成六大類: * 行程控制(process control) * 檔案的管理(file manipulation) * 裝置的管理(device manipulation) * 資訊的維護(information mainranace) * 通信(communication) * 保護(protection) ![](https://i.imgur.com/Qu33WPt.png) * 標準C程式庫範例 ![](https://i.imgur.com/oFAng78.png) * 行程控制(process control) * end, abort * load, execute * create process, terminate process * get process attributes, set process attributes * wait for time * wait event, signal event * allocate and free memory * 在正常或不正常的環境下,作業系統必須將控權轉移給命令直譯程式。然後,命令直譯程式讀取下一個命令 * 當錯誤時,將記憶體傾印(dump)出來 * 偵錯程式(dbugger):幫助找出缺陷(bug) * 鎖(locks):為了確保資料的完整,鎖住共享的資料,直到鎖被開啟 * 單工系統: ![](https://i.imgur.com/Rcm2V5u.png) * 多工系統: ![](https://i.imgur.com/syWRYBt.png) * 檔案的管理(file manipulation) * create file, delete file * open, close file * read, write, reposition * get and set file attributes * 裝置的管理(device manipulation) * request device, release device * read, write, reposition * get device attributes, set device attributes * logically attach or detach devices * 資訊的維護(information mainranace) * get time or date, set time or date * get system data, set system data * get and set process, file, or device attributes * 許多系統提供傾印(dump),對除錯非常有幫助 * 程式追蹤(trace)可列印每一個正在執行的系統呼叫 * 單步(single step):每個指令後,cpu都會執行一個中斷 * 通信(communication) * create, delete communication connection * send, receive messages * transfer status information * attach and detach remote devices * 訊息傳遞(message-passing): * 進行通信前,必須先開啟連接管道 * 每部電腦都有一個大家知道的主機名稱(host name) * 每個行程都有一個行程名稱(process name) * 大部份接收連接的行程都是特定用途的守護行程(daemons) * 通信來源稱為用戶(client) * 接收通信稱為伺服器(server) * 共用記憶體(shared-memory): * 必須確保行程不會對同一區域進行寫入 * 訊息傳遞較共用記憶體容易製作 * 但共用記憶體有較好的速度及便利性 * 保護(protection) * Control access to resources * Get and set permissions * Allow and deny user access ### 系統程式(System Programs) * 又稱為系統常式(system utility),提供一些程式開發與執行的便利環境,有些只是使用者的系統呼叫之間的介面 * 可分成這些類型: * 檔案管理(File manipulation) * 狀態資訊(Status information) * 登入檔(registry):用來儲存和取回組態(configuration)的訊息 * 檔案的修改(File modification) * 程式語言支援(Programming language support) * 程式語言支援(Program loading and execution) * 通信(Communications) * 背景服務(Background services) * Known as services, subsystems, daemons * Most users' view of the operation system is defined by system programs, not the actual system calls ### 作業系統的設計和製作(Operating System Design and Implementation) * 設計目標: * 定義系統目標及規格 * 受到硬體選擇與系統類型的影響 * 使用者目的(user goal):operating system should be convenient to use * 系統目的(system goal):operating system should be efficient * 方法與策略(Mechanism & Policy) * 方法(Mechanism):How to do it? * 策略(Policy):What will be done? * 方法與策略的分開對提高作業系統的彈性非常重要 * Specifying and designing OS is highly creative task of software engineering ### 製作(Implementation) * 早期的作業系統是由組合語言寫成的,現在多使用高階語言寫成(C、C++) * 作業系統可以使用一種以上的語言撰寫 * 使用高階語言的優點除了更容易寫好與偵錯外,也更容易移植(port)到其他硬體上(組語會依硬體而不同),但會降低速度與增加儲存需求 * 模擬器(Emulation)可使作業系統在非原生的硬體上執行 ### 作業系統結構(Operating System Structure) * 作業系統是一個大而複雜的系統,假如要有合適的功能和容易的修改,一般的方法是將它分成較小的元件而不是一個單一(monolithic)的系統 * 簡單的結構(Simple Structure) * MS-DOS * 介面和功能的層次不是分隔的很好 * 應用程式能夠直接的存取基本I/O常式 ![](https://i.imgur.com/zvlDVAM.png) * UNIX * 分成核心與系統程式 ![](https://i.imgur.com/P438Wu3.png) * 分層方法(Layered Approach) * 使用由上而下(top-down)的作法 * 底部層(第0層)為硬體,最高層(第N層)為使用者介面 ![](https://i.imgur.com/VDl254t.png) * 每層只能使用較低層的功能與服務 * 設計的困難為如何適當的定義每一層 * 效率不好:每個動作須經過多層 * 微核心(Microkernel System Structure) * Mach OS使用微核心的技術 * 移去核心所有非必要元件,將作業系統結構化 * 微核心提供最少的形成和記憶體管理,和通訊功能 * 程式藉由微核心訊息傳遞(message passing)達到間接通訊 * 容易擴展,不要求更改核心 * 系統功能增加會導致效能降低 ![](https://i.imgur.com/iIYLC4J.png) * 模組(Modules) * 目前最好的設計方法:可載入的模組核心(loadable kernel modules) * 核心提供主要的服務,其他的服務則在核心執行時,動態的被製作 * 每一核心模組都有定義好的介面 * 其他核心模組可在需要的時候被核心(kernel)載入 ![](https://i.imgur.com/Be1C9Qs.png) * 混合系統(Hybrid Systems) * 很少作業系統採用單一的架構,多組合組合不同的架構產生混合系統 * Mac OS X: * Aqua UI * Cocoa為Objective-C程式語言設定API,用來撰寫應用程式 * 核心環境提供動態可載入模組(Mac OS X稱為kernel extensions) ![](https://i.imgur.com/2RKgSik.png) * iOS * iOS是架構在Mac OS X上,加入了行動裝置相關功能,但不能直接執行Mac OS X的應用程式 ![](https://i.imgur.com/nwIukKS.png) * Cocoa touch)=:用來撰寫應用程式 * 媒體服務(Media services):繪圖、聲音、視訊的服務 * 核心服務(Core services):提供各種服務,包括雲端運算、資料庫支援 * Android * 開放原始碼 * 與iOS架構類似 * 使用Linux,但被google修改過 * Java程式開發使用Android API * Dalvik虛擬機是為Android所設計,並對行動裝置做最佳化 ![](https://i.imgur.com/2frrpwz.png) ### 作業系統除錯(Operating-System Debugging) * 除錯(debugging):發現和修正系統中硬體和軟體錯誤 * 記錄檔(log files):作業系統將錯誤資訊編寫至記錄檔 * 核心轉存(core dump):應用程式錯誤,捕捉行程的記憶體,儲存在檔案做後續的分析 * 當機轉存(crash dump):核心錯誤被稱為當機(crash),作業系統錯誤,捕捉行程的記憶體,儲存在檔案做後續的分析 * Kernighan’s Law: Debugging is twice as hard as writing the code in the first place. * 性能調適(performance tuning) * 追蹤列表(trace listings):作業系統產生系統行為紀錄的檔案,用來尋求性能的改善 * 藉由移除發生在系統中的瓶頸(bottleneck)來改善性能 * 剖析(Profiling):對指令指標定期採樣以統計分析 ![](https://i.imgur.com/ylPRaBe.png) * DTrace * 可以對執行程式動態增加探測點(probe)的設施 ![](https://i.imgur.com/aLDK4S5.png) * 當執行提供者(provider)的程式,探測點(probe)會將擷取到的狀態數據發送給探測點的消費者(comsumer) * Dtrace紀錄使用者ID 101的每個行程執行和它們花在CPU上執行的時間 ![](https://i.imgur.com/xtvZE9j.png) ![](https://i.imgur.com/s2Ct06m.png) ### 作業系統建立(Operating System Generation) * 作業系統通常以磁碟、ISO映像檔(CD-ROM或DVD-ROM的格式)發布 * SYSGEN program: * 獲取有關硬體系統特定配置的信息以組成該機的作業系統 * 如果有多個CPU,每一個的屬性都需讀取 * 決定記憶體的容量需求 * 最佳化系統選項 ### 系統啟動(System Boot) * 載入核心來開啟電腦稱為啟動(booting) * 靴帶式程式(bootstrap program/bootstrap loader):找到核心,並載入記憶體,開始執行核心程式 * 有些電腦將此分為兩步驟,先有一個簡單的靴帶式程式儲存在韌體,從磁碟中的啟動區塊(boot block)載入一個程式,再由該程式載入作業系統核心 * 靴帶式程式儲存在唯讀記憶體(ROM)或可抹寫式唯讀記憶體(EPROM) * 所有形式的ROM就是韌體(firmware) * GRUB是Linux的靴帶式程式範例 * 當作業系統核心載入記憶體並開始執行,系統才能稱為執行(running)

    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