Re: mmap, /dev/zero, and MAP_SHARED

ncherry@home.net
Fri, 26 Nov 1999 20:17:54 -0500


Jamie Lokier wrote:
>
> ncherry@home.net wrote:
> > a = mmap (0, 8192, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FILE, fd, 0);
> >
> > a always returns as -1 and errno is 22 (EINVAL). I've changed all the
> > EINVAL's to EINVAL+n (n is a constant not a variable) I've replaced n by
> > 1 - 22 in the mm directory and the above code still returns -1/EINVAL.
> >
> > Why?
>
> Because shared anonymous mappings (MAP_ANON could be used instead of
> /dev/zero) are not implemented.
>
> Use MAP_PRIVATE instead of MAP_SHARED and it succeeds of course, but
> probably doesn't do what you want.
>
> You best solutions are to use a temporary file instead of /dev/zero
> (unlink it straight after opening if you like), or use shared memory
> (shmget et al.).

Thanks. I refinde my search a bit more and with all the info I gathered
I finally figured out a few things. The first is that /dev/zero isn't
what I thought it was. The second is that MAP_SHARED works on files (not
character special, just files). I've not been able get MAP_ANON and
MAP_SHARE to work. But I've discovered that to avoid bus errors one needs
to open a file that already contains at least the number of bytes you're
going to work with or to create a file and write enough bytes to it to
avoid the bus error.

The solution I've chosen (for now) is mkstemp, unlink, mmap, close and
fork.

I don't like the fact it writes to a file but it at least looks like
variables, I'm used to working with those. I need to get some good books
on Unix programming. The Steven's series is very good but I need further
references. Alternate thinking is always a good thing.

-- 
Linux Home Automation           Neil Cherry             ncherry@home.net
http://members.home.net/ncherry                         (Text only)
http://meltingpot.fortunecity.com/lightsey/52           (Graphics GB)

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.tux.org/lkml/