【发布时间】:2014-03-23 23:11:35
【问题描述】:
我正在使用 pread 一次获取大量数据。
但如果我尝试收集大量数据(例如 100mb)并将其保存到数组中,我会遇到段错误......
对 pread 可以读取的最大字节数有硬性限制吗?
#define _FILE_OFFSET_BITS 64
#define BLKGETSIZE64 _IOR(0x12,114,size_t)
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <inttypes.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
int readdata(int fp,uint64_t seekpoint, uint64_t seekwidth) {
int16_t buf[seekwidth];
if (pread(fp,buf,seekwidth,seekpoint)==seekwidth) {
printf("SUCCES READING AT: %"PRIu64"| WITH READ WIDTH: %"PRIu64"\n",seekpoint,seekwidth);
return 1;
} else {
printf("ERROR READING AT: %"PRIu64"| WITH READ WIDTH: %"PRIu64"\n",seekpoint,seekwidth);
return 2;
}
}
int main() {
uint64_t readwith,
offset;
int fp=open("/dev/sdc",O_RDWR);
readwith=10000; offset=0;
readdata(fp,offset,readwith);
readwith=100000; offset=0;
readdata(fp,offset,readwith);
readwith=1000000; offset=0;
readdata(fp,offset,readwith);
readwith=10000000; offset=0;
readdata(fp,offset,readwith);
readwith=10000000; offset=0;
readdata(fp,offset,readwith);
readwith=100000000; offset=0;
readdata(fp,offset,readwith);
readwith=1000000000; offset=0;
readdata(fp,offset,readwith);
readwith=10000000000; offset=0;
readdata(fp,offset,readwith);
readwith=100000000000; offset=0;
readdata(fp,offset,readwith);
readwith=1000000000000; offset=0;
readdata(fp,offset,readwith);
readwith=10000000000000; offset=0;
readdata(fp,offset,readwith);
readwith=100000000000000; offset=0;
readdata(fp,offset,readwith);
readwith=1000000000000000; offset=0;
readdata(fp,offset,readwith);
close(fp);
}
【问题讨论】:
-
我会说你正在抓取比你想要的更多的东西,并试图读取比文件末尾更远的字节。这可以解释段错误。你能粘贴你的代码吗?
-
你确定你调用了一个足够大的缓冲区来存储数据的 pread() 吗?
-
您是在读入堆栈分配的缓冲区,还是使用
malloc分配的缓冲区?后者对于非常大的缓冲区要可靠得多。 -
堆栈分配。稍后我将发布示例代码。截至目前,它太长了。我堆栈分配,因为如果我没有太多,我真的不想处理 malloc...
-
是的,所以你试图将 10 兆字节读入 8 兆字节堆栈。