sysprog
  • NEW!
    NEW!  Connect Ideas Across Notes
    Save time and share insights. With Paragraph Citation, you can quote others’ work with source info built in. If someone cites your note, you’ll see a card showing where it’s used—bringing notes closer together.
    Got it
        • 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
          • Owners
          • Signed-in users
          • Everyone
          Owners Signed-in users Everyone
        • Write
          • Owners
          • Signed-in users
          • Everyone
          Owners 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 No publishing access yet

        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.

        Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

        Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

        Explore these features while you wait
        Complete general settings
        Bookmark and like published notes
        Write a few more notes
        Complete general settings
        Write a few more notes
        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
      • 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 Help
    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
    Owners
    • Owners
    • Signed-in users
    • Everyone
    Owners Signed-in users Everyone
    Write
    Owners
    • Owners
    • Signed-in users
    • Everyone
    Owners 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 No publishing access yet

    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.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    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
    # 2026-04-28/30 問答簡記 ## nyraa mutex 和 semaphore 的差別 生產者消費者問題用 mutex 要怎麼設計 ## andy20040 stack 和 heap的差別? * stack: stack 為 LIFO 的 資料結構,其適配了 C 語言函式呼叫的方式 例如: 先呼叫 A( ) 再呼叫 B( ) 時,會先將 A( ) 的 stack frame 壓入堆疊再將 B( ) 的 stack frame 壓入堆疊,當函式要 return 的時候由於 LIFO 的特性 B( ) 的 stack frame 會先被彈出 ,這跟 C 語言函式返回的方式相同。 * heap: 使用 malloc( ) 或 calloc() 申請空間,當空間不需要的時候,用 free()來釋放空間,可以避免記憶體洩漏 ## rrrchii 如何加速llm稀疏矩陣的運算速度? 算出改為 COO 的時間複雜度,用期望值計算(會有e)? 回答: 測試程式: 來源:https://hackmd.io/@Aquamay/Syd8UdLqu ![image](https://hackmd.io/_uploads/HJjaKgA6-e.png) 可以用for迴圈先找到非零值並把位置紀錄下來,減少結果為0的矩陣乘法來加速稀疏矩陣的運算,像是上面的例子,每一筆非零值就是一個 $1 \times 3$ 的矩陣,若有 $N$ 筆非零值,我們可以得到一個 $N \times 3$ 的小矩陣,這個方式稱為 Coordinate Format (COO),以下是測試結果 已知密度如何算出(有e) ## Bigtooth123 Thread 間如何保護共享記憶體? * Mutex: 同一時間只能有一個 thread 在 critical section,具有 Ownership,誰加鎖就必須由誰解鎖,未取得鎖的 Thread 會進入睡眠,去到 waiting queue,釋放 CPU,需注意死鎖(Deadlock)風險。 * Semaphore: 計數器機制,控制資源數量,wait() 扣減資源,post() 釋放資源,當無可用資源時(計數器 $\le$ 0),執行 wait() 的 Thread 會被阻塞(Block)並進入 waiting queue。 * Spinlock: Busy Waiting,Thread 在 while 迴圈中不斷測試變數(如使用 test_and_set),不釋放 CPU,直到獲得鎖並進入到 critical section,需注意死鎖(Deadlock)風險。 而以上操作都需要 atomic 指令的幫助才能實現。 Atomic 指令確保了一段段操作是不可分割的,期間不會有任何的中斷。 Read Modify Write: 以 ``i++`` 來做舉例,這個操作包含了三個部分 * 讀取變數 `i` * 修改變數 `i` * 寫入變數 `i` Race Condition 如果沒有 Atomic 指令保護,這三個步驟可能被其他核心的中斷或存取交錯執行,導致資料不一致,例如:兩個執行緒各加 `1`,結果卻只增加了 `1`。 實作方法: ```c int compare_and_swap(int *value, int expected, int new_value) { int temp = *value; if (*value == expected) *value = new_value; return temp; } void increment(atomic_int *v) { int temp; do { temp = *v; } while (temp != (compare_and_swap(v,temp,temp+1)); } ``` 其中 compare_and_swap 就是 atomic 操作。 當很多 Thread 競爭時,Spinlock 效率低下該怎麼辦? 從 Cache Coherence 的角度來分析 * Test & Set Lock: * 指令裡包含一個「寫入」動作(把 lock 設為 true) * 影響 : 核心 A 執行 test_and_set,發出訊號給其他所有核心,使得其他核心 cache 需要重新抓取資料。 * Test & Test & Set Lock: * 在鎖被持有的期間,所有等待的 Thread 都在執行「普通讀取」。 * 影響: 大家都可以從自己的 Cache 裡讀到鎖的狀態,鎖被釋放後再去搶鎖。 第二種方法不會像是第一種方法那樣造成匯流排充斥著無意義的「失效」與「抓取」訊號。但是真正在實作的時候通常會用 MCS spinlock。 Object Lifecycle: 必須防止 Use-After-Free,確保物件釋放與存取之間具備原子性,可以使用引用計數的方式,最後一個 Thread 把計數器減到 0 時,才由該 Thread 負責執行 free。 ## MAX990ck 甚麼時候會發生 lock 的爭奪 linux 核心中,有哪些方式可以實作 lock semaphore 跟 mutex 的差別 mutex 跟 semaphore 會應有在哪裡 甚麼場合適合用semaphore 生產與消費者問題 timer 是如何震盪的 -> RLC 電路震盪的功耗 https://wiki.csie.ncku.edu.tw/embedded/priority-inversion-on-Mars.pdf https://github.com/sysprog21/concurrent-programs https://github.com/sysprog21/concurrent-programs/blob/master/lf-queue/lfq.c kweb: https://hackmd.io/@sysprog/SJYvYX_Wxl RTOS: https://hackmd.io/@Appmedia/Preempt-RT

    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
    Sign in via Google Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    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