contribute by <andy19950
>
void *mmap(void *addr, size_t length, int prot, int flags,
int fd, off_t offset);
addr
: 要map到的記憶體位置,傳NULL系統會自行尋找一塊空間 mapping 完之後回傳。
length
: 要 mapping 的長度,它會從 fd + offset 的位置開始 mapping 檔案。
prot
: 不能跟 open mode 有衝突
PROT_EXEC
Pages may be executed.PROT_READ
Pages may be read.PROT_WRITE
Pages may be written.PROT_NONE
Pages may not be accessed.flags
: 選擇 map file 能不能被其他程式看到
fd
: 用系統函式 open 打開的檔案位置,open mode 可以選擇檔案的讀寫屬性,不能跟 prot 有衝突。
offset
: 從檔案的哪裡開始 mapping,offset 必須是 page size 的倍數,可以用 sysconf(_SC_PAGE_SIZE) 取得。
mmap
是 UNIX 系統呼叫 jserv
在這份程式碼中,缺乏驗證資料正確性的機制,請嘗試逐一輸出 last name 到一份檔案,再跟
dictionary/words.txt
比對。 jserv
while(fgets(line, sizeof(line), fp)) {
e = pHead;
printf("%s", line);
if(strcmp(findName(line, e)->lastName, line) != 0) {
printf("ERROR : Name %s is not in linked-list!!\n", line);
}
}
./phonebook-concurrent > output.txt
搜尋 ERROR
之後找不到就代表 linked-list 內部資料完全正確E486: 找不到 ERROR
mmap
可在記憶體映射 (map) 給定檔案時,指定存取模式,請詳細閱讀 man page: mmap(2) jserv
謝謝老師,正在想有沒有辦法解決呢!! andy19950
下面有修改過 findName 的版本所以刪掉的地方就當做沒看到吧 andy19950
int fd = open(ALIGN_FILE, O_RDWR | O_NONBLOCK);
char *map = mmap(NULL, fs, PROT_READ | PROT_WRITE,
MAP_SHARED, fd, 0);
while (current != NULL) {
if (strncasecmp(lastName, current->lastName, len) == 0) {
current->lastName[len] = '\0';
current->dtl = (detail *) malloc( sizeof(detail));
return current;
}
current = current->pNext;
}
threadpool_t *threadpool_create(int thread_count,
int queue_size,
int flags);
int threadpool_add(threadpool_t *pool,
void (*routine)(void *),
void *arg, int flags);
int threadpool_destroy(threadpool_t *pool, int flags);
threadpool_create
:傳入 thread 的數量,queue 的大小,回傳 threadpool 型態是 threadpool_tthreadpool_add
:傳入 threadpool,要執行的function位置,要傳入的參數threadpool_destroy
:傳入 threadpool,會把 threadpool 的空間 free 掉flags 只有 destroy 會用到, threadpool_graceful = 1,其他的傳 NULL 就可以了。
pthread_create
-> threadpool_create
+ threadpool_add
pthread_join
-> threadpool_destroy
threadpool_t *pool = threadpool_create(THREAD_NUM, 512, NULL);
for (int i = 0; i < THREAD_NUM; i++) {
threadpool_add(pool, &append, app[i], NULL);
}
threadpool_destroy(pool, 1);
我們只對 append() 有興趣,可以單獨製圖,然後調整 Y 軸刻度,得以比較不同 thread 數量對效能的影響。讓視覺效果更顯著 jserv
Reference