何俊逸
    • 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 Homework2 (phonebook-concurrent) contributed by <`hugikun999`> ## Pthread * ```#include <pthread.h>``` * 建立出額外的執行緒 * function (1) ```C int pthread_create(pthread_t,const pthread_attr_t,void,void) ``` 參數1是pthread的指標,參數2是thread的屬性,參數3是function pointer,參數4是function pointer所須要的參數。 (2) ```C int pthread_cancel (pthread_t) ``` 送一個取消的要求給指定的thread,而收到要求的thread要如何處理則取決於它的type和state。state有enable和disable兩種。type有asynchronous和deferred。 參數1是要取消的目標<s>pthread</s>。 >> 不是指標!請熟讀 man page: [pthread_cancel](http://man7.org/linux/man-pages/man3/pthread_cancel.3.html),儘量讀權威資料,否則你會一錯再錯下去! [name=jserv] >> 已改正 [name=俊逸] (3) ```C int pthread_exit(void *retval) ``` <s>關閉</s> 中止一個thread,retval可以拿來存放成功時回傳的值 參數1是設定執行成功時的回傳值。 >> 不是「關閉」,而是「終止」(terminate),以中文意思來說,這兩者根本不同,請注意!這個參數很重要,也不會混淆了。再次詳閱 man page: [pthread_exit](http://man7.org/linux/man-pages/man3/pthread_exit.3.html) [name=jserv] >> 已改正 [name=俊逸] (4) ```C int pthread_join(pthread_t,void) ``` 暫停執行join的thread,等目標thread執行完畢join的thread才繼續執行。 參數1是等待的目標thread,參數2是取得目標thread的回傳。 (5) ```C int pthread_setconcurrency(int new_level) ``` 設定concurrency的level ## phonebook-concrrence ### file (1)file_align.c ```C static off_t fsize(char *path); ``` 要使用off_t必須要```#include <sys/type.h>```,拿來存目標檔案的路徑和名子。 ```C struct stat(char,struct stat) ``` 要用stat必須要```#include<sys/stat.h>```,取得有關char所存的檔案名的資訊,並寫到參數2所指的buf。 ```C void *memset(void *s, int c, size_t n) ``` 將s中前n個bytes用c填滿。 * 不懂的部份 ```C while (fgets(rbuf, sizeof(rbuf), fd0)) { memset(wbuf, '\0', pad); if ((suffix = (pad - strlen(rbuf))) != 0) strncpy(wbuf, rbuf,strlen(rbuf)); fwrite(wbuf, pad, 1, fd1); } ``` 這個部份應該是拿來將沒有滿的rbuf複製到已事先填滿```\0```的wbuf裡面,但是如果rbuf已經被填滿就會略過if裡面的事,那這樣的話應該是要執行```fwrite(rbuf, pad, 1, fd1)```。 將程式中while的部份改成如下,發現append()的時間變高了1倍多,測試過將```\0```改成填入其它char,但是時間依然很長沒有變短,目前還想不到的可能原因。 ```C while (fgets(rbuf, sizeof(rbuf), fd0)) { memset(wbuf, '\0', pad); if ((suffix = (pad - strlen(rbuf))) != 0) { strcpy(wbuf, rbuf); fwrite(wbuf, pad, 1, fd1); } fwrite(rbuf, pad, 1, fd1); } ``` ![](https://i.imgur.com/wwLqGbN.png) >> 程式碼是寫給你修改的,不是給你背誦用,遇到疑惑就修改程式來觀察! [name=jserv] >> 已經測試過,等等會補。[name=俊逸] (2)file.c 和```file_align.c```主要差別在```file.c```是一個function,```file_align.c```是一個program。 (3)main.c ```C __builtin___clear_cache(char *begin,char *end) ``` <s>清除memory中的instruction</s> 清除指定範圍內的instruction cache >> 請認真看 gcc 手冊: [Other Built-in Functions Provided by GCC](https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html),操作對象是 cache! [name=jserv] >> 已更正 [name=俊逸] ```C open(const char *pathname, int flags) ``` pathname給定目標檔案的路徑,會回傳一個很小、非負的整數。O_NONBLOCK可以使檔案為不被鎖的狀態。 ```C mmap(void *addr, size_t lengh, int prot, int flags, int fd, off_t offset) ``` 配置一段虛擬記憶體的位址給呼叫的process。addr是開始mapping的起始位置,length是mapping的長度,從fd指向的檔案中的offset位置開使算length,prot是指這個mapping的特性,分為PROT_EXEC、PROT_READ、PORT_WRITE、PORT_NONE,flags可以放MAP_SHARED、MAP_PRIVATE。傳回一個pointer指向新建立的空間。 ## check correctness 利用```phonebook_opt.c```中所寫的show_entry()將thread所做出來的link list和原始的dictionary做比較,檢查正確性。發現缺少了兩個名字,而且從輸出的名字看來中間有很多重覆建的名字。 ``` lack of:aaaa lack of:aaaaa ``` ## concurrent-ll ### file (1) atomic_ops_if.h ```C __sync_val_compare_and_swap(type *ptr, type oldval, type newval) __sync_bool_compare_and_swap(type *ptr, type oldval, type newval) ``` 如果現在的value和oldval是一樣的,則將newval寫到ptr。一個回傳內容,一個回傳true or false。 ```C __sync_fetch_and_add(type *ptr, type value) __sync_fetch_and_sub(type *ptr, type value) __sync_add_and_fetch(type *ptr, type value) __sync_sub_and_fetch(type *ptr, type value) ``` 其即是運算 ```C tmp = *ptr; *ptr += value; return tmp; ``` 一個是回傳更新前的值,一個是回傳更新後的值。 ```C CAS_U64_bool((volatile AO_t *)(a), (AO_t)(e), (AO_t)(v)) ``` volatile會告訴compiler這個值可能會在任何時間被更新,即使在code上下文中沒有任何對其修改的動作。 >> AO_t不知道是什麼? [name=俊逸] >> Check this: http://www.hpl.hp.com/research/linux/atomic_ops/README.txt (全部讀過!) [name=jserv] >> 了解~ [name=俊逸] memory barrier:用來確保多thread時不會被out-of-order影響,在屏障前的運算都會在對屏障後發揮效用。 ```C AO_t ``` type of unsigned integers。AO_TS_t會以最好的效能做為分割的依據,有時候是byte,有時候是等同於AO_t,其位置必須以AO_TS_INITALIZER初始化。 (2)getticks.h ```C __asm__ __volatile__(code : output : input : clobber) ``` rdtsc:read timestamp counter,讀取一個64bits的計數器,它會+1每個clock cycle。 輸出暫存器語法,"="寫入專用,"+"讀寫兩用。

    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