LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-17-2011, 08:25 PM   #1
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
python: safe to mmap a tempfile (that may close while mapped)?


In python, tempfile.NamedTemporaryFile(...) creates a file "like" object that is visible in the fs.

mmap can read that file & treat it like an mmap would treat any normal file.

With a normal file, however, I can close the file, but still use the mmap (This is, in fact, true, though I'm not completely sure that it is safe). If I close the tempfile, it gets destroyed (http://docs.python.org/library/tempfile.html). In my tests, I see that I can still read the file with the mmap after it is destroyed (which also works with regular files after they are deleted, assuming nothing in the fs has changed or the file isn't massively truncated).

My question is this, and I think I know the answer, but I'd like to be sure: Is it safe to use a map of a file that no longer exists (especially a tempfile)? If not (which I assume is the case), is there a way to tell if the mmap points at a valid file?

Second question along the same lines: Is it safe to use an mmap of a file who's file descriptor has been closed? i.e.:
Code:
fp = open("foo")
map = mmap.mmap(fp.fileno(),length=0,access=mmap.ACCESS_READ)
fp.close()
map.seek(map.size())
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 10-18-2011, 10:19 AM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,783

Rep: Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214
Yes, it is safe to continue using a mapping of a file that has been closed and/or unlinked. The use of the word "destroy" in that Python document is extremely misleading. There is no system call to explicitly destroy a file. The kernel returns a file's inode and data blocks to the free pool when the link count in the inode goes to zero and no process is holding any reference to the inode. That reference could be either an open file descriptor or an mmap reference.

If you'd like to try an experiment, open a file on an otherwise idle file system (perhaps on a USB flash drive formatted with ext{2,3,4}), mmap it, and then unlink the file and close the file descriptor. While that process is still holding the mmap reference, go to another terminal and try to unmount the file system. You'll see that the file system is still busy due to the mmap, and you can also see that reference with 'lsof'. Now use 'df' to check the file system's space usage. You'll see that the file is still using space on the file system. When you release the mmap, you'll see the space usage drop and you will now be able to unmount the file system.

Last edited by rknichols; 10-18-2011 at 10:20 AM.
 
2 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Saving file data using Python in an embedded system in an safe and fast way Fabio Paolini Programming 7 04-24-2011 11:07 AM
python thread safety: printing from thread to redirected stdout - safe? BrianK Programming 2 10-11-2010 11:28 AM
memory mapped i/o and i/o mapped i/o shilpa rangappa General 1 12-14-2009 10:27 PM
Network device Memory mapped or Port mapped ?? Bignon Linux - Hardware 0 10-20-2009 08:36 AM
Python: when does one have to .close() ? bardinjw Programming 1 07-22-2005 01:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration