Taiyou Kuo
    • 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
    # 2019q1 Homework1(list) contributed by < `st9540808` > * [F02: list](https://hackmd.io/s/S12jCWKHN) ## 自我檢查事項 ### 為何 Linux 採用 macro 來實作 linked list?一般的 function call 有何成本? 有一些操作只能用 macro 實作像是 `list_for_each` ,需要展開成 for 才能支援這種循序走訪。 一般的 function call,caller 需要把引數放到 stack 中,新增一段新的 stack,並在回傳時收回,macro 沒有這些操作的成本 參考: [x86 calling convention](https://en.wikibooks.org/wiki/X86_Disassembly/Calling_Conventions) ### Linux 應用 linked list 在哪些場合?舉三個案例並附上對應程式碼,需要解說,以及揣摩對應的考量 > v4.20.13 kernel/sched/sched.h, line 228 `struct rq` 是 linux 中的 runqueue,裡面有兩個成員 `struct cfs_rq cfs` CFS 的排程器和 `struct rt_rq rt` 即時排程器的 runqueue,即時任務總共有 100 個優先級 (0-99),所以在 `struct rt_rq rt` 中有一個 `struct rt_prio_array` 的實體 `active`,對應的 runnable 及時任務會被放在相對應優先級的 runqueue 中 ```c struct rt_prio_array { DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */ struct list_head queue[MAX_RT_PRIO]; }; ``` :::warning 列出 Linux 核心版本並 **解說** 只有列出結構體,不能算是解說,而要廣泛提及針對該結構體做了哪些 linked list 操作,又考量點為何 :notes: jserv ::: ### GNU extension 的 typeof 有何作用?在程式碼中扮演什麼角色? ## merge sort 實作 ### function prototype: ```C list_ele_t *get_middle(struct list_head *list); void list_merge(struct list_head *lhs, struct list_head *rhs, struct list_head *head); void list_merge_sort(queue_t *q); ``` `queue_t` 和 `list_ele_t` 的定義是從 lab0 引入,在兩者中都加入 `struct list_head` ,以便最終能夠支援 qtest merge sort 需要兩個輔助的函式以便實作 `list_merge_sort()` ,兩個函式的說明如下: * `get_middle()`: 回傳一個 list 中,位於中間節點的位址 * `list_merge()`: 合併兩個已排序的 list ### 實作細節 #### `get_middle()` 使用兩個指標 `fast slow` 來走訪 list,`fast` 一次會前進 2 個節點,`slow` 一次會前進 1 個。 參考: [Floyd Cycle Detection Algorithm](https://zh.wikipedia.org/wiki/Floyd%E5%88%A4%E5%9C%88%E7%AE%97%E6%B3%95) ```c list_ele_t *get_middle(struct list_head *list) { struct list_head *fast, *slow; fast = list->next; list_for_each (slow, list) { if (fast->next != list && fast->next->next != list) fast = fast->next->next; else break; } return list_entry(slow, list_ele_t, list); } ``` #### `list_merge()` 將兩個已排序好的 list `lhs rhs` 合併在一起並傳給 `head`,因為排序對象是字串,排序完要變成字典序,所以用 `strcmp()` 比較兩個字串在字典中的順序, ```c void list_merge(struct list_head *lhs, struct list_head *rhs, struct list_head *head) { struct list_head *temp; INIT_LIST_HEAD(head); if (list_empty(lhs)) { list_splice_tail(lhs, head); return; } if (list_empty(rhs)) { list_splice_tail(rhs, head); return; } while (!list_empty(lhs) && !list_empty(rhs)) { if (strcmp(list_entry(lhs->next, list_ele_t, list)->value, list_entry(rhs->next, list_ele_t, list)->value) <= 0) temp = lhs->next; else temp = rhs->next; list_del(temp); list_add_tail(temp, head); } list_splice_tail(list_empty(lhs) ? rhs : lhs, head); } ``` #### `list_merge_sort()` 合併排序的主演算法,全部使用 `list_*` 的操作 1. 檢查 list 是否只有一個節點 (遞迴的中止條件) 2. 將 list 切成兩份,左邊的在 `left` ,右邊的在 `q` 3. 遞迴呼叫 ```list_merge_sort(&left); list_merge_sort(q); ``` 4. 將左右兩個 list 合併,並將合併後的 list 放入 `q` ```c void list_merge_sort(queue_t *q) { struct list_head sorted; queue_t left; if (list_is_singular(&q->list)) return; INIT_LIST_HEAD(&left.list); list_cut_position(&left.list, &q->list, &get_middle(&q->list)->list); list_merge_sort(&left); list_merge_sort(q); list_merge(&left.list, &q->list, &sorted); INIT_LIST_HEAD(&q->list); list_splice_tail(&sorted, &q->list); } ``` merge sort 與 quick sort 的效能比較,資料來自 [dict](https://github.com/sysprog21/dict) 的 [cities.txt](https://github.com/sysprog21/dict/blob/master/cities.txt) ,內含九萬多筆城市的字串,並分別對 30000、60000、90000 筆資料做排序,比較兩種演算法對**亂序資料**的排序效能。 ![](https://i.imgur.com/9w1AyCO.png) 可以看到 quick sort 比起 merge sort 有比較好的效能表現,但如果排序資料是已經排序好的,就會出現 worst case $O(n^2)$ 的時間複雜度。 `list_merge()` 需要插入 n 個節點,每次比較都要呼叫 `strcmp()` ,但因為測試資料中字串的最大長度是固定的,所以每次比較需要 $Θ(1)$。 `list_merge_sort()` 的時間複雜度可以用以下遞迴式表示: $T(n) = 2T(n/2) + Θ(1)$ 用 master theorem 可以解以上式子,得到 $T(n) = Θ(nlogn)$

    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