owned this note
owned this note
Published
Linked with GitHub
# 2016q3 Homework2 (phonebook-concurrent)
contributed by <`janetwei`>
參考 [TempoJiJi 的共筆](/rymKa4aT),測試程式效能以及正確性
>> 列出參考的共筆時,要一併提及人名或代稱。 [name=jserv]
```
size of entry : 24 bytes
execution time of append() : 0.005272 sec
execution time of findName() : 0.004161 sec
```
這是預期做出的結果
```
append() findName() 0.005272 0.004161
append() findName() 0.005856 0.003475
```
但是做出來的output卻不是這樣,少了兩個值
```
size of entry : 24 bytes
execution time of append() : 0.005856 sec
execution time of findName() : 0.003475 sec
```
>> 避免用圖片來表示程式的文字訊息,請改正 [name=jserv]
>> 已改正[name=孜昀]
## Function progrmming
- Function programming(FP) won't make your word processing program faster or better. But there are domains where it is highly useful, and in particular FP looks like the paradigm of choice for unlocking the power of multicore processors
## concurrency vs parallelism
- In programming, concurrency is the composition of independently executing processes, while parallelism is the simultaneous execution of (possibly related) computations.
- concurrency is about dealing with lots of things at once. Parallelism is about doing lots of things at once.
並行是將一件事情區分成很多塊,然後只有用一個CPU去跑,而平行是將事情區分但是由很多個CPU去跑
## Concurrency
* Sequenced-before
一種對同一個thread下,求值順序關係的描述
- Evaluation(求值)
1. value computations:對一串運算式計算的結果
2. side effect:對物件狀態的修改,像是修改記憶體內變數的值、呼叫library的I/O function之類的
在C++ 中一行運算的方程式(f1() + f2()) + f3(),雖然求出來的值一樣,但執行時f3()可以是第一個、第二個、或是最後才被呼叫
- i++ 這類的後置運算子,value computation會先於side effect
- ++ i這類的前置運算子,side effect會先於value computation
- &&, ||, ,這類的運算子,有著比較特殊的例外,左邊的運算元的evaluation會先於右邊的evaluation。因此i++ && (i+j),右邊的i會是副作用產生後的結果。
* Happen-before
- Java對於Happen-before的定義
If one action happens-before another, then the first is visible to and ordered before the second.
當行為A happens-before B時,代表A的效果可見,而且發生於B之前。
- A happens-before B並不代表實際上A happening before B。關鍵在於只要A的效果在B執行之前,對於B可見就可以了,實際上怎麼執行的並不需要深究。
- Sequenced-before其實就是同一個thread內的happens-before。
在跨thread的情況下,如果沒有保證happens-before的關係,程式常常會出現意料之外的結果
## POSIX Thread
[source](https://linux.die.net/man/3/pthread_mutex_init)
mutex:指的是一組用來控制共享資源存取的一組函數
因為在thread的情況下,所有的資源以及變數是共用的,因此必須對大家都可以存取的變數加以控制
因此區分為以下幾個function:
- pthread_mutex_init(),
- pthread_mutex_lock(),
- pthread_mutex_trylock(),
- pthread_mutex_unlock(),
- pthread_mutex_destroy(),
這些使用方法還需要再加以練習
### 宣告type:
pthread_cond_t:use for condition variables
pthread_mutex_t:use for mutex
### function:
- pthread_cond_wait():
It is used to block on a condition variable. They are called with mutex locked by the calling thread or undefined behaviour will result.
These functions atomically release mutex and cause the calling thread to block on the condition variable.
- pthread_attr_setdetachstate():
The detachstate attribute controls whether the thread is created in a detached state.
- pthread_create():
It is used to create a new thread, with attributes specified by attr, within a process. If attr is NULL, the default attributes are used.
- pthread_join():
This function suspends execution of the calling thread until the target thread terminates, unless the target thread has already terminated.
On return from a successful pthread_join() call with a non-NULL value_ptr argument, the value passed to pthread_exit() by the terminating thread is made available in the location referenced by value_ptr.
### file.c
[檔案處理參考](http://pydoing.blogspot.tw/2010/07/c-stdfile.html)
`#include <stdio.h>`
1. `FILE *fopen( char *filename,char *mode);`
:回傳指定檔案的資訊給FILE型態的結構指標,mode控制要給什麼資訊
mode為"r":讀取文字檔案
mode為"w+":新建文字檔案並讀取,寫入資料
2. `int fclose(FILE *stream);`
:將緩衝區中為輸出資料輸出,清除輸入資料,接著關閉串流
3. `char *fgets(char *s, int n, FILE *stream);`
:從FILE stream取得單行字串(size為n而名稱為s的字串)
4. `size_t fwrite( void *ptr, size_t size, size_t nobj, FILE *stream)`
:將陣列ptr中具有nobj個元素,每個元素大小為size的資料寫到檔案中
5.`strcpy(wbuf, rbuf);`
把rbuf的字串複製到wbuf的字串中,然後回傳第一個參數
`#include <string.h>`
1. `void * memset ( void * ptr, int value, size_t num )`
:將ptr中前num byte填上value的值
`#include <sys/types.h>`
1. `off_t fsize(char *path);`
:用於設定檔案(path)的size
`#include <sys/stat.h>`
1. `struct stat(char filename,struct stat *buf)`
:顯示檔案或檔案的狀態
`struct stat`:是用來儲存檔案資訊的資料結構,包含檔案大小、類型、修改日期等資訊
程式問題:
1.wbuf是由system call `malloc()`配置的,結束時卻沒有`free`->memory leak
2.當輸入字串字數等於pad時,資料卻不會copy過去,這造成些資料的遺失
[參考LanKuDot的共筆](https://hackmd.io/s/HJFhaiAp)
### Refactoring
- 「在不改變軟體的外在行為之下,改善既有軟體的內部設計」
- 對於既有的context(既有的原始馬),需要重構的對象 (form)。因為無法平衡force而發出壞味道(smell),讓整個 form 也有壞味道。
### astyle
`$astyle --style=kr --indent=spaces=4 --indent-switches --suffix=none *.[ch]`
確認排版一致
### main.c
1.`file_align(DICT_FILE, ALIGN_FILE, MAX_LAST_NAME_SIZE);`
將DICT_FILE轉成ALIGN_FILE