yaohwang99
    • 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
    # 2022q1 Homework3 (quiz4) contributed by <`yaohwang99` > > [quiz4](https://hackmd.io/@sysprog/linux2022-quiz4) ## Problem 1 $\lceil\log_2x\rceil$ = number of bits needed to store $x-1$, for example, $\log_28=3,\ 8-1=7=111_2$, $\lceil\log_29\rceil=4, \ 9-1=8=1000_2$ Therefore, `ceil_log2(uint32_t x)` is find last set for `x-1`. 1. Decrease x by 1. 2. Check if any of the 16 more significant bits are set, if so, increase r by 16 and shift x right 16. 3. Repeat step 2 for 8, 4, 2 bits. ```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; } ``` After line 14, x has 2 bit left, we also need to update `r`. With the same approach, The code after line 14 is ```c r |= shift; shift = (x > 0x1) << 0; x >>= shift; r |= shift; return r; ``` The above code can be reduced to `return (EXP1) + 1`, where ==EXP 1 is `r | shift | x >> 1`==. --- ## Problem 2 Similar to Problem 1, the following code uses `t` as mask and check if any of the `shift` less significant bit are set. If none, increase the return value `o` by `shift`. ==EXP2: `x & t`== ==EXP3: `o |= shift`== ```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; } ``` --- ## Problem 3 ```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; } ``` At the start of `for` at line 23, `con` points to `foo->consumers` ```graphviz digraph { compound = true rankdir = LR subgraph cluster0{ label = "struct foo" style = filled color = gray90 node0[label = "<consumers>consumers|flag" shape = record] } subgraph cluster1{ label = "struct foo_consumer" style = filled color = gray90 node1[label = "handler|<next>next" shape = record] } subgraph cluster2{ label = "struct foo_consumer" style = filled color = gray90 node2[label = "handler|<next>next" shape = record] } subgraph cluster3{ label = "struct foo_consumer" style = filled color = gray90 node3[label = "handler|<next>next" shape = record] } con[shape = plaintext] fc[shape = plaintext] con->node0:consumers fc->node2[lhead = cluster2] node0:consumers->node1[lhead = cluster1] node1:next->node2[lhead = cluster2] node2:next->node3[lhead = cluster3] } ``` Then `con` traverse through the list untill `*con == fc`, therefore, ==EXP4 is `con = &(*con)->next`==. ```graphviz digraph { compound = true rankdir = LR subgraph cluster0{ label = "struct foo" style = filled color = gray90 node0[label = "<consumers>consumers|flag" shape = record] } subgraph cluster1{ label = "struct foo_consumer" style = filled color = gray90 node1[label = "handler|<next>next" shape = record] } subgraph cluster2{ label = "struct foo_consumer" style = filled color = gray90 node2[label = "handler|<next>next" shape = record] } subgraph cluster3{ label = "struct foo_consumer" style = filled color = gray90 node3[label = "handler|<next>next" shape = record] } con[shape = plaintext] fc[shape = plaintext] con->node1:next[color = red] fc->node2[lhead = cluster2] node0:consumers->node1[lhead = cluster1] node1:next->node2[lhead = cluster2] node2:next->node3[lhead = cluster3] } ``` Then remove `fc` from the list, ==EXP3: fc->next== ```graphviz digraph { compound = true rankdir = LR subgraph cluster0{ label = "foo" style = filled color = gray90 node0[label = "<consumers>consumers|flag" shape = record] } subgraph cluster1{ label = "foo_consumer" style = filled color = gray90 node1[label = "handler|<next>next" shape = record] } subgraph cluster2{ label = "foo_consumer" style = filled color = gray90 node2[label = "handler|<next>next" shape = record] } subgraph cluster3{ label = "foo_consumer" style = filled color = gray90 node3[label = "handler|<next>next" shape = record] } con[shape = plaintext] fc[shape = plaintext] con->node1:next fc->node2[lhead = cluster2] node0:consumers->node1[lhead = cluster1] node1:next->node3[lhead = cluster3 color = red] node2:next->node3[lhead = cluster3] } ``` ## Problem 4 The structure stores the `env` value. `tasks` is function designator ```c struct task { jmp_buf env; struct list_head list; }; static LIST_HEAD(tasklist); static void (**tasks)(void *); static int ntasks; static jmp_buf sched; ``` ```graphviz digraph { compound = true rankdir = LR subgraph cluster0{ label = "tasklist" style = filled color = gray90 node0[shape = record label="<next>next|<prev>prev"] } subgraph cluster1{ label = "task" style = filled color = gray90 node1[shape = record label="env|<list>list"] } subgraph cluster2{ label = "task" style = filled color = gray90 node2[shape = record label="env|<list>list"] } subgraph cluster3{ label = "task" style = filled color = gray90 node3[shape = record label="env|<list>list"] } node0->node1:list[dir = both] node1:list->node2:list[dir = both] node2:list->node3:list[dir = both] node3:list->node0:prev[dir = both] } ``` `task_add` inserts a new tast from tail and `task_switch` removes the first task in the list. Therefore, ==EXP6 is `list_del(&t->list)`==. `task_join` goes through each task in the list and complete them, so ==EXP7 is `list_del(&t->list)`==. ```c static void task_add(struct list_head *tasklist, jmp_buf env) { struct task *t = malloc(sizeof(*t)); memcpy(t->env, env, sizeof(jmp_buf)); INIT_LIST_HEAD(&t->list); list_add_tail(&t->list, tasklist); } static void task_switch(struct list_head *tasklist) { jmp_buf env; if (!list_empty(tasklist)) { struct task *t = list_first_entry(tasklist, struct task, list); EXP6; memcpy(env, t->env, sizeof(jmp_buf)); free(t); longjmp(env, 1); } } static void task_join(struct list_head *tasklist) { jmp_buf env; while (!list_empty(tasklist)) { struct task *t = list_first_entry(tasklist, struct task, list); EXP7; memcpy(env, t->env, sizeof(jmp_buf)); free(t); longjmp(env, 1); } } ``` `longjmp(sched, 1)` will jump to `setjump(sched)` in `schedule`. ```c void schedule(void) { static int i; srand(0xCAFEBABE ^ (uintptr_t) &schedule); /* Thanks to ASLR */ setjmp(sched); while (ntasks-- > 0) { int n = rand() % 5; tasks[i++](&n); printf("Never reached\n"); } task_join(&tasklist); } ``` For each tast, the program prints `n` then add itself to `tasklist` then do EXP8. ```c /* A task yields control n times */ void task0(void *arg) { jmp_buf env; static int n; n = *(int *) arg; printf("Task 0: n = %d\n", n); if (setjmp(env) == 0) { task_add(&tasklist, env); EXP8; } for (int i = 0; i < n; i++) { if (setjmp(env) == 0) { task_add(&tasklist, env); task_switch(&tasklist); } printf("Task 0: resume\n"); } printf("Task 0: complete\n"); longjmp(sched, 1); } ``` From the output, we can see that the program jumps back to `schedule()` after n is printed, so ==EXP8 and EXP9 is `longjmp(sched, 1)`== ``` Task 0: n = 3 Task 1: n = 3 Task 0: resume Task 1: resume Task 0: resume Task 0: complete Task 1: resume Task 1: complete ``` --- ## Problem 5 ```c #include <stdint.h> #include <string.h> #include <stdlib.h> #define __READ_ONCE_SIZE \ ({ \ switch (size) { \ case 1: \ *(uint8_t *) res = *(volatile uint8_t *) p; \ break; \ case 2: \ *(uint16_t *) res = *(volatile uint16_t *) p; \ break; \ case 4: \ *(uint32_t *) res = *(volatile uint32_t *) p; \ break; \ case 8: \ *(uint64_t *) res = *(volatile uint64_t *) p; \ break; \ default: \ memcpy((void *) res, (const void *) p, size); \ } \ }) static inline void __read_once_size(const volatile void *p, void *res, int size) { __READ_ONCE_SIZE; } ``` Here, we would like to implement READ_ONCE using the above macro. The parameter `res` corresponds to the argument `__u.__c`. The intuitve answer is let DECL0 be `void *__c`, however, case 1 (line 8) of the above macro indicates that we need `*res` to be 1 byte. Therefore, ==DECL0 is `char __c[1]`==, where `__c` is a pointer to 1 byte memory. ```c #define READ_ONCE(x) \ ({ \ union { \ typeof(x) __val; \ DECL0; \ } __u; \ __read_once_size(&(x), __u.__c, sizeof(x)); \ __u.__val; \ }) ```

    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