In Python on Windows I can create a large file by
from mmap import mmap
f = open('big.file', 'w')
f.close()
f = open('big.file', 'r+')
m = mmap(f.fileno(), 10**9)
And now big.file
is (about) 1 gigabyte. On Linux, though, this will return ValueError: mmap length is greater than file size
.
Is there a way to get the same behavior on Linux as with Windows? That is, to be able to increase a file's size using mmap
?
In computing, mmap(2) is a POSIX-compliant Unix system call that maps files or devices into memory. It is a method of memory-mapped file I/O. It implements demand paging because file contents are not immediately read from disk and initially use no physical RAM at all.
What happens with mmap is that the data remains on disk, and it is copied from disk to memory as your process reads it. It can also be copied to physical memory speculatively.
The mmap() function establishes a mapping between a process' address space and a stream file. The address space of the process from the address returned to the caller, for a length of len, is mapped onto a stream file starting at offset off.
The offset argument specifies the first byte of the file to be mapped. The offset must be an exact multiple of the system page size (4096 bytes). Note: See the IBM UNIX System Services Assembler Callable Services manual for additional information about the behavior of mmap and the conditions under which it can be used.
On POSIX systems at least, mmap()
cannot be used to increase (or decrease) the size of a file. mmap()
's function is to memory map a portion of a file. It's logical that the thing you request to map should actually exist! Frankly, I'm really surprised that you would actually be able to do this under MS Windows.
If you want to grow a file, just ftruncate()
it before you mmap()
it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With