yangyang128
    • 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
    # 2021q1 Homework2 (quiz2) contributed by < [`xxiex123`](https://github.com/xxiex123) > ###### tags: `linux2021` ## 測驗一 ### 解釋上述程式碼運作原理 `list_head` 的結構如同 [quiz1](https://hackmd.io/@yangyang128/quiz1) 所述,它是一個通用的 doubly-linked list 結構,只要將他當作struct 裏的一個 member ,那他就能幫助該 struct 建立一個雙向鏈接。其能達到這樣的功能主要靠歸功于 **`container_of`** ```c #ifndef container_of #define container_of(ptr, type, member) \ __extension__({ \ const __typeof__(((type *) 0)->member) *__pmember = (ptr); \ (type *) ((char *) __pmember - offsetof(type, member)); \ }) #endif ``` `__extension__` 用來防止使用 GNU extension 時可能發生的警告訊號。 將list_head作爲member 放到struct 裏,便擁有雙向鏈接特性。 ```c typedef struct __element { char *value; struct __element *next; struct list_head list; } list_ele_t; ``` 這裏的成員 `__element *next` 其實沒必要存在,因爲 linked list 功能可直接用 `list_head` 完成。 從 [DDerveialm](https://hackmd.io/@XDEv11/linux2021q1_homework2_quiz2) 同學那邊看到,將 list_head list 成員放到該結構第一個成員的位置,可以使用 (list_ele_t *) list 去直接得到 list_ele_t 的位置,省下用 `container_of` 的麻煩和成本。因此修改後 ```c typedef struct __element { struct list_head list; char *value; } list_ele_t; ``` 而queue_t 結構定義如下 ```c typedef struct { list_ele_t *head; /* Linked list of elements */ list_ele_t *tail; size_t size; struct list_head list; } queue_t; ``` 該例題並不需要用到 head 和tail,因此將其移除使結構更簡潔 ```c typedef struct { size_t size; struct list_head list; } queue_t; ``` 在做了上述改動後,所有程式碼中的list_entry函數就都可以用(list_ele_t *)取代。 下面get middle 的方法 ```c static list_ele_t *get_middle(struct list_head *list) { struct list_head *fast = list->next, *slow; list_for_each (slow, list) { if (fast->next == list ||fast->next->next == list) break; fast = fast->next->next; } return list_entry(TTT, list_ele_t, list); } ``` 利用走一步比上走兩步的方法達到一種類似除2的效果,道理類似與100米賽跑,A選手速度為50m/s,B選手速度100m/s,所以B選手到終點時,A選手正好到中間的地方,也就是50米。 這邊list merge的部分,把左邊已排序好的list 和右邊已排序好的list 合并成一組已排序好的list。 ```c static void list_merge(struct list_head *lhs, struct list_head *rhs, struct list_head *head) { 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)) { char *lv = list_entry(lhs->next, list_ele_t, list)->value; char *rv = list_entry(rhs->next, list_ele_t, list)->value; struct list_head *tmp = strcmp(lv, rv) <= 0 ? lhs->next : rhs->next; list_del(tmp); list_add_tail(tmp, head); } list_splice_tail(list_empty(lhs) ? rhs : lhs, head); } ``` 上面的判別式應該是寫錯了 ```c if (list_empty(lhs)) { list_splice_tail(lhs, head); return; } ``` 意為左邊list 如果為空,就把左邊list加到head裏,正確應該是: 左邊為空,則把右邊加到head,再return。 同理右邊空,把左邊加到head ,return。 下面 merge 不多説,把list 分成左右邊去處理,再合并,是一個遞迴演算法。 ```c void list_merge_sort(queue_t *q) { if (list_is_singular(&q->list)) return; queue_t left; struct list_head sorted; INIT_LIST_HEAD(&left.list); list_cut_position(&left.list, &q->list, MMM); 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); } ``` 從quiz1 的 [Non-recursive](https://alienryderflex.com/quicksort/)所述,遞迴函數大量使用stack的特性在效率上并不樂觀,且穩定性收到限制。 > they’re really doing is using the stack as their own private array. This is much slower than using a real array, and could cause stack overflow on some systems 因此可以將merge sort 用iteration 方式實作。 --- ## 測驗二 ### 解釋程式碼運行 *考慮函式 func 接受一個 16 位元無號整數 N,並回傳小於或等於 N 的 power-of-2* ```c uint16_t func(uint16_t N) { /* change all right side bits to 1 */ N |= N >> 1; N |= N >> X; N |= N >> Y; N |= N >> Z; return (N + 1) >> 1; } ``` 我們觀察二進制 21 , 21=0000000000010101,其小於等於N的power of 2 是16,二進制為16=0000000000010000,可以發現其實答案只是左邊開始遇到的第一個1留著,其他為0。上述實作做法是吧第一個1後面全都變成1,即00010101->00011111,此時讓其+1,變成00100000,再往左位移,變成00010000,就是答案。 但考慮到第16bit為1的輸入,假設1000001000000000,變成左邊變1后111111111111111,加1變成0000000000000000,發生overflow,因此更好的做法是,1111111111111111 往右先位移,變成0111111111111111,再加1。 更: 從 [linD026](https://hackmd.io/YflLkD86QdW-ojCVXwwj4g?view#C11-63-Conversions) 的作業得知,`N + 1` 這個動作,因爲 1 是 int 形態,當 uint16 與 int 做運算時,會把較低位元的形態轉成高位元的形態再做運算,因此 N 會先轉成 int 形態再做 +1 的動作,所以并不會有 overflow 發生。 但儘管如此,上面所使用的方法還是一個較佳的做法,因爲假設我想做的是 uint32 形態的 rounddown_pow_of_2 ,那 overflow 就會發生。 ### 觀察 power of 2 相關函數 由 linux 的 bitops.h ```c static inline __attribute__((const)) bool is_power_of_2(unsigned long n) { return (n != 0 && ((n & (n - 1)) == 0)); } /* * round up to nearest power of two */ static inline __attribute__((const)) unsigned long __roundup_pow_of_two(unsigned long n) { return 1UL << fls_long(n - 1); } /* * round down to nearest power of two */ static inline __attribute__((const)) unsigned long __rounddown_pow_of_two(unsigned long n) { return 1UL << (fls_long(n) - 1); } ``` is_power_of_2 這個函數比較直觀, x 假設是 2 的冪,那必定只有一個位元是 1,其他都是 0 ,而此數被 -1 的話,結果其實就是由這一個 1 (包含)開始向右做 reverse bits。 因此 x & (x - 1)必定為 0 。 而下面的 roundup 和 rounddown 則要追蹤到 fls 這個函數 而 1UL 代表 type 為 unsigned long 的 1。 ```c /** * fls - find last (most-significant) bit set * @x: the word to search * * This is defined the same way as ffs. * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32. */ static __always_inline int fls(unsigned int x) { int r = 32; if (!x) return 0; if (!(x & 0xffff0000u)) { x <<= 16; r -= 16; } if (!(x & 0xff000000u)) { x <<= 8; r -= 8; } if (!(x & 0xf0000000u)) { x <<= 4; r -= 4; } if (!(x & 0xc0000000u)) { x <<= 2; r -= 2; } if (!(x & 0x80000000u)) { x <<= 1; r -= 1; } return r; } ``` 由注解可以知道此函數尋找 most-significant bit 開始遇到的第一個 1。 由此函數,roundup 回傳 `1UL << fls_long(n - 1)` ,可以先思考,n 的 roundup pow of 2 其實就是原本的 fls + 1 的2的冪次,比方説 000100100100 fls 為 9 ,那其結果就是 2的10次方,也就是 001000000000。此結果可由 000000000001 << fls 來得到。而傳入 fls 的值為 n - 1 是因爲要避免值已經是2的冪的情況還去做 roundup 。 而 rounddown 則是回傳 `1UL << (fls_long(n) - 1)` ,可以想成是 roundup 的結果 >> 1 。這裏用到了 shift -1 的情況。 考慮到 n = 0 的時候,fls 會 return 0,因此變成 `1UL << -1` ,因此 n = 0 的 rounddown 也 = 0。 ### slab allocation 由維基百科 [Slab allocation](https://en.wikipedia.org/wiki/Slab_allocation) 的定義,由於 CPU 在初始化和拆除(destruction)核心的資料物件時,耗費了大量的時間導致效能降低,因此利用 slap allocation 的機制: 預先 allocated 一些常用到的特定形態和大小的資料物件在記憶體裏,當 kernel 要用到時可以不需要重新初始化記憶體,而直接到 slab 去拿一個空的(free)位置來用。 這個方法降低記憶體 fragmentation 的問題,因爲 slab 是由一塊實體上連續的記憶體空間所形成。 由 linux kernel organization 網站的 [Chapter 8 Slab Allocator](https://www.kernel.org/doc/gorman/html/understand/understand011.html#:~:text=The%20slab%20allocator%20has%20three,allocating%2C%20initialising%20and%20destroying%20objects.) 也有提到 > The slab allocator has three principle aims: * The allocation of small blocks of memory to help eliminate internal fragmentation that would be otherwise caused by the buddy system; * The caching of commonly used objects so that the system does not waste time allocating, initialising and destroying objects. Benchmarks on Solaris showed excellent speed improvements for allocations with the slab allocator in use [Bon94]; * The better utilisation of hardware cache by aligning objects to the L1 or L2 caches. buddy system 在 [linD026](https://hackmd.io/YflLkD86QdW-ojCVXwwj4g?view#考量) 同學作業中有詳細的説明,是一種 memory allocation 的方法,顯然這種方法效能被 fragmentation 所限制。 第三點指出,slab allocator 也讓 cpu 裏的 L1、L2 caches 使用率更佳。

    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