【发布时间】:2018-10-04 10:03:58
【问题描述】:
如何使用 C++ 为二进制文件的读写操作创建 linux I/O 基准测试?
我尝试使用下面的代码生成一个 10MB 的文件,但它返回“分段错误(核心转储)”。
#define FILE_SIZE 10240
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int main()
{
int fd, i;
int stream[FILE_SIZE];
double span;
clock_t start,end;
start=clock();
fd=open("data.bin", O_CREAT|O_TRUNC|O_WRONLY, S_IRWXU);
srand(time(NULL));
for(i=0; i<FILE_SIZE; i++){
stream[i]=rand()%2;
write(fd, (char*)stream[i], strlen((char*)stream[i]));
}
for(i=0; i<FILE_SIZE; i++)
read(fd, (char*)stream[i], strlen((char*)stream[i]));
close(fd);
end=clock();
time_lapse=((double)(end-start))/CLOCKS_PER_SEC;
return 0;
}
然后由于写入和读取功能中包含的转换,它返回给我一些警告,我确信有更好的方法来做到这一点。
谁能帮帮我?
提前致谢, 安东尼奥
【问题讨论】:
-
您的标签和标题是 C++,但您显示的代码本质上是 C。
标签: c++ linux input output benchmarking