0

I came across something I've never seen before in the Linux pthreads documentation here: http://linux.die.net/man/3/pthread_mutexattr_init

What I'm referring to is the below:

int fd = open(....);
void* ptr = mmap(....);
close (fd);
return ptr;

How can ptr still be valid if the file is closed? I was under the impression that the file has to be open throughout the life-time of the map. Then when you are ready to destroy it, you munmap first THEN you close..

Can someone explain why this code is valid? Does this also hold true for windows?

Brandon
  • 22,723
  • 11
  • 93
  • 186
  • 1
    Why should memory mapping a file be connected to having a stream connected to it? That said, it is the same under MS Windows, though it used to be different under early versions of MS Windows CE. – Ulrich Eckhardt Feb 09 '15 at 21:32
  • 2
    File handles in Linux are reference-counted, so the file doesn't need to be kept open (or rather, `mmap` will ensure it stays open!) – Mats Petersson Feb 09 '15 at 21:37

0 Answers0