Passing -1 as file descriptor to mmap

Active3 hr before
Viewed126 times

7 Answers


But I am not sure how to get the file descriptor that I would pass to mmap.,To memory map a local file, I would first open the file and get its file descriptor using open and then use mmap to memory map the file, like this -,Since the file is located in HDFS, is it even possible to get a file descriptor?,If it is not, then how would I memory map a file that I read from HDFS? Or in other words, how do I get foo for the second code snippet above?

To memory map a local file, I would first open the file and get its file descriptor using open and then use mmap to memory map the file, like this -

int fd = open("somefile.txt", 0)
char * foo = (char * ) mmap(0, size_in_bytes, PROT_READ, MAP_FILE | MAP_SHARED, fd, 0);
load more v

© 2021, O’Reilly Media, Inc. All trademarks and registered trademarks appearing on are the property of their respective owners.,Features Certifications Interactive learning Live events Answers , Teams For business For government For higher ed Individuals Features Certifications Interactive learning Live events Answers Blog Content sponsorship ,Teams For business For government For higher ed


mmap(filedesc, length, tagname = '') # Windows
mmap(filedesc, length, flags = MAP_SHARED,
   prot = PROT_READ | PROT_WRITE) # Unix

Here are some hints: ,Run usertests to make sure everything still works.

Fetch the xv6 source for the lab and check out the mmap branch:

  $ git fetch
  $ git checkout mmap
load more v

Unmaps the mapping for the specified address range addr, which must be the virtual address returned by a previous call to mmap by the same process that has not yet been unmapped.,Initializes the file-backed page subsystem. In this function, you can setup anything related to the file backed page.,Destroys the file backed page by closing the associated file. If the content is dirty, make sure you write back the changes into the file. You do not need to free the page struct in this function. The caller of file_backed_destroy should handle it.,All mappings are implicitly unmapped when a process exits, whether via exit or by any other means. When a mapping is unmapped, whether implicitly or explicitly, all pages written to by the process are written back to the file, and pages not written must not be. The pages are then removed from the process's list of virtual pages.

void * mmap(void * addr, size_t length, int writable, int fd, off_t offset);
load more v

The library offers access to the following functions:,It also provides a convenience macro called with-mmap to perform safe, local mappings of files.,This is a utility library providing access to the mmap family of functions in a portable way. It should work on Posix and Windows systems. mmap allows you to directly map a file into the address space of your process without having to manually read it into memory sequentially. Typically this is much more efficient for files that are larger than a few Kb.,Portable mmap (file memory mapping) utility library.

It also provides a convenience macro called with-mmap to perform safe, local mappings of files.

(mmap: with - mmap(addr fd size #p "/etc/lsb-release")
   (with - output - to - string(out)
         for i from 0 below size
         for char = (code - char(cffi: mem - aref addr: char i)) do(write - char char out))))
load more v

mmap() function - Linux, C++ compilation issues By Rupan in forum C++ Programming Replies: 1 Last Post: 08-22-2005, 05:45 AM ,Community Boards Article Discussions Tech Board General Discussions A Brief History of Contests Board Projects and Job Recruitment ,Creating a game, from start to finish

unsigned long memory(unsigned long want_value) {
   int fd;
   unsigned long * data, ret;

   fd = open("/dev/mem", O_RDWR | O_SYNC); //file descriptor

   data = mmap(((unsigned long * ) want_value), 4, PROT_READ, MAP_SHARED, fd, 0); // returns actual memory address

   ret = * ((unsigned long * )(data));

   munmap(((unsigned long * ) want_value), 4);


   return ret;

Created on 2013-10-30 05:19 by rfm, last changed 2016-05-07 08:13 by martin.panter. This issue is now closed.

Same code but different problem as this issue: http: //

   The above - linked issue was closed as WONTFIX, but there is a secondary problem: the file descriptor that mmap.mmap() allocates is not set to close - on - exec.

This means that any process you spawn using os.system() and the like, its children, its children 's children etc will inherit this file descriptor.

This is unexpected, unnecessary behavior, and potentially a huge pain
if you mmap() a file and then spawn, say, inetd or something that does similar stuff, because now you have to kill every process that spawned from that in order to unmount filesystems or remove kernel modules that are being blocked as a result of the fds being open.
load more v

Other "passing-undefined" queries related to "Passing -1 as file descriptor to mmap"