Why I can't map memory with mmap

Kevin O'Gorman kogorman at gmail.com
Mon Aug 18 11:15:12 EDT 2014


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;
}


-- 
Kevin O'Gorman

programmer, n. an organism that transmutes caffeine into software.
Please consider the environment before printing this email.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20140818/28874551/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: image/gif
Size: 441 bytes
Desc: not available
Url : http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20140818/28874551/attachment.gif 


More information about the Kernelnewbies mailing list