StevenChou43
    • 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
    --- tags: linux2022 --- # 2022q1 Homework4 (quiz4) contributed by < `StevenChou499` > * [作業需求](https://hackmd.io/@sysprog/BJJMuNRlq) * [測驗題目](https://hackmd.io/@sysprog/linux2022-quiz4) --- ## 測驗 `1` > 延伸[第 3 週測驗題](https://hackmd.io/@sysprog/linux2022-quiz3)的測驗 `7` ,已知輸入必為大於 `0` 的數值 (即 `x > 0` ),以下程式碼可計算$[\log _{2} (x)]$,,也就是 [ceil](https://man7.org/linux/man-pages/man3/ceil.3.html) 和 [log2](https://man7.org/linux/man-pages/man3/log2.3.html) 的組合並轉型為整數: > ```c > int ceil_log2(uint32_t x) > { > uint32_t r, shift; > > x--; > r = (x > 0xFFFF) << 4; > x >>= r; > shift = (x > 0xFF) << 3; > x >>= shift; > r |= shift; > shift = (x > 0xF) << 2; > x >>= shift; > r |= shift; > shift = (x > 0x3) << 1; > x >>= shift; > return (EXP1) + 1; > } > ``` > 請補完,使其行為符合預期。作答規範: > * `EXP1` 應以最簡潔的形式撰寫,且符合[作業一](https://hackmd.io/@sysprog/linux2022-lab0)排版規範 (近似 Linux 核心程式碼排版風格) > * `EXP1` 為表示式,限制使用 `^` , `&` , `|` , `<<` , `>>` 這幾個運算子,可能會出現常數 > * `EXP1` 不該包含小括號 (即 `(` 和 `)` ) > * 為了自動批改的便利,變數出現的順序 (可從缺) 從左到右為 `r` , `shift` , `x` > * `EXP1` 不可包含 `,` 和 `;` 這樣的分隔符號 (separator) ### 思考與想法 本題與[測驗3](https://hackmd.io/@sysprog/linux2022-quiz3)的第 `7` 題其實非常相似,前面之時做方法其實都是相同的,只是因為這次需要搭配計算的是 [ceil](https://man7.org/linux/man-pages/man3/ceil.3.html) 的方式, 因此一開始需要先將數字 `x` 減 `1` ,最後在計算完之後再加上 `1` ,以避免在計算以 `2` 為底的對數時少算。而因為 `EXP1` 只需要計算最後一位,不需要經過計算相加並儲存,其值為 `r | shift | x >> 1` 。 :::success EXP1 : r | shift | x >> 1 ::: --- ## 測驗 `2` > 複習〈[你所不知道的 C 語言: bitwise 操作](https://hackmd.io/@sysprog/c-bitwise)〉,改寫[第 3 週測驗題](https://hackmd.io/@sysprog/linux2022-quiz3)的測驗 `11` 裡頭的 fls 函式 (fls 意謂 "find last set"),使其得以計算 [Find first set](https://en.wikipedia.org/wiki/Find_first_set): > ```c > #define BITS_PER_BYTE 8 > #define BITS_PER_LONG (sizeof(unsigned long) * BITS_PER_BYTE) > > #include <stddef.h> > > static inline size_t ffs(unsigned long x) > { > if (x == 0) > return 0; > > size_t o = 1; > unsigned long t = ~0UL; > size_t shift = BITS_PER_LONG; > > shift >>= 1; > t >>= shift; > > while (shift) { > if ((EXP2) == 0) { > x >>= shift; > EXP3; > } > > shift >>= 1; > t >>= shift; > } > > return o; > } > ``` > 請補完,使其行為符合預期。作答規範: > `EXP2` 和 `EXP3` 應以最簡潔的形式撰寫,且符合[作業一](https://hackmd.io/@sysprog/linux2022-lab0)排版規範 (近似 Linux 核心程式碼排版風格) > `EXP2` 和 `EXP3` 限制使用 ^, &, |, <<=, >>=, +=, -= 這幾個運算子,可能會出現常數 > `EXP2` 和 `EXP3` 不該包含小括號 (即 `(` 和 `)` ) > 為了自動批改的便利,變數出現的順序 (可從缺) 從左到右為 `x` , `o` , `t` , `shift` ,也就是說,你應該寫 `x ^ t` 而非 `t ^ x` > `EXP2` 和 `EXP3` 不可包含 `,` 和 `;` 這樣的分隔符號 (separator) ### 思考與想法 --- ## 測驗 `3` > 考慮以下改寫自 Linux 核心的程式碼: > ```c > struct foo_consumer { > int (*handler)(struct foo_consumer *self, void *); > struct foo_consumer *next; > }; > > struct foo { > struct foo_consumer *consumers; > unsigned long flags; > }; > > #include <stdbool.h> > > /* > * For foo @foo, delete the consumer @fc. > * Return true if the @fc is deleted sfccessfully > * or return false. > */ > static bool consumer_del(struct foo *foo, struct foo_consumer *fc) > { > struct foo_consumer **con; > bool ret = false; > > for (con = &foo->consumers; *con; EXP4) { > if (*con == fc) { > *con = EXP5; > ret = true; > break; > } > } > > return ret; > } > ``` > 請補完,使 `consumer_del` 行為符合註解規範。作答規範: > 1. `EXP4` 和 `EXP5` 應以最簡潔的形式撰寫,且符合作業一排版規範 (近似 Linux 核心程式碼排版風格) > 2.`EXP4` 和 `EXP5` 都包含指標操作 (如 `->`) ### 思考與想法 題目使用了 `pointer to a pointer` 的方式循序訪問各個 `foo_consumer` ,並比較之後若相同則刪除該 `foo_consumer` ,回傳 `true` 。因此 `EXP4` 之值應該為讓 `for` 迴圈跳往下個 `foo_consumer` 的 `con = &(*con)->next` ,而 `EXP5` 之值應該為 `con = &(*con)->next` ,才可以跳過想要刪除的點而直接連接至下一個 `foo_consumer` 。 :::success EXP4 : con = &(*con)->next EXP5 : con = &(*con)->next ::: ## 測驗 `4`

    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