Shengyuu
    • 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
    # 2019q1W7上:林聖堯 contributed by `Shengyuu` ###### tags: `Linux 核心設計 2019` ## 測驗 1 ### 函式解析: `find_next_bit` 這個函式是從給定的資料 `bitmap` 的起始點 `start` 開始找,找到第一個 `set bit` 及回傳該位址,反之,則回傳 `bitmap` 的最大邊界 `bits` 的位址 在 C 語言中想要一個 bit 一個 bit 逐一檢查事件非常沒有效率的事情,因為每次從記憶體取值的單位可能為 32 bits 或 64 bits 等等,但不會是 1 bit。因此在 `bits.c` 中是作這些 find bit 的函式都是透過 unsigned long 為單位,一次檢查 64 bits ,若 `find_next_bit` 發現要找的結果在某個 `set` 裡面,在從這個 `set` 裡找出第一個 1 的位址。 觀察 `bits.c` 會發現函式 `find_next_zero_bit` ```c static inline size_t find_next_zero_bit(const unsigned long *bitmap, size_t bits, size_t start) { size_t pos; unsigned long t; size_t l = BITS_TO_LONGS(bits); size_t first_long = start / BITS_PER_LONG; size_t long_lower = start - (start % BITS_PER_LONG); if (start >= bits) return bits; t = bitmap[first_long] | ~BITMAP_FIRST_WORD_MASK(start); t ^= ~0UL; for (size_t i = first_long + 1; !t && i < l; i++) { /* search until valid t is found */ long_lower += BITS_PER_LONG; t = bitmap[i]; t ^= ~0UL; } if (!t) return bits; pos = long_lower + bitops_ffs(t) - 1; if (pos >= bits) return bits; return pos; } ``` 此函式為求出字串中第一個為 0 的位址,和題目要求的 `find_next_bit` 相反。觀察此段程式碼會發現只要把判斷是否找到 `0` 的程式碼 ```c t ^= ~0UL; ``` 更改為是否找到 `1` 的程式碼 ```c t ^= 0UL; ``` 即可移植到 `find_next_bit` 函式中 ### find_next_bit ```c static inline size_t find_next_bit(const unsigned long *bitmap, size_t bits, size_t start) { unsigned long t; size_t l = BITS_TO_LONGS(bits); size_t first_long = start / BITS_PER_LONG; size_t long_lower = start - (start % BITS_PER_LONG); if (start >= bits) return bits; t = bitmap[first_long] & BITMAP_FIRST_WORD_MASK(start); t ^= 0UL; for (size_t i = first_long + 1; !t && i < l; i++) { // ...待補... long_lower += BITS_PER_LONG; t = bitmap[i]; t ^= 0UL; } if (!t) return bits; size_t pos; // ...待補... pos = long_lower + bitops_ffs(t) - 1; if (pos >= bits) return bits; return pos; } ``` ## 測驗 2 觀察程式碼會發現 `check_bits` 函式,`check_bits` 用另一個函式 `find_next` 來驗證我們實作的 `find_next_bit` 的結果是否正確。因此我們只要想辦法產生隨機的 `bitmap` 丟到 `check_bits` 函式即可驗證 想到隨機驗證,就會想用 `rand()` 函式來試驗,翻閱規格書後確定在 `gnu C libary` 內定義的 `rand()` 最大值為 32 bits singed int 的最大值,而 `test` 的型態是 `unsigned long` ,有 64 個位元,因此用 4 個 rand() * rand() 相加來產生一個 test 的 element ```c test[j] = rand() * rand() + rand() * rand() + rand() * rand() + rand() * rand(); ``` ### main 程式碼 ```c int main(void) { srand(time(0)); for(int i = 0; i < 1000; i++) { for(int j = 0; j < BITS_TO_LONGS( MAX_TEST_BITS ); j++) { test[j] = rand() * rand() + rand() * rand() + rand() * rand() + rand() * rand(); } check_bits(test, MAX_TEST_BITS); } } ``` ## 測驗 3 函式 `bitops_ffs` 呼叫巨集 `__builtin_ffsl(x)` ,上網查了一下得知此巨集的作用為 `Returns one plus the index of the least significant 1-bit of x, or if x is zero, returns zero.` , ### `bitops_ffs` ```c static inline size_t bitops_ffs(unsigned long x) { for(int i = 0; i< 64; i++) if(x & (1UL << i)) return i + 1; } ``` ### `main` ```c int main() { for(int i = 0; i < 64; i++) assert(bitops_ffs((1UL << i))-1 == i); return 0; } ``` ## 測驗 4 ### `bitops_ffz` ```c static inline size_t bitops_ffs(unsigned long x) { for(int i = 0; i< 64; i++) if(~x & (1UL << i)) return i + 1; } ``` ### `main` ```c int main() { for(int i = 0; i < 64; i++) assert(bitops_ffs(~(1UL << i))-1 == i); return 0; }

    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