Questions tagged [mmap]

mmap is a POSIX-compliant Unix system call that maps files or devices into memory.

mmap() creates a new mapping in the virtual address space of the calling process.

https://en.wikipedia.org/wiki/Mmap

1936 questions
34
votes
3 answers

How to portably extend a file accessed using mmap()

We're experimenting with changing SQLite, an embedded database system, to use mmap() instead of the usual read() and write() calls to access the database file on disk. Using a single large mapping for the entire file. Assume that the file is small…
Dan Kennedy
  • 468
  • 1
  • 5
  • 9
33
votes
5 answers

In malloc, why use brk at all? Why not just use mmap?

Typical implementations of malloc use brk/sbrk as the primary means of claiming memory from the OS. However, they also use mmap to get chunks for large allocations. Is there a real benefit to using brk instead of mmap, or is it just tradition?…
Nate C-K
  • 5,744
  • 2
  • 29
  • 45
33
votes
3 answers

Does malloc() use brk() or mmap()?

c code: // program break mechanism // TLPI exercise 7-1 #include #include void program_break_test() { printf("%10p\n", sbrk(0)); char *bl = malloc(1024 * 1024); printf("%x\n", sbrk(0)); free(bl); …
Eric
  • 22,183
  • 20
  • 145
  • 196
33
votes
4 answers

Why use shm_open?

What's the advantage of doing: shm_open followed a mmap? Why not create a regular file, and then pass that fd to mmap? I can't see the advantage of shm_open - these are just references, are they not? I've read the man of the whole family. It seems…
Trevor
  • 1,858
  • 4
  • 21
  • 28
33
votes
4 answers

When would you use mmap

So, I understand that if you need some dynamically allocated memory, you can use malloc(). For example, your program reads a variable length file into a char[]. You don't know in advance how big to make your array, so you allocate the memory in…
Steve Walsh
  • 6,363
  • 12
  • 42
  • 54
30
votes
7 answers

Driving Beaglebone GPIO through /dev/mem

I'm trying to write a C program for blinking a LED on the Beaglebone. I know I can use the sysfs way...but I'd like to see if it is possible to get the same result mapping the physical address space with /dev/mem. I have a header file,…
Salvatore
  • 1,145
  • 3
  • 21
  • 42
29
votes
6 answers

How big can a memory-mapped file be?

What limits the size of a memory-mapped file? I know it can't be bigger than the largest continuous chunk of unallocated address space, and that there should be enough free disk space. But are there other limits?
user88185
  • 293
  • 1
  • 3
  • 4
27
votes
3 answers

Sharing memory between processes through the use of mmap()

I'm in Linux 2.6. I have an environment where 2 processes simulate (using shared memory) the exchange of data through a simple implementation of the message passing mode. I have a client process (forked from the parent, which is the server) which…
Andrea Sprega
  • 2,221
  • 2
  • 29
  • 35
27
votes
5 answers

appending to a memory-mapped file

I'm constantly appending to a file of stock quotes (ints, longs, doubles, etc.). I have this file mapped into memory with mmap. What's the most efficient way to make newly appended data available as part of the memory mapping? I understand that I…
Joel Reymont
  • 861
  • 2
  • 7
  • 16
27
votes
2 answers

Using mmap and madvise for huge pages

I want to allocate memory on the hugepages being used by a Linux machine. I see that there are two ways to do this, using mmap and madvise. That is, using the MAP_HUGETLB flag with the mmap call - base_ptr_ = mmap(NULL, memory_size_, PROT_READ |…
ssb
  • 7,422
  • 10
  • 36
  • 61
26
votes
3 answers

What if I allocate memory using mmap instead of malloc?

What are the disadvantages of allocating memory using mmap (with MAP_PRIVATE and MAP_ANONYMOUS) than using malloc? For data in function scope, I would use stack memory anyway and therefore not malloc. One disadvantage that comes to mind is for…
MetallicPriest
  • 29,191
  • 52
  • 200
  • 356
25
votes
3 answers

How to access(if possible) kernel space from user space?

How exactly is user memory and kernels memory differentiated inside the Linux kernel(in terms of giving security to kernel space)? What are the different ways I can write in kernel address space from user space? One way I know is through a system…
Sandeep
  • 18,356
  • 16
  • 68
  • 108
25
votes
5 answers

How to read lines from a mmapped file?

Is seems that the mmap interface only supports readline(). If I try to iterate over the object I get character instead of complete lines. What would be the "pythonic" method of reading a mmap'ed file line by line? import sys import mmap import…
Maxim Veksler
  • 29,272
  • 38
  • 131
  • 151
25
votes
1 answer

Does mmap directly access the page cache, or a copy of the page cache?

To ask the question another way, can you confirm that when you mmap() a file that you do in fact access the exact physical pages that are already in the page cache? I ask because I’m doing testing on a 192 core machine with 1TB of RAM, on a 400GB…
quinthar
  • 443
  • 4
  • 8
24
votes
4 answers

Linux MMAP internals

I have several questions regarding the mmap implementation in Linux systems which don't seem to be very much documented: When mapping a file to memory using mmap, how would you handle prefetching the data in such file? I.e. what happens when you…
Mr Jay