I have a small example program which simply fopen
s a file and uses fgets
to read it. Using strace
, I notice that the first call to fgets
runs a mmap
system call, and then read system calls are used to actually read the contents of the file. on fclose
, the file is munmap
ed. If I instead open read the file with open/read directly, this obviously does not occur. I'm curious as to what is the purpose of this mmap
is, and what it is accomplishing.
On my Linux 2.6.31 based system, when under heavy virtual memory demand these mmap
s will sometimes hang for several seconds, and appear to me to be unnecessary.
The example code:
#include <stdlib.h>
#include <stdio.h>
int main ()
{
FILE *f;
if ( NULL == ( f=fopen( "foo.txt","r" )))
{
printf ("Fail to open\n");
}
char buf[256];
fgets(buf,256,f);
fclose(f);
}
And here is the relevant strace output when the above code is run:
open("foo.txt", O_RDONLY) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=9, ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb8039000
read(3, "foo\nbar\n\n"..., 4096) = 9
close(3) = 0
munmap(0xb8039000, 4096) = 0
Using strace , I notice that the first call to fgets runs a mmap system call, and then read system calls are used to actually read the contents of the file.
Memory-mapped (MMAP) file I/O is an OS-provided feature that maps the contents of a file on secondary storage into a program's address space. The program then accesses pages via pointers as if the file resided entirely in memory.
fopen is a function call, but it may sometimes be referred to as a system call because it is ultimately handled by the "system" (the OS). fopen is built into the C runtime library.
The mmap() function is used for mapping between a process address space and either files or devices. When a file is mapped to a process address space, the file can be accessed like an array in the program.
It's not the file that is mmap
'ed - in this case mmap
is used anonymously (not on a file), probably to allocate memory for the buffer that the consequent reads will use.
malloc
in fact results in such a call to mmap
. Similarly, the munmap
corresponds to a call to free
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With