許峻維
    • 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 New
    • Engagement control
    • Make a copy
    • 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 Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy 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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # NCU Linux2024 Final Project Group 4 ## 組員 李倬安 110403516 林晉宇 110403518 陳 立 110502552 許峻維 110504007 ## 環境 Ubuntu 版本: ```bash lsb_release -a Distributor ID: Ubuntu Description: Ubuntu 24.04.1 LTS Release: 24.04 Codename: noble ``` kernel版本: ```bash uname -a Linux xxxx 6.12.5 #37 SMP PREEMPT_DYNAMIC Thu Dec 26 00:15:40 CST 2024 x86_64 x86_64 x86_64 GNU/Linux ``` 核心數: ```bash nproc 32 ``` ## wait queue實現 ### 實現 #### system call 定義 使用 `SYSCALL_DEFINE1` 定義了一個新的 system call : ```c SYSCALL_DEFINE1(wait_queue, int, id) { switch (id){ case 1: enter_wait_queue(); break; case 2: clean_wait_queue(); break; } return 0; } ``` 根據 `id` 的值, system call 要麼將 thread 添加到 wait queue,要麼喚醒 thread 。 #### 資料結構 - **wait queue head**:使用 `DECLARE_WAIT_QUEUE_HEAD` 宣告了一個 wait queue: ```c wait_queue_head_t my_wait_queue; DECLARE_WAIT_QUEUE_HEAD(my_wait_queue); ``` - **spin lock**:用於保護變數 `cnt` 和 `condition`: ```c static spinlock_t my_spinlock; ``` - **計數器**: - `int cnt;` - 計算已進入 wait queue的 thread 數量。Initialized to be 0. - `int condition;` - 用於控制何時喚醒 thread 。Initialized to be 0. #### 函數:`enter_wait_queue` 此函數將 thread 添加到 wait queue: ```c static int enter_wait_queue(void) { int id; spin_lock(&my_spinlock); id = cnt++; spin_unlock(&my_spinlock); printk(KERN_INFO "Thread %d is entering wait queue\n", id); wait_event_interruptible(my_wait_queue, ({ int ret; spin_lock(&my_spinlock); ret = (condition > id); spin_unlock(&my_spinlock); ret; })); printk(KERN_INFO "Thread %d is exiting wait queue\n", id); return 0; } ``` - **flow**: 1. 獲取 spin lock,以安全地增加 `cnt` 並為 thread 分配一個 `id`。 2. 釋放 spin lock。 3. 使用 `wait_event_interruptible` 使 thread 休眠,直到條件 `(condition > id)` 為真。 - 該條件確保 thread 按 FIFO 順序被喚醒。 #### 函數:`clean_wait_queue` 此函數喚醒 wait queue中的 thread : ```c static int clean_wait_queue(void) { printk(KERN_INFO "Starting to wake up threads...\n"); while(condition < cnt){ spin_lock(&my_spinlock); condition++; spin_unlock(&my_spinlock); wake_up_interruptible(&my_wait_queue); mdelay(50); } return 0; } ``` - **流程**: 1. 印出一條訊息,喚醒過程的開始。 2. 在迴圈中,當 `condition` 小於 `cnt` 時,增加 `condition`。 - 這確保每個 thread 的等待條件 `(condition > id)` 按其 `id` 的順序依次為真。 3. 使用 `wake_up_interruptible` 喚醒 thread 。 4. 使用 `mdelay(50)` 引入一個小的延遲來控制喚醒的時間間隔。 ### 使用者空間程式碼 在使用者空間程式中,使用 `syscall` 呼叫 `wait_queue` system call : ```c void *enter_wait_queue(void *thread_id) { int tid = *(int *)thread_id; printf("enter wait queue thread_id: %d\n", tid); syscall(wait_queue_syscall_number, 1); printf("exit wait queue thread_id: %d\n", tid); return NULL; } void clean_wait_queue() { // printf("start clean queue ...\n"); syscall(wait_queue_syscall_number, 2); } ``` - **thread**: - 每個 thread 呼叫 `enter_wait_queue`,它以 `id == 1` 呼叫 system call ,使其進入 wait queue。 - **主函數**: - 建立多個 thread (例如,10 個 thread )。 - 延遲一段時間後,呼叫 `clean_wait_queue` 喚醒 thread 。 ### 執行結果 **樣本輸出:** ![image](https://hackmd.io/_uploads/S1QwqEsH1e.png) - thread 以與進入順序相同的順序退出 wait queue,驗證了 FIFO 行為。 ## 解釋 ### 確保 FIFO 順序 透過在 thread 進入 wait queue時為其分配一個遞增的 `id`,可以確保 thread 按相同的順序被喚醒。條件 `(condition > id)` 是一個判斷,使每個 thread 依次通過。 ### 同步機制 - **spin lock**: - 用於保護變數 `cnt` 和 `condition`。 - 確保atomic,防止多個 thread 同時存取共享資源時發生競爭。 - **wait queue**: - `wait_event_interruptible` 使 thread 休眠,直到指定條件為真。 - `wake_up_interruptible` 喚醒在 wait queue上等待的 thread 。 ### 其他考慮 - **競爭**: - 如果沒有適當的同步,多個 thread 可能同時增加 `cnt`,導致 `id` 分配錯誤。 - 使用spin lock確保一次只有一個 thread 修改 `cnt` 或 `condition`。 - **喚醒時機**: - 透過使用 `mdelay(50)` 在喚醒 thread 之間引入延遲,有助於輸出 FIFO 行為。 ## 附錄 ### 完整程式碼列表 #### 內核模組(`wait_queue.c`) ```c #include <linux/init.h> #include <linux/module.h> #include <linux/kernel.h> #include <linux/syscalls.h> #include <linux/wait.h> #include <linux/sched.h> #include <linux/delay.h> #include <linux/spinlock.h> wait_queue_head_t my_wait_queue; DECLARE_WAIT_QUEUE_HEAD(my_wait_queue); static spinlock_t my_spinlock; static int cnt = 0; static int condition = 0; SYSCALL_DEFINE1(wait_queue, int, id) { switch (id){ case 1: enter_wait_queue(); break; case 2: clean_wait_queue(); break; } return 0; } static int enter_wait_queue(void) { int id; spin_lock(&my_spinlock); id = cnt++; spin_unlock(&my_spinlock); printk(KERN_INFO "Thread %d is entering wait queue\n", id); wait_event_interruptible(my_wait_queue, ({ int ret; spin_lock(&my_spinlock); ret = (condition > id); spin_unlock(&my_spinlock); ret; })); printk(KERN_INFO "Thread %d is exiting wait queue\n", id); return 0; } static int clean_wait_queue(void) { printk(KERN_INFO "Starting to wake up threads...\n"); while(condition < cnt){ spin_lock(&my_spinlock); condition++; spin_unlock(&my_spinlock); wake_up_interruptible(&my_wait_queue); mdelay(50); } return 0; } static int __init init_wait_queue_module(void) { spin_lock_init(&my_spinlock); printk(KERN_INFO "wait queue Module Initialized\n"); return 0; } static void __exit exit_wait_queue_module(void) { printk(KERN_INFO "wait queue Module Exited\n"); } module_init(init_wait_queue_module); module_exit(exit_wait_queue_module); MODULE_LICENSE("GPL"); MODULE_AUTHOR("G4"); MODULE_DESCRIPTION("Custom wait queue Implementation"); ```

    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