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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # OS筆記-Chapter 3: Processes ###### tags: `OS` --- #### 目錄 * 總論 [Chapter 1: Introduction](https://hackmd.io/NoZq3J7IQvOQpcbo_tctjA) [Chapter 2: Operating-System Structures](https://hackmd.io/OKykRLBESI6v9a13HgS35A) * 行程管理 <font color="red">Chapter 3: Processes</font> [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) --- ### 行程的觀念(Process Concept) * 程式是一項被動(passive)的個體,是存儲在磁碟的可執行案(executable file),行程就是一個執行的程式,是一項主動(active)的個體 * 一個程式可能有多個行程,但每個行程的記憶體不相同 * CPU不斷的在行程之間轉換,使行程可以並行 * 如何稱呼CPU所有的運作項目: * 整批式系統(Batch system):工作(jobs) * 分時系統(Time-shared systems):使用者程式(user program)或任務(tasks) * 工作或行程兩詞幾乎可以交互使用 * 程行包括: * 程式碼(program code):也稱本文區(text section) * 程式計數器(PC,program counter)的數值 * 暫存器內容 * 堆疊(stack): * 存放暫用資料 * Function parameters、return addresses、local variables * 資料區間(data section):儲存global variables * 堆疊(heap):行程執行期間動態配置的記憶體 ![](https://i.imgur.com/VUS0Qe8.png) * 行程本身可能是其他程式碼的執行環境 * Java程式是在Java虛擬機內(JVM,Java virtual machine)執行,JVM是以行程的方式執行 * 行程狀態(Process State) * 新產生(new):行程正在產生 * 執行(running):指令正在執行 * 等待(waiting):等待事件發生 * 就緒(ready):行程等待指定一個處理器 * 結束(terminated):完成執行 * 任何時候只有一項行程可以在一個處理器上執行,但可以有多個行程在等待和就緒 ![](https://i.imgur.com/laf6Ivi.png) * 行程控制表(PCB,Process Control Block) * 或稱任務控制表(Task Control Block) * 用來儲存各個行程的相關資訊 * 程式計數器(program counter):下一個要執行的指令位址 ![](https://i.imgur.com/x6m8obU.png) ![](https://i.imgur.com/KREVolY.png) * 執行緒(thread) * 單一執行緒只允許行程一次執行一個任務 * 允許行程執行多個執行緒,在多核心系統特別有利,因為多執行緒可以並行 ### 行程排班(Process Scheduling) * 多元程式規劃系統的主要目的是隨時保有一個行程在執行,以提高CPU的使用率 * 分時系統的目的是將CPU在不同的行程之問不斷轉換 * 行程排班程式(Process scheduler):為CPU選則一個可用的行程執行 * 在單一處理器系統裡,不可能有一個以上的行程同時執行 * 排班佇列(scheduling queue) * 工作佇列(Job queue):行程進入系統(new state)時,放在此佇列中 * 就緒佇列(Ready queue):位於主記憶中且就緒等侍(ready state)執行的行程,放在此佇列中 * 裝置佇列(Device queue):每個裝置都有本身的裝置佇列,以儲存等侍某裝置的行程 ![](https://i.imgur.com/nrus9da.png) ![](https://i.imgur.com/zodQtxT.png) * 排班程式(Scheduler) * 長程排班程式(Long-term scheduler): * 又稱工作排班程式(job scheduler) * 從行程池中選出行程,並且將他們載入記憶體內以便執行 * 短程排班程式(Short-term scheduler): * 又稱CPU排班程式(CPU scheduler) * 從記憶體中選出一個已經準備就緒的行程,並將CPU分配給他 * 長程排班程式控制著多元程式規劃的程度(degree of multiprogramming)(在記憶體中行程的數量) * I/O傾向行程(I/O-bound process): * 在I/O的時間比計算的時間還多 * 多但短的CPU使用 * CPU傾向行程(CPU-bound process): * 在計算的時間比I/O的時間還多 * 較少但很長的CPU使用 * 混合行程(process mix):若要使系統有最佳效能,就必須對兩種傾向的行程做組合才行 * 中程排班程式(midium-term scheduler): * 置換(swapping):有時將行程從記憶體中有效的移開,稍後再放回記憶體 * 藉此降低多元程式規劃的程度 ![](https://i.imgur.com/TvvDtYL.png) * 行動系統的多工 * 前景(foreground):目前開啟和顯示在螢幕的應用程式 * 背景(background):保留在記憶體中的應用程式,但不佔據螢幕 * 應用程式必須使用一個服務(service) * 服務(service):代表背景行程執行單獨的應用元件 * 內容轉換 * 內容轉換(context switch):轉換CPU至另一項行程時必須將舊行程的狀態儲存起來,然後再載入新行程的儲存狀態 * 行程內容(context)以PCB表示 * 內容轉換所花費的時間是額外的浪費 * 內容轉換所花費的時間取決於硬體 ### 行程的操作(Operations on Processes) * 行程的產生(Process Creation) * 父(parent)行程產生子(children)行程,每一個新產生的行程可以再產生其它行程,形成一個行程樹 * 行程識別碼(pid,process identifier):每個行程唯一,被用來做為索引值來存取核心內行程的各種屬性 ![](https://i.imgur.com/qHfCNqs.png) * 資源分配: * 父行程與子行程共用全部的資源 * 子行程使用父行程部份的資源 * 不共用 * 執行作法: * 父行程繼續執行而子行程也同時執行 * 父行程等著它的所有子行程中止後才繼續執行 * 新行程位址空間: * 子行程是父行程的複製品 * 子行程有一個程式載入其中 * UNIX範例: * fork():產生新行程,新行程有原行程位址空間的一份拷貝,新行程的fork()傳回碼為0,而新行程的行程識別碼(非0)則會回給原父行程 * exec():在fork()之後使用,以使載入新的程式來替換掉原先行程的記憶體 * 使用這種方式,這兩個行程可以互相溝通,各自執行 ![](https://i.imgur.com/GpySiJM.png) * Windows範例: * CreateProcess():需要載入一個指定程式到子行程記憶體空間,且需要不少於十個參數 * STARTUPINFO(傳給CreateProcess()的參數之一):記載許多新行程的特性 * PROCESS_INGORMSTION(傳給CreateProcess()的參數之一):包含一個新產生形成和它的執行緒的處理器與識別碼 * 行程的結束(Process Termination) * 通常只有父行程才能使用exit()來中止其子行程,父行程必須能夠區別各個子行程,因此新產生行程的識別名必須傳回給它的父行程 * 行程終止的理由: * 子行程使用超過配置的資源數量 * 子行程的工作已不再需要 * 父行程結束,而作業系統不允許子行程在父行程結束後繼續執行 * 串接式結束(cascading termination):如果父行程結束,系統不允許子行程繼續存在,由作業系統啟動,強迫結束所有子行程 * wait():可以傳回中止之子行程的識別碼,因此父行程能分辨到底是哪一個子行程中止執行 * 殭屍(zombie):已經結束的行程,但父行程還沒呼叫wait() * 孤兒(orphan):如果父行程已經結束,但子行程還在執行 * init行程週期性的呼叫wait()讓孤兒可以結束 * 多行程架構(Multiprocess Architecture) * Chrome瀏覽器使用行程架構: * 只有一個瀏覽器(browser)行程 * 每個新分頁開啟的網站都是一個渲染器(renderer)行程 * 插件行程 * 當一個渲染器行程錯誤,不會影響到其它分頁 * 當渲染器行程在沙箱(sandbox)中執行,表示存取磁碟和網路是受限的,減少安全漏洞 ### 行程間通訊(Interprocess Communication) * 獨立 V.S 合作 * 獨立行程(independent process):不受其他行程影響、不影響其他行程、不與其他行程共用資料 * 合作行程(cooperating process):受其他行程影響、能影響其他行程、和其他行程共用資料 * 行程合作的目的: * 資訊共享 * 加速運算 * 模組化 * 方便性 * 行程間通訊(IPC,interprocess communication): ![](https://i.imgur.com/ulaEPDG.png) * (a)共用記憶體(shared memory): * 比訊息傳遞快 * 通常作業系統避免一個行程去存取另一行程的記憶體,共用記憶體則移除此限制 * 資料形式與位置由行程決定,而非作業系統 * 生產者(producer)產生資訊 * 消費者(consumer)消耗資訊 * 緩衝區:允許生產者與消費者同時執行,讓生產者填滿,讓消費者取光 * 無限緩衝區:無大小限制,生產者能不斷生產 * 有限緩衝區: * 使用環狀陣列 * 邏輯指標in==out時,緩衝區為空 * 邏輯指標(in+1)%BUFFER_SIZE==out時,緩衝區為滿 * producer: ![](https://i.imgur.com/yNPlGvP.png) * consumer: ![](https://i.imgur.com/SpOSma8.png) * (b)訊息傳遞(message passing): * 作業系統提供行程間通訊的設施 * send(message) * receive(message) * 一個行程所傳送的訊息大小可能是固定或可變的 * 通訊鏈(communication link):兩行程要互相聯繫,必須存在通訊鏈 * 直接聯繫(direct communication):每個傳送或接收的行程必須確定聯繫者的名稱 * send(P,message) * receive(Q,message)(對稱) * 對稱 V.S不對稱 * 對稱:接收與傳送都必須指名 * 不對稱:傳送才須要指名接收者 * 行程定義的模組性受到限制,行程改名後,可能需要檢查所有其它行程 * 間接式聯繫(indirect communication): * 信箱(mailbox)/port(埠):藉此來傳送與接收訊息 * 每個信箱都有一個識別字 * 只有在兩個行程有共用的信箱時,才能互相聯繫 * 信箱可能是一個行程或是作業系統擁有 * 一個新的信箱由它的所有人產生 * 性質: * 只有在一對具有共用信箱的行程間才能建立通訊鏈 * 一個鏈可以有兩個以上的行程 * 每對互相通訊的行程間,可能存在數個鏈 * P1、P2、P3共用信箱A,P1傳遞訊息至A,而P2、P3都要接收,到底哪個行程能接收到P1的訊息呢 * 一個鏈最多只能有兩個行程(就不會有此問題了) * 只有一個行程能接收 * 行程能任意地接收 * 同步(synchronization)/等待(blocking) * 等待傳送(blocking send):傳送行程等待著,直到接收行程或信箱接收 * 等待接收(blocking receive):接收者等待,直到有訊息出現 * 非同步(asynchronous)/非等待(nonblocking) * 非等待傳送(nonblocking send):傳送行程送出訊息,並繼續執形 * 非等待接收(nonblocking receive):接收者取回有效訊息或無效資料 * 接收與傳送有不同的組合,當兩者都在等待時,兩者之間就有約會(rendezvous) * 無論是直接或間接聯繫,經由通訊的訊息是放在一個暫時的佇列 * 零容量(zero capacity):傳送者必須等待,直到接收者收到訊息 * 有限的容量(bounded capacity):若佇列未滿,傳送者可以不需等待,將新訊息放入佇列中,當佇列已滿,傳送者必須等待,直到佇列有可用空間 * 無限制容量(unbounded capacity):不須等待

    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