これが私の問題の説明です:
Cのシステムコールを使って6.3GB程度の大きなファイルを全てメモリに読み込みたいのですread
が、エラーが発生します。コードは次のとおりです。
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <limits.h>
int main(int argc, char* argv[]) {
int _fd = open(argv[1], O_RDONLY, (mode_t) 0400);
if (_fd == -1)
return 1;
off_t size = lseek(_fd, 0, SEEK_END);
printf("total size: %lld\n", size);
lseek(_fd, 0, SEEK_SET);
char *buffer = malloc(size);
assert(buffer);
off_t total = 0;
ssize_t ret = read(_fd, buffer, size);
if (ret != size) {
printf("read fail, %lld, reason:%s\n", ret, strerror(errno));
printf("int max: %d\n", INT_MAX);
}
}
そしてそれをコンパイルします:
gcc read_test.c
次に実行します:
./a.out bigfile
出力:
total size: 6685526352
read fail, 2147479552, reason:Success
int max: 2147483647
システム環境は
3.10.0_1-0-0-8 #1 SMP Thu Oct 29 13:04:32 CST 2015 x86_64 x86_64 x86_64 GNU/Linux
私が理解していない2つの場所があります:
- 大きなファイルの読み取りは失敗しますが、小さなファイルでは失敗しません。
- エラーが出ていても
errno
が正しく設定されていないようです。