Memory mapped files

Hi folks,

What is wrong with this code? I think it is ok, but I always get: “map
failed : Not supported”

int main(){
int fd, i, c;
struct stat buf;
unsigned char *addr;

fd =open("/test.txt", O_RDWR);
if (fd<0){
fprintf(stderr, “File not found : %s\n”,
strerror(errno));
return -1;
}

if( stat( FILE2MAP, &buf ) == -1 ) {
printf(“stat failed\n”);
return -1;
}

printf("\r\nWe try to allocate %d Bytes\r\n", buf.st_size);
// addr = (unsigned char *)mmap(0, buf.st_size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_FIXED, fd, 0);
addr = (unsigned char *)mmap(0, buf.st_size, PROT_READ, MAP_SHARED, fd,
0);

if (addr == MAP_FAILED){
fprintf(stderr, “\r\n\r\nmmap failed : %s\n”,
strerror(errno));
return EXIT_FAILURE;
}
}

Who can help me?

Hello Martin,

We are aware of this issue and there is already PR for it (13668).

A small note:
Since you get file descriptor I would personally use fstat() instead of
stat().

Best Regards,

Marcin

“Martin Binner” <m.binner@caa.de> wrote in message
news:av4jm8$5pd$1@inn.qnx.com

Hi folks,

What is wrong with this code? I think it is ok, but I always get: “map
failed : Not supported”

int main(){
int fd, i, c;
struct stat buf;
unsigned char *addr;

fd =open("/test.txt", O_RDWR);
if (fd<0){
fprintf(stderr, “File not found : %s\n”,
strerror(errno));
return -1;
}

if( stat( FILE2MAP, &buf ) == -1 ) {
printf(“stat failed\n”);
return -1;
}

printf("\r\nWe try to allocate %d Bytes\r\n", buf.st_size);
// addr = (unsigned char *)mmap(0, buf.st_size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_FIXED, fd, 0);
addr = (unsigned char *)mmap(0, buf.st_size, PROT_READ, MAP_SHARED,
fd,
0);

if (addr == MAP_FAILED){
fprintf(stderr, “\r\n\r\nmmap failed : %s\n”,
strerror(errno));
return EXIT_FAILURE;
}
}

Who can help me?

Martin Binner <m.binner@caa.de> wrote:

What is wrong with this code? I think it is ok, but I always get:
“map failed : Not supported”

Since QNX does not have full integration between the OS and filesystem
memory buffers, memory-mapped files do not work as in traditional UNIX.
Try specifying the MAP_NOSYNCFILE flag to your mmap(); you’ll probably
also have to explicitly msync() it yourself when you are done with it.

John Garvey <jgarvey@qnx.com> wrote:

Martin Binner <> m.binner@caa.de> > wrote:
What is wrong with this code? I think it is ok, but I always get:
“map failed : Not supported”
Since QNX does not have full integration between the OS and filesystem
memory buffers, memory-mapped files do not work as in traditional UNIX.
Try specifying the MAP_NOSYNCFILE flag to your mmap(); you’ll probably
also have to explicitly msync() it yourself when you are done with it.

By the way, this only applies because you tried to get write access;
if you just opened O_RDONLY and mapped PROT_READ, then your example
should work and give you read-only memory-mapped access to the file …