[問題] 關於Linux寫檔問題?

看板LinuxDev作者 (haha)時間16年前 (2008/12/26 12:09), 編輯推噓1(101)
留言2則, 1人參與, 最新討論串1/1
簡單的程式 如附錄二 但是在讀寫檔案時, 出現了一些不可思議的結果一 但是我看dd的原始碼 (http://www.koders.com/c/fid8FA83D210BAE79DA3A7C4088AC9C54909DC88C8D.aspx) 小弟的東西根本和dd一模一樣, 但是dd到了 bs=4 後, 速度都很平均, 這才正常 而小弟的程式出現不正確的結果, 請問問題到底出在哪呢? ================附錄一================================= Block Size: 2 Speed: 1.375332 MB/S Block Size: 4 Speed: 2.805992 MB/S Block Size: 8 Speed: 6.129276 MB/S Block Size: 16 Speed: 11.170536 MB/S Block Size: 32 Speed: 26.849388 MB/S Block Size: 64 Speed: 48.011832 MB/S Block Size: 128 Speed: 97.772515 MB/S Block Size: 256 Speed: 158.443034 MB/S Block Size: 512 Speed: 282.057241 MB/S Block Size:1024 Speed: 470.192368 MB/S ====================================================== =================附錄二================================ #include <stdio.h> #include <string.h> #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/time.h> #include <stdlib.h> #define MAX_Block 1024 //byte #define FileSize 5 //MB int main(int argc, char **argv){ char *buf; int outfd; double t1,t2; struct timeval timep; int BUF_SIZE, i; int TempFileSize = 1024*1024 * FileSize; //5MB BUF_SIZE = 2; while( BUF_SIZE <= MAX_Block) { buf = (char *)malloc(BUF_SIZE); memset(buf, 'a' , BUF_SIZE); outfd = open("tempfile", O_WRONLY | O_CREAT | O_TRUNC, 0666); gettimeofday(&timep, NULL); t1 = timep.tv_sec+(timep.tv_usec/1000000.0); for(i=0; i< TempFileSize/BUF_SIZE; i++){ write(outfd, buf, BUF_SIZE); }//for gettimeofday(&timep, NULL); t2=timep.tv_sec+(timep.tv_usec/1000000.0); close(outfd); printf("Block Size:%4d\tSpeed: %.6lf MB/S\n",BUF_SIZE, FileSize/(t2-t1) ); free(buf); BUF_SIZE = BUF_SIZE * 2; }//while }//main -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.120.70.34

12/26 12:47, , 1F
應該是 buffer cache 的關係...
12/26 12:47, 1F

12/26 12:54, , 2F
試試看使用 O_DIRECT,但是不一定 work...
12/26 12:54, 2F
文章代碼(AID): #19L5ZvH5 (LinuxDev)
文章代碼(AID): #19L5ZvH5 (LinuxDev)