English 中文(简体)
memory mapped files system call - linux
原标题:

When we map a file to memory, a system call is required. Do subsequent accesses to the file require system calls or is the virtual memory page of the process mapped to the actual page cache in memory?

update: what i also want to know is that if multiple processes are accessing the same file through mmap. they will be accessing the same physical memory portion write.

最佳回答

No need for additional system calls ( by your process ), you just access it like regular memory. When you re done with the file, just call munmap.

Return Value

On success, mmap() returns a pointer to the mapped area. On error, the value MAP_FAILED (that is, (void *) -1) is returned, and errno is set appropriately. On success, munmap() returns 0, on failure -1, and errno is set (probably to EINVAL).

See the man page here for details.

Edit For clarification:

I m saying that the function maps the file into the calling process s memory space and returns a pointer to the beginning of the memory block.

For example, if you have two different processes map the same file with the MAP_SHARED flag then each process will be accessing the same physical memory, but that memory may be mapped at a different location in each process s virtual memory space, i.e. the pointers returned by mmap in each process s virtual memory space may not be equal.

This brings up the point that if you for instance need to store pointers inside the shared memory block those pointers would only be useful if they were stored as offsets relative to the beginning of the block / file and they would only be able to usefully point to locations internal to the block / file.

问题回答

When you mmap a file, Linux creates entries in the MMU (memory management unit). The MMU watches over all reads and writes of the CPU to the real RAM. This way, it knows when you access parts of the memory which mmap() returned. Reading parts which are not yet in the real RAM will cause page faults. The MMU will catch them and call a kernel routine to load the right part of the file into RAM somewhere and then it will update the entry in the MMU table so it appears that the data is now located at the address which mmap() gave you. In fact, it will be somewhere else but the MMU will make this completely transparent.

When you write to the memory, the MMU will mark the modified pages as "dirty". When they are flushed out (because you access more of the file or because you call munmap()), then the changes will be written out to disk.

So every time a page fault and a dirty page flush happens, a system call happens. But since pages are 4 or 8KB, these happen rarely. Also, the kernel will load more than a single page at a time, so the number of system calls is reduced again. Lastly, the same code is used to implement swapping, so it is very optimized.

All these effects make mmap so efficient.





相关问题
Signed executables under Linux

For security reasons, it is desirable to check the integrity of code before execution, avoiding tampered software by an attacker. So, my question is How to sign executable code and run only trusted ...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How can I use exit codes to run shell scripts sequentially?

Since cruise control is full of bugs that have wasted my entire week, I have decided the existing shell scripts I have are simpler and thus better. Here is what I have so far svn update /var/www/...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

热门标签