views:

262

answers:

2

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.

+2  A: 

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.

Robert S. Barnes
the pointer is in virtual address space which is mapped to the actual file. now performing read and write to the memory pointed to by this pointer does not require a system call. is that what you are saying
iamrohitbanga
@iamrothitanga: 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.
Robert S. Barnes
By the way, why was this comment marked down?
Robert S. Barnes
+1  A: 

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.

Aaron Digulla
if multiple processes are accessing a file through mmap. then they are accessing the same bits in physical memory. without system calls. their page table entries are marked as dirty and ... . that is what i understand from your answer
iamrohitbanga
Different processes can share the same memory. You can have shared access, so writes by one process will be seen by other processes, or you can use copy-on-write where a process gets an individual copy of a page as soon as they start writing to it.
Aaron Digulla