Re: mmap & MAP_ANON/MAP_PRIVATE/MAP_SHARED

Andrew Pollard (andrew@odie.demon.co.uk)
Sun, 21 Sep 1997 13:25:36 +0100


Sorry that this contains a lot of quoting.... but I want to try and
get anonymous shared mappings into Linux....

Peter Belsanti wrote:
>Andrew Pollard wrote:
>>
>> Hello,
>>
>> I wonder if anyone can tell me whether Linux (2.0.31pre9 and 2.1.55)
>> should handle mmap with MAP_ANON|MAP_SHARED as an argument?
>
>I'm interested in the EXACT SAME THING!! This beats the pants out of
>[snip]
>My old posting was:
>
>> We have a piece of code that looks something like this:
>>
>> int *mapped;
>>
>> if ((mapped = (int*)mmap((caddr_t)0, 1024,
>> PROT_READ | PROT_WRITE,
>> MAP_SHARED | MAP_ANONYMOUS,
>> -1, 0)) == -1) {
>> perror("mmap");
>> exit(2);
>> }
>>
>> This ALWAYS fails and returns 0xffffff no matter what I try! I've
>> tried in all kernels up to 2.0.31-pre7. From the man pages, mmap()
>> should be able to accept MAP_ANONYMOUS but this just doesn't work.
>> Has anyone got this type of mmap() call to work in their code? Is
>> this feature supported in mmap()? Am I crazy? HELP!!!!!!
>
>The response I got:
>
>> Date: Thu, 28 Aug 1997 11:57:09 +0000 ( )
>> From: Benjamin C R LaHaise <blah@dot.superaje.com>
>>
>> This isn't implemented yet and won't be for 2.0.... Hopefully it will
>> be for 2.2, but don't expect it too soon.
>
>...and...
>
>> Date: Thu, 28 Aug 1997 07:59:34 -0400 (EDT)
>> From: Jim Nance <jlnance@avanticorp.com>
>>
>> This used NOT to work. I have not heard about any changes. You can
>> make something that looks like it work with:
>>
>> fd=open("/tmp/myfile", O_RDWR|O_CREAT);
>> unlink("/tmp/myfile");
>> ftruncate(fd, size);
>> ptr=mmap(0,???,MAP_SHARED,fd,??);
>> close(fd);
>>
>> Jim
>
>I too wait with baited breath for this feature to be put in!
>
>NOTE:
>1. If you are intereested in making your code portable, you should know
> that different platforms implement this feature in different ways.
> Some use the "-1" for a file descriptor, others mmap() /dev/zero for
> their memory. ALL O/S I've played with have this feature EXCEPT FOR
> LINUX! This includes SunOS, Solaris, AIX, OSF/1 and HPUX.

Yeah... know this :-) Is there any reason why Linux couldn't do both?

>2. Linux already has MAP_ANONYMOUS as long as there is a file
> descriptor, and MAP_SHARED as long as you don't declare
> MAP_ANONYMOUS, just not both! (All the pieces are there, just some
> glue code is required.)

OK. Does anyone out there in Linux Kernel land know what needs to be
done to get this functionality into the kernels? Even if it is just
getting one of the alternatives available (eg the one with a -1 file
descriptor). I'm quite willing to give it a go if I know what needs to
be done :-) (Actually, I want to get more into Linux development
anyway - and what better than to jump right into the VM subsystem :-)

I could do with it because I work from home a lot on my Linux based
PC, but our main targets are solaris2 and sunos, so I don't like
putting Linux specific bodges into the main source code :-)

Does anyone know if the 'Solaris2' way of doing it (mapping in
/dev/zero) is a 'bodge' checking for /dev/zero, or something else?

Thanks for any responses,
Andrew.

--
 Andrew Pollard, Auto Simulations Ltd. UK.  | home: andrew@odie.demon.co.uk
 2 Milbanke Court, Milbanke Way, Bracknell  | work: andrewp@autosim.com
Tel:+44(0)1344 426486 Fax:+44(0)1344 426615 | http://www.odie.demon.co.uk