Why I can't map memory with mmap
Jeff Haran
Jeff.Haran at citrix.com
Mon Aug 18 12:42:33 EDT 2014
From: kernelnewbies-bounces at kernelnewbies.org [mailto:kernelnewbies-bounces at kernelnewbies.org] On Behalf Of Kevin O'Gorman
Sent: Monday, August 18, 2014 8:15 AM
To: kernelnewbies at kernelnewbies.org
Subject: Why I can't map memory with mmap
maybe someone here can help me figure out how to map a big (really big) work area and lock it in memory.
I wrote a little test program to try this out, and it fails. As a regular user, perror() tells me some "resource is temporarily unavailable". As root, it says it "cannot allocate memory". I'm only asking for 1 byte. What's the problem? BTW it also fails if it asks for a mibibyte (1<<20).
Here's the whole program
/**
* @file
* <pre>"Find out the limits on locked memory"
* Last Modified: Mon Aug 18 07:31:01 PDT 2014</pre>
* @author Kevin O'Gorman
*/
#define _GNU_SOURCE /* enable some of the mmap flags */
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
int
main(int argc, char *argv[])
{
void *where;
size_t length = 1;
where = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_LOCKED | MAP_POPULATE, -1, 0);
if (where != MAP_FAILED) {
printf("Mapped at %p\n", where);
} else {
perror("Mapping failed");
}
return EXIT_SUCCESS;
}
I'm pretty sure you need to pass the file descriptor of an open file or device as the fd (second parameter from the end). -1 is not a valid fd.
Jeff Haran
More information about the Kernelnewbies
mailing list