Re: mmap & MAP_ANON/MAP_PRIVATE/MAP_SHARED

Peter Belsanti (pbelsant@algorithmics.com)
Fri, 19 Sep 1997 10:44:04 -0400


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
shmget() since anonymous memory gets cleaned up automatically after
coring! (I know, apps are NOT supposed to core dump, but this isn't a
perfect world and some ayaadmin's don't both checking servr conditions
regularily!)

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.

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.)