何俊逸
    • 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
    # 2016q3 Homework3 (mergesort-concurrent) contributed by <`hugikun999`> ## mergesort-concurrent ### 流程圖 ```graphviz digraph{ 輸入thread_data->pthread初始化->task放入queue->task_run task_run->task_run task_run->pthread_exit task_run->cut_func; cut_func->cut_func; cut_func->merge_sort; merge_sort->merge_list; merge_sort->merge; merge_list->merge; merge->list_print; } ``` ### file (1) list.c ```C node_t *node_new(val_t val, node_t *next) ``` 新增一個node把資料放進去並且使該node指向next。 ```C llist_t list_new() ``` 新增一個list,將head設為NULL、size設為0。 ```C list_add(llist_t *list, val_t val) ``` 將所給的list新增一個node其data是val,且此node會被加在list的最前面。 ```C list_nth(llist_t *list, uint32_t idx) ``` 尋找第idx個node,若idx超過size回傳NULL。 ```C list_print(llist_t *list) ``` 印出list中所有node的data。 (2)threadpool.c ```C task_free(task_t *the_task) ``` 將要傳入的工作釋放,包括arg和本身。 ```C tqueue_init(tqueue_t *the_queue) ``` 將目標工作佇列初始化,head、tail、mutex、cond、size都設為NULL。 ```C *tqueue_pop(tqueue_t *the_queue) ``` 先將lock鎖起來,檢查the_queue的tail有沒有東西,如果有就會將tail指向last。接下來檢查新的tail,如果有東西就將目前tail的next設為NULL;沒有就將the_queue的head設為NULL。size最後再減一,將鎖起來的lock解鎖,回傳ret。 ```C tqueue_size(tqueue_t *the_queue) ``` 取得the_queue中的工作數。 ```C tqueue_push(tqueue_t *the_queue, task_t *task) ``` 先將lock鎖起來,將task的last設為NULL,task的next接到the_queue。先檢查the_queue的head是否是空的,如果不是就將the_queue的last指向task,再將the_queue的head指向task。如果原本the_queue的size是0,則將the_queue的tail指向task順便將size+1。最後在解鎖。 ```C tqueue_free(tqueue *the_queue) ``` 一個一個將the_queue釋放掉,再將mutex摧毀。 ```C tpool_init(tpool_t *the_pool, uint32_t tcount, void *(*func)(void *)) ``` 分配給threads、queue空間,將the_pool的count設為tcount且初始化queue,宣告一個pthread的屬性並初始化設定為JOINABLE。開始呼叫新的process並且新的thread開始執行func。最後將attr給摧毀。 ```C tpool_free(tpool_t *the_pool) ``` 用pthread_join()等待每個pthread終止,確定終止之後再將the_pool的threads給釋放,最後將queue也釋放。 ### 更改 (1) 在```threadpool.c```中把已經釋放memory的the_pool這個指標設為NULL,以必免再次使用造成錯誤。在```main.c```中的最後也加了將pool設為NULL。 (2) 將```threadpool.c```中tpool_init()裡面有關的```pthread_create()```給碼掉。剛開始_task的func和arg根本就還沒有放東西,所以就算呼叫了```pthread_create()```進到```*task_run()```也會只會一直在while迴圈循環。另外在```threadpool.c```寫一個```threads_run()```的函式將碼掉的地方放進去。如此便可以等到_task放完東西再呼叫,不會浪費資源。 >> 可用 `diff -up` 的輸出來表示程式碼變更,而非只是貼出修改後、包含過時註解的程式碼 [name=jserv] >> 了解[name=俊逸] ```C the_pool->threads = (pthread_t *) malloc(sizeof(pthread_t) * tcount); the_pool->count = tcount; the_pool->queue = (tqueue_t *) malloc(sizeof(tqueue_t)); tqueue_init(the_pool->queue); /* pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); for (uint32_t i = 0; i < tcount; ++i) pthread_create(&(the_pool->threads[i]), &attr, func, NULL); pthread_attr_destroy(&attr);*/ return 0; ``` (3) 將`word.txt`亂數後的檔案先補齊16位,利用`mmap()`得出開頭指標在用`list_add()`建入list中,最後將lastname的字元做比大小(字元比大小會自動換為ASCII code)。 ```C The whole program cost: 0.051664 sec ``` 字元比大小 ```C while (a->size && b->size) { int count = 0, diff = 0; char *asize = a->head->data; char *bsize = b->head->data; llist_t *small; while((asize[count] != '\0') || (bsize[count] != '\0')) { diff = asize[count] - bsize[count]; if(diff < 0) { small = a; break; } else if(diff > 0) { small = b; break; } else count++; } ``` ### Problem (1) ```mmap()```建立一個連續的memory區域並傳回傳開頭指標,那我將指標直接丟入```list_add()```,這樣所指向的位置會有多少char?如何知道最後會存入多少個byte?如果我將```MAX_LAST_NAME_SIZE```設為32,會不會```lastname[]```有兩個名字。 >> 取決於輸入 data set 的內容,目前實做只在意輸入的位址,至於每筆要取多少長度,是 mergesort 裡頭比較程式碼指定 [name=jserv] >> 不太懂意思,所以我是要從mergrsort裡面更改嗎?[name=俊逸] 我有嘗試將`phonebook_concurrent`的size改成32且```file.c```的```MAX_BUFF_SIZE```也改為32,但是建立出來的數目是一樣多的,並沒有少一半。

    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