第二次调用mmap失败,错误代码为22。我不知道第二次mmap调用有什么问题。请帮帮忙。
typedef int fd_t;
int
main (int argc, char **argv) {
fd_t fd;
FILE *file;
const char *file_name = "db-sample.txt";
if (access(file_name, F_OK) == -1) {
/* File do not exist or we dont have permissions,
then create the file*/
file = fopen(file_name, "w+");
}
else {
file = fopen(file_name, "r+");
}
if (!file) assert(0);
fd = fileno (file);
if (ftruncate(fd, 8192 * 64) == -1) {
assert(0);
}
disk_io_write_to_file (fd, 0, "Hello World", strlen ("Hello World"));
lseek(fd, 0, SEEK_SET);
assert (write (fd, "Hello World", strlen("Hello World") - 1) != -1 );
void *ptr1 = mmap (NULL, 8, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (ptr1 == MAP_FAILED) {
printf ("%d : Error : mmap failed with error %d\n", __LINE__, errno);
exit(0);
}
void *ptr2 = mmap (NULL, 8192, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 8);
if (ptr2 == MAP_FAILED) {
printf ("%d : Error : mmap failed with error %d\n", __LINE__, errno);
exit(0;
}
munmap (ptr1, 8);
munmap (ptr2, 8192);
return 0;
}
字符串
1条答案
按热度按时间bmp9r5qi1#
第二个mmap调用中指定的偏移量不是系统页大小的倍数,在大多数现代系统中,系统页大小为4KiB