楊朝智
    • 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 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
    • 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 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
    # Communication、Synchronization、IPC、Message Queues ###### tags: `EOS` Lec13-14 嵌入式作業系統 ## Semaphore / mutex - 可由一個以上的 thread 取得或釋放 - 可以用來做同步或互斥鎖 ![](https://i.imgur.com/eR0Ai77.png) ### 結構 `include/linux/semaphore.h` ```c struct semaphore { raw_spinlock_t lock; unsigned int count; struct list_head wait_list; }; ``` https://ithelp.ithome.com.tw/articles/10213633 1. 盡可能使用 spin_lock 1. 在絕對不允許被搶佔的地方使用 raw_spin_lock 1. 如果 Critical Section 夠小,使用 raw_spin_lock * count 允許多少資源進入 CS * struct list_head wait_list 一個雙向的 Linked List,這邊用作排隊使用,規則是 FIFO(First-In-First-Out),記錄所有正在睡眠的 ### Types of Semaphores - Binary semaphores - Counting semaphores - Mutual-exclusion (mutex) semaphores #### Binary semaphores 左:互斥鎖 右:同步 ![](https://i.imgur.com/Ifpr5Dw.png) #### Counting semaphores 左:保護相同資源,只給幾個人用 右:同步用,等所有人都到 ![](https://i.imgur.com/bRpqXb8.png) ##### Bounded vs. Unbounded Count - Unbounded: maximum value that can be held by the count’s data type 受限於資料型別 - Unsigned integer vs. unsigned long value - Bounded: Maximum count #### Mutual-exclusion (mutex) semaphores 特殊的 Binary semaphores ![](https://hackmd.io/_uploads/H1w0PPm82.png) 1. ownership 2. recursive access - 如果不支援 1. ownership、2. recursive access 同一支 task x 會被自己的函式鎖住。 ![](https://i.imgur.com/qV0z1sg.png) 3. task deletion safety 使用 mutex 時,task deletion locks 會防止 task 被刪除 4. protocols for avoiding mutual exclusion problems 優先序顛倒問題 ![](https://i.imgur.com/1c0oxXZ.png) 解決辦法 - Priority inheritance protocol • Priority of lower priority task is raised to that of higher priority task 低的繼承高的優先序,讓他釋放資源 • Priority of raised task is lowered to original value a/f releasing mutex 釋放完再降低優先序 - Ceiling priority protocol • Priority of task that acquires mutex is set to the highest priority 低的變最高 • When mutex is released, the priority is lowered to its original value 使他釋放 ==三個 semaphore 的差異性在哪? 有資源時值是多少? mutex 有哪幾個特性?== `counting 只給 n 個人用相同資源,有可用資源值會是正數。 mutex 值為 0 時為 unlocked ,只能給一個 task 用 , 且同一個 task 內的 function 都可以重複用資源,有 lock 可以防止被刪除 , 使用 繼承優先序 或 置頂優先序 避免優先序顛倒。 ` ### Common Semaphore Use 用途 wait 優先,但是會等 signal 釋放旗幟 1. Waiting-and-signal synchronization **B** 1. Multiple-task wait-and signal synchronization **B** 1. Single shared-resource-access synchronization **B** 1. Recursive shared-resource-access synchronization **M** 1. Multiple shared-resource-access synchronization **C** 1、2 同步用。3、4、5 溝通用。 #### Waiting-and-Signal synchronization 同步,雖然沒溝通但可以藉由 signal 釋放旗幟使 wait 開始 ![](https://i.imgur.com/kwkhKXm.png) ```c= tWaitTask ( ) { : Acquire binary semaphore token : } tSignalTask ( ) { : Release binary semaphore token : } ``` #### Multiple-Task Wait-and-Signal flush: 所有 wait 一起跑 ![](https://i.imgur.com/lojK7db.png) #### Single Shared-Resource-Access ![](https://i.imgur.com/IfsVAXj.png) 保護資源 binary semaphore 從 1 開始 - 但可能被別人打開,除非用 mutex ```c= tAccessTask() { : Acquire binary semaphore token Read/Write to shared resource Release binary semaphore token : } ``` #### Recursive Shared-Resource-Access ![](https://i.imgur.com/QR46hkH.png) ```c= tAccessTask () { : Acquire mutex ->lock+1 Access shared resource Call Routine A Release mutex ->lock+3 -3 移除mutex : } Routine A () { : Acquire mutex ->lock+2 Access shared resource Call Routine B Release mutex ->lock+3 -2 : } Routine B () { : Acquire mutex ->lock+3 Access shared resource Release mutex ->lock+3 -1 : } ``` #### Multiple Shared-Resource-Access - #eq shared resources: 2 - #access tasks: 3 ![](https://i.imgur.com/bqC2LW6.png) ## Message queue ![](https://i.imgur.com/P8hJ2nC.png) 每段元素大小固定,一次放一段訊息,可能會有剩餘空間 - empty state -> receiving task waiting list (lined up comsumers) - full state -> sending task waiting list ### States ![](https://i.imgur.com/z3hLsI2.png) ### memory use 兩個process在中間共用,複製資料兩次 ![](https://i.imgur.com/BZFfA7F.png) ### Storage * System Pools 共用 * Private Buffers 專用 ### Receiving Messages * Destructive read 常見 * Non-destructive read 可重複讀取,少見 ### Use 1. Non-Interlocked, One-Way Data Communication ![](https://i.imgur.com/hmfulvl.png) 2. Interlocked 雙向 , One-Way Data Communication * 同步: * binary semaphore (0): ACK * 收到一個訊息後 binary semaphore (1) ![](https://i.imgur.com/hUqPis4.png) 3. Interlocked, Two-Way Data Communication * 有去有回 ![](https://i.imgur.com/jgTmWXZ.png) 4. Broadcast Communication * 支援 Non-destructive read 可重複讀取的電腦才能廣播 ![](https://i.imgur.com/hEK1sc4.png) ## Pipe ![](https://i.imgur.com/GdSJXGQ.png) UNSTRUCTURED: 讀的大小不定 ```c= #include <unistd.h> //int pipe(int filedes[2]); pipe(fd) write(fd[1], read(fd[0], ``` ![](https://i.imgur.com/4EOoZDo.png) data-producing task and a dataconsuming task ### Pipe vs. Message Queue - Pipe store a stream of bytes. - Pipe does not support priority - Pipe supports select operation ![](https://i.imgur.com/b4mwcD0.png) select 觀察有限時間 pipe 有沒有可用的讀寫 ==message queue 和 pipe 差異性和應用?== ## Shared memory `man 2 shmget` (2: syscall) ```c= #include <sys/ipc.h> #include <sys/shm.h> //allocates a System V shared memory segment int shmget(key_t key, size_t size, int shmflg); ``` 函數傳入值 - key * 0(IPC_PRIVATE):會建立新共享內存對象 * 大於0的32位整數:視參數shmflg來確定操作。通常要求此值來源於ftok返回的IPC鍵值 - size * 大於0的整數:新建的共享內存大小,以字節為單位 * 0:只獲取共享內存時指定為0 - shmflg * 0:取共享內存標識符,若不存在則函數會報錯 * IPC_CREAT:當shmflg&IPC_CREAT為真時,如果內核中不存在鍵值與key相等的共享內存,則新建一個共享內存;如果存在這樣的共享內存,返回此共享內存的標識符 * IPC_CREAT | IPC_EXCL:如果內核中不存在鍵值與key相等的共享內存,則新建一個消息隊列;如果存在這樣的共享內存則報錯 函數返回值 * 成功:返回共享內存的標識符 * 出錯:-1,錯誤原因存於error中

    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