English 中文(简体)
How portable is mmap?
原标题:

I ve been considering using mmap for file reading, and was wondering how portable that is. I m developing on a Linux platform, but would like my program to work on Mac OS X and Windows.

Can I assume mmap is working on these platforms?

最佳回答

The mmap() function is a POSIX call. It works fine on MacOS X (and Linux, and HP-UX, and AIX, and Solaris).

The problem area will be Windows. I m not sure whether there is an _mmap() call in the POSIX compatibility sub-system. It is likely to be there — but will have the name with the leading underscore because Microsoft has an alternative view on namespaces and considers mmap() to intrude on the user name space, even if you ask for POSIX functionality. You can find a definition of an alternative Windows interface MapViewOfFile() and discussion about performance in another SO question (mmap() vs reading blocks).

If you try to map large files on a 32-bit system, you may find there isn t enough contiguous space to allocate the whole file in memory, so the memory mapping will fail. Do not assume it will work; decide what your fallback strategy is if it fails.

问题回答

Using mmap for reading files isn t portable if you rely on mapping large bits of large files into your address space - 32-bit systems can easily not have a single large usable space - say 1G - of address space available so mmap would fail quite often for a 1G mapping.

The principle of a memory mapped file is fairly portable, but you don t have mmap() on Windows (but things like MapViewOfFile() exist). You could take a peek at the python mmap modules c code to see how they do it for various platforms.

I consider memory mapped io on UNIXs as not useable for interactive applications, as it may result in a SIGSEGV/SIGBUS (in case of the file has been truncated meanwhile by some other process). Ignoring such sick "solutions" as setjmp/longjmp there is nothing one can do other than to terminate the process after getting SIGSEGV/SIGBUS. The new G++ feature to convert such signals into exceptions seems to be intended mainly for apples OS, since the description states, that one needs runtime support for this G++ feature and there is no information to be found about this G++ feature anywhere. We probably have to wait a couple of years, until structured exception handling like it can be found on windows since more than 20 years makes its way into UNIXs.





相关问题
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 ...

热门标签