Thread (2 messages) 2 messages, 2 authors, 2014-08-18

Why I can't map memory with mmap

From: Jeff Haran <hidden>
Date: 2014-08-18 16:42:33

From: kernelnewbies-bounces@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
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help