سؤال

I'm trying to open a file with mmap: It just works fine when using MAP_PRIVATE, but MAP_SHARED causes a invalid argument error: The file for mmap ist read/write

int size;
struct stat s;
const char * file_name = argv[1];
int fd = open (argv[1], O_RDWR);
int pagesize = sysconf(_SC_PAGE_SIZE);

/* Get the size of the file. */
int status = fstat (fd, & s);
size = s.st_size;
size += pagesize-(size%pagesize);

//mmap memory
d = mmap (0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
//error handeling
if(d == -1)
{
    perror("mmap");
    printf("Error opening file %s\n",argv[1]);
    return -1;
}

What am I doing wrong?

هل كانت مفيدة؟

المحلول

I found out that the cause for the error was that I was using Ubuntu Linux in a VM (Parallels) when running the code on my native system everything worked fine. Seems that Parallels doesn't implement this kind of memory modifications in it's filesystem drivers...

This question helped me a lot: Invalid argument for read-write mmap?

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top