[問題] fwrite 寫入時間異常

看板C_and_CPP作者 (高原)時間11年前 (2014/06/12 01:18), 編輯推噓2(204)
留言6則, 5人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) Linux Scientific 6.2 , GCC 4.4.6 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 由於研究的關係我寫了一個測試fwrite的小程式,每次寫入51200Byte大小的binary資料 ,程式碼如下,結果顯示大部分每次花0.1ms,但有時候會有100~200ms的出現,不知道 這種現象造成的原因是什麼? 如果需要長時間穩定寫入時間,是否有其他方式可以處理? 程式碼(Code):(請善用置底文網頁, 記得排版) #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <fcntl.h> #include <string.h> int main(){ FILE *time; FILE *test; float time_use=0; struct timeval start; struct timeval end; int size; int i=0; test=fopen("file.dat","wb"); time=fopen("time1.txt","w"); for(i=1;i<40000;i++){ size=51200; unsigned char a[size]; gettimeofday(&start,NULL); fwrite(a,sizeof(char),size,test); gettimeofday(&end,NULL); time_use=(end.tv_sec-start.tv_sec)*1000000+(end.tv_usec-start.tv_usec); fprintf(time,"size=%d\ttime=%f\n",size,time_use); } fclose(time); fclose(test); return 0; } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 180.177.9.172 ※ 文章網址: http://www.ptt.cc/bbs/C_and_CPP/M.1402507086.A.64E.html

06/12 01:35, , 1F
我猜是實際寫到硬碟才花較久
06/12 01:35, 1F

06/12 01:36, , 2F
時間的差異應該是硬碟的暫存造成的
06/12 01:36, 2F

06/12 06:51, , 3F
組NAS/RAID 使用logical volume理論上比較穩 但是要慎選廠牌
06/12 06:51, 3F

06/12 17:17, , 4F
fwrite會先進buffer, 不妨試 open/write
06/12 17:17, 4F

06/12 18:46, , 5F
正常的
06/12 18:46, 5F

06/12 18:47, , 6F
換ssd吧
06/12 18:47, 6F
文章代碼(AID): #1Jc8zEPE (C_and_CPP)