我需要打开一个文件并通过mmap将其加载到共享内存中,但如果文件尚不存在,我想要打开它,向其中写入一些(虚假)数据,然后再进行mmap。我在C语言中编写了以下函数,但是在写入时出现了错误(请参见下面)。 (我知道mmap部分可能是错误的(data被分配了两次!),但是错误发生在此之前,因此不应对此问题产生任何影响)。
// These 2 are global so they can be referenced in other functions.
int dfd = -1;
long* data = NULL;
void load_data(char* filename)
{
dfd = open(filename, O_RDONLY);
if (dfd == -1) {
printf("Creating file %s\n", filename);
dfd = open(filename, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
if (dfd == -1) {
fprintf(stderr, "Couldn't create file %s\n", filename);
perror("create");
exit(1);
}
data = (long *) valloc(M * GB);
if (data == nullptr) {
fprintf(stderr, "Couldn't allocate %ld bytes", (M * GB));
perror("malloc");
exit(1);
}
for (size_t i = 0; i < M * GB / sizeof(long); ++i)
data[i] = (long) i;
printf("%d %p %ld\n", dfd, data, M * GB);
ssize_t w = write(dfd, (void*) data, M * GB);
if (w != M * GB) {
fprintf(stderr, "Couldn't write %ld bytes to file %s\n", (M * GB), filename);
fprintf(stderr, "Wrote %ld bytes\n", w);
perror("write");
exit(1);
}
}
data = (long *) mmap(0, M * GB, PROT_READ, MAP_SHARED, dfd, 0);
if (data == MAP_FAILED) {
perror("mmap");
exit(1);
}
}
在MacOS 64位系统上,使用Apple g++编译器时,输出和错误信息如下:
Creating file bench2_datafile.bin
3 0x101441000 2147483648
Couldn't write 2147483648 bytes to file bench2_datafile.bin
Wrote -1 bytes
write: Invalid argument
有什么指针问题吗?我一直在阅读打开和写入文档,并在互联网上寻找示例,但似乎无法解决此错误。
经过评论后的输出:
RHEL 6、g++ 4.8 上的输出:
Creating file bench2_datafile.bin
3 0x7f79048af000 2147483648
write: Success
Couldn't write 2147483648 bytes to file bench2_datafile.bin
Wrote 2147479552 bytes
2147479552确实是ls中的文件大小。
此外,在Mac上使用1 GB是可行的,但2 GB会不够用。嗯,我的真正目标是Linux,只是在解决问题之前在Mac上更方便而已 :-)
open
而不是fopen
?您可以使用fopen(filepath, "r")
测试文件是否存在,如果不存在,则使用fopen(filepath, "w")
进行写入,然后继续进行文件存在时的操作。 - Meik Vtunemmap()
需要一个像open()
返回的那样的int
类型文件描述符。此外,fopen()
/fwrite()
缓冲写操作-在这种情况下并不必要。 - Andrew Henle