title: In linux , how to create a file descriptor for a memory region
body: <p>I have some program handling some data either in a file or in some memory buffer.  I want to provide uniform way to handle these cases.</p>
  
  <p>I can either 1) mmap the file so we can handle them uniformly as a memory buffer; 2) create FILE* using fopen and fmemopen so access them uniformly as FILE*.</p>
  
  <p>However, I can't use either ways above.  I need to handle them both as file descriptor, because one of the libraries I use only takes file descriptor, and it does mmap on the file descriptor.</p>
  
  <p>So my question is, given a memory buffer (we can assume it is aligned to 4K), can we get a file descriptor that backed by this memory buffer?  I saw in some other question popen is an answer but I don't think fd in popen can be mmap-ed.</p>