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
    # Dead lock :::danger 複習用 :::spoiler [TOC] ::: ![](https://i.imgur.com/4Oske0g.png) ## 定義 ![](https://i.imgur.com/eixIu0S.png) 可以運用汽車塞車了 ![](https://i.imgur.com/S81iFkM.png) process之間互相搶奪資源,雙方都無法運行,造成了CPU效能大幅度下降 ### Dead lock & Starvation ![](https://i.imgur.com/Pc7TR3i.png) :::info **Dead lock**:必須要等到 前面 process 用完成之後或是放棄了下一個才能使用 ::: :::info **Starvation**: 會有優先順位的差別 如果這是又是可以搶先的那情況就會更亂了 ::: ## Dead lock 四項必要條件 ### Mutual exclutoin (resource are non-shareable) ![](https://i.imgur.com/DnarePm.png) :::info 可以用列表機來想 一次只能列印一份文件 在一個時間內,只能有一個行程在使用資源 ::: ### Hold and wait (持有並等待) ![](https://i.imgur.com/QfUXbjj.png) :::warning 排隊排到了才能上車 ::: ### No preemption (不可搶先) ![](https://i.imgur.com/vB40uSb.png) :::warning 不能當流氓插隊 ::: ### Circular waiting (循環式等待) ![](https://i.imgur.com/gaKHzfP.png) **single process 不會發生** ## 處理方法 ![](https://i.imgur.com/WCQMXMi.png) ### Dead-Lock Prevention(預防) :::warning 太預防&保護造成性能下降 ::: ![](https://i.imgur.com/FktD0sL.png) ![](https://i.imgur.com/lxmXPRC.png) :::info 魚與熊掌 一次只能吃得一個 ::: ![](https://i.imgur.com/zhL17HN.png) ![](https://i.imgur.com/y3QNFv5.png) :::warning 利用反證法來說明 **只要以上方法可以愈deadlock的發生** ::: #### 優缺點 :::danger **優點:保證系同不會有死結發生 缺點:系統效能低(Utilization)** ::: :::success ### Dead-Lock Avoidance (避免) ![](https://i.imgur.com/KsW48lb.png) :::info 房屋貸款是否合理 當提出不合理要求時 OS 會kill請求(unsafe 得情況就會發生 deadlock) 所以下方會產生 banker algo ::: ##### Banker Algo [银行家算法](https://zh.wikipedia.org/wiki/%E9%93%B6%E8%A1%8C%E5%AE%B6%E7%AE%97%E6%B3%95) ![](https://i.imgur.com/fbRqkun.png) ![](https://i.imgur.com/7JGXWut.png) ![](https://i.imgur.com/99FWzxR.png) 設所有 process 在 set P 中;n 為 process 數目,m 為資源種類。 一維陣列: :::info - Requests_i[m] : process i 提出的資源申請量。 - Available[m] : 系統目前各類資源的可用數量。 - Finish[n] : 各個 process 是否完成。 ::: 二維陣列: :::info - Allocation[n,m] : 目前各 process 持有的資源量。 - Max[n,m] : 各 process 需要多少資源才可以完成工作。 - Need[n,m] : 還要多少資源才可以完成工作。 (Need = Max - Alloc) ::: ![](https://i.imgur.com/8WKYo5f.png) ###### 虛擬碼: ``` =ruby while( Finish is not all TRUE ){ found = FALSE; foreach (i in process set P) { if ( Need[i,:] <= Avail ) { Avail = Avail + Alloc[i,:]; P = P - {i}; found = TRUE; } } if (!found) return FAIL; } return OK; ```` ###### 例題: ![](https://i.imgur.com/423W0P4.png) ![](https://i.imgur.com/qCdLegR.png) ![](https://i.imgur.com/EK3fXLW.png) ![](https://i.imgur.com/J4bY9gs.png) ![](https://i.imgur.com/gVJ1zxT.png) :::spoiler ``` =ruby // 基本變數設置 Allocation   Max   Available   ABCD  ABCD  ABCD P1 0014  0656  1520  P2 1432  1942  P3 1354  1356 P4 1000  1750 // 算出 need NEED ABCD 0642  0510 0002 0750 // 宣告確認是否完成的變數 FINISH false false false false // loop : 持續找出可以釋放的 process 並更新 finish // 直到找不到或 finish 全為 true NEED   Available ABCD  ABCD 0642  1520 0510<- 0002 0750 NEED   Available ABCD  ABCD 0642  1520 0000 +1432 0002------- 0750  2952 FINISH false true false false ``` ::: ### Dead Lock Avoidance的重要定理 :::success 假設系統包含m個單一種類的Resources,且被n個Process共用。如果 下列兩個條件滿足,則系統無死結存在 (Dead Lock Free)。 ``` 1 ≤ Maxi ≤ m ΣMaxi < m + n ``` :::warning 有6部印表機提供給n個process使用,每個process之最大需求量為2。 在系統不發生Dead Lock的情況下,最多允許多少個process在系統內 執行? (求n的最大値) :::spoiler $7<n$ ::: ### Deadlock Detection ![](https://i.imgur.com/0X5SakK.png) :::success Allow system to enter deadlock state. Detection algorithm : :::info Single instance : topological sort (using wait-for graph) - 使用 adjcent matrix : O(n^2^) - 使用 adjcent list : O(V+E) ::: ###### Several instance : 用 Banker algorithm 判斷系統是否已經在 unsafe state ### Recovery scheme :::info - 終止 process (成本都高) 1. 全部刪除 2. 一次刪一個process,直到打破 deadlock。 - 哪個行程我們應該先中斷? ::: :::info - 資源搶奪 1. 剝奪 victim process 資源 (可能造成 starvation) 2. 恢復無該資源前狀態 (cost 高) ::: #### Resource preemption :::info - 選一個受害者去preempt - which one to preempt? - rollback - checkpoint - 部份rollback或全部rollback? - starvation - 可以同一個行程被preempted? ::: ### [重要] Resource allocation graph :::success Some facts about RAG : - If graph contains no cycles => no deadlock. - If graph contains a cycle - one instance <=> deadlock. - several instances, possibility of deadlock. ( deadlock => cycle ) ![](https://i.imgur.com/4rXFPFp.png)

    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