1

Output of lsof on my RHEL7 shows that one file with file descriptor mem is used by 40 processes. Does it mean that this file is mapped in memory 40 times or what? Could someone please explain what does memory mapped files mean? Does it mean that it 40 times in my memory?

# lsof /usr/lib/locale/locale-archive
COMMAND     PID     USER  FD   TYPE DEVICE  SIZE/OFF     NODE NAME
vmtoolsd    605     root mem    REG    8,5 106070960 50808629 /usr/lib/locale/locale-archive
agetty      656     root mem    REG    8,5 106070960 50808629 /usr/lib/locale/locale-archive
tuned       963     root mem    REG    8,5 106070960 50808629 /usr/lib/locale/locale-archive
iostat     1199      adm mem    REG    8,5 106070960 50808629 /usr/lib/locale/locale-archive
chkMtaMem  1205     root mem    REG    8,5 106070960 50808629 /usr/lib/locale/locale-archive
snmpd      4704     root mem    REG    8,5 106070960 50808629 /usr/lib/locale/locale-archive
sleep      5461     root mem    REG    8,5 106070960 50808629 /usr/lib/locale/locale-archive
cmsubagt   6487     root mem    REG    8,5 106070960 50808629 /usr/lib/locale/locale-archive
sleep      6649     root mem    REG    8,5 106070960 50808629 /usr/lib/locale/locale-archive
proc1      6803     root mem    REG    8,5 106070960 50808629 /usr/lib/locale/locale-archive
proc2      6835     root mem    REG    8,5 106070960 50808629 /usr/lib/locale/locale-archive
proc3      6836     root mem    REG    8,5 106070960 50808629 /usr/lib/locale/locale-archive
proc4      6856     root mem    REG    8,5 106070960 50808629 /usr/lib/locale/locale-archive
proc5      6884     root mem    REG    8,5 106070960 50808629 /usr/lib/locale/locale-archive
proc6      6889      usr mem    REG    8,5 106070960 50808629 /usr/lib/locale/locale-archive
proc7      6893     usr1 mem    REG    8,5 106070960 50808629 /usr/lib/locale/locale-archive
cmfpagt    7704     root mem    REG    8,5 106070960 50808629 /usr/lib/locale/locale-archive
proc8      7943     root mem    REG    8,5 106070960 50808629 /usr/lib/locale/locale-archive
crond      8001     root mem    REG    8,5 106070960 50808629 /usr/lib/locale/locale-archive
sh         8005      adm mem    REG    8,5 106070960 50808629 /usr/lib/locale/locale-archive
iostat     8014      adm mem    REG    8,5 106070960 50808629 /usr/lib/locale/locale-archive
crond     20427     root mem    REG    8,5 106070960 50808629 /usr/lib/locale/locale-archive
proc9     20648     root mem    REG    8,5 106070960 50808629 /usr/lib/locale/locale-archive
proc10    20649     root mem    REG    8,5 106070960 50808629 /usr/lib/locale/locale-archive
proc10    20760     usr2 mem    REG    8,5 106070960 50808629 /usr/lib/locale/locale-archive
proc9     20777     usr2 mem    REG    8,5 106070960 50808629 /usr/lib/locale/locale-archive
proc11    21353     root mem    REG    8,5 106070960 50808629 /usr/lib/locale/locale-archive
proc12    21354     root mem    REG    8,5 106070960 50808629 /usr/lib/locale/locale-archive
proc13    21355     root mem    REG    8,5 106070960 50808629 /usr/lib/locale/locale-archive
proc14    21356     root mem    REG    8,5 106070960 50808629 /usr/lib/locale/locale-archive
proc15    21357     root mem    REG    8,5 106070960 50808629 /usr/lib/locale/locale-archive
proc16    21358     root mem    REG    8,5 106070960 50808629 /usr/lib/locale/locale-archive
proc17    21554     root mem    REG    8,5 106070960 50808629 /usr/lib/locale/locale-archive
proc18    21569     usr2 mem    REG    8,5 106070960 50808629 /usr/lib/locale/locale-archive
proc19    21590     usr2 mem    REG    8,5 106070960 50808629 /usr/lib/locale/locale-archive
proc20    21647     usr2 mem    REG    8,5 106070960 50808629 /usr/lib/locale/locale-archive
proc21    22016     root mem    REG    8,5 106070960 50808629 /usr/lib/locale/locale-archive
proc22    22017     root mem    REG    8,5 106070960 50808629 /usr/lib/locale/locale-archive
proc23    22104     usr2 mem    REG    8,5 106070960 50808629 /usr/lib/locale/locale-archive
proc24    22122     usr2 mem    REG    8,5 106070960 50808629 /usr/lib/locale/locale-archive
  • Can you update your question and copy/paste (i.e., don't include a screenshot) of the output of lsof to which you're referring? – Andy Dalton May 31 '18 at 13:20
  • @AndyDalton, thanks. Updated the description. – sys463 May 31 '18 at 13:47
1

Have a look at the difference between virtual and physical memory.

Many processes can map the same physical memory. If 10 processes map the same file, then at most one copy will be cached in RAM.

If memory is not-shared, then if one process changes it, then this one page (with the change), is duplicated before committing the write. (So not all of the memory is copied. This is called copy on write or COW).

A memory mapped file is when you ask the OS to map a file into memory. It does not load the file until you start reading/writing and then only what is needed. Memory mapping is just a different interface to read/write/seek/etc. You can also access memory through read/write/seek.

So how is this done?

It is done by realizing that some things are the same: That is swap and files are the same; and ram and file cache are the same. So when you open a file it is mapped as swap (don't worry it won't be used to swap out other stuff). When you start reading a seg-fault is generated, and the OS swaps it in, from the file (The exception (seg-fault) is handled by the OS so not passed on to a process) (actually it is not a segmentation fault, it is a page fault). It does not matter what interface you use memory or file. They are just interfaces over the same functionality.

  • Thanks for such a capacious answer, @ctrl-alt-delor ! Now it's clearer. – sys463 May 31 '18 at 17:12
2

Yes, by the manual of lsof:

   FD         is the File Descriptor number of the file or:

                   cwd  current working directory;
                   Lnn  library references (AIX);
                   err  FD information error (see NAME column);
                   jld  jail directory (FreeBSD);
                   ltx  shared library text (code and data);
                   Mxx  hex memory-mapped type number xx.
                   m86  DOS Merge mapped file;
                   mem  memory-mapped file;
                   mmap memory-mapped device;
                   pd   parent directory;
                   rtd  root directory;
                   tr   kernel trace file (OpenBSD);
                   txt  program text (code and data);
                   v86  VP/ix mapped file;

It's only if it's mapped in non-shared mode and any process writes to the virtual memory that would require more than one copy of the file in physical memory. Otherwise if no writing occurs (which I don't think) the data doesn't even have to occupy physical memory before any process reads the memory.

  • thanks for your answer. I think, this file is only opened for reading. So, that file doesn't occupy physical memory, does it? – sys463 May 31 '18 at 13:58
  • 2
    If it's opened read-only, then only the pages (= 4 KB blocks) of the file that are actually being read are needed in memory. Those pages are automatically shared between all the processes needing that file. And if memory is needed more urgently elsewhere, the kernel can just take away those memory pages and reuse them after marking the address space allocated for them as paged out; if a process needs one of those pages later, it can simply be paged in by reading from the original file again. – telcoM May 31 '18 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.