Search code examples
cmemory-mapping

C: mmap failed: No such device


I'm trying to create a memory map using the c commands

  void* mem_map = mmap(NULL, 
                       sizeof(serverData),      //200000
                       PROT_READ | PROT_WRITE, 
                       MAP_SHARED, 
                       mem_map_fp, 
                       0);

  if(mem_map == MAP_FAILED){
    bail_out(EXIT_FAILURE, "mmap");
  }

The program compiles, but when trying to run the following error is produced:

mmap: No such device

To my understanding there is nothing wrong with the code which makes me suspect that the reason might be a little more complex. I'm running this linux version:

Linux ubuntu 4.2.0-16-generic #19-Ubuntu SMP Thu Oct 8 15:35:06 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

Solution

  • I suppose you are trying to allocate memory, so you should use the MAP_ANON or MAP_ANONYMOUS flag, along with the standard arguments -1 for the file descriptor and 0 for the offset, like so :

    mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);