linuxmmaptmpfs

delete file in tmpfs(/dev/shm) after mmap the same file


I did some search but didn't find similar questions. Here is my steps:

  1. copy a file to /dev/shm (tmpfs)
  2. mmap that file with lock
  3. read data from that file
  4. delete the file

After step 4, the previous loaded data is still available, why? Thanks in advance


Solution

  • On Unix, a file's data is not deleted until every remaining process closes/munmaps it.

    Since you've opened the file and have an active handle or mapping to it, you can continue reading and writing the file data for as long as you want. It will not be freed until after you close it.