Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increasing a file's size using mmap

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?

like image 791
chew socks Avatar asked Jun 30 '13 18:06

chew socks


People also ask

Does mmap consume RAM?

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.

Where is mmap stored?

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.

What is the use of mmap?

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.

What is offset mmap?

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.


1 Answers

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.

like image 118
Celada Avatar answered Sep 17 '22 03:09

Celada