mmap not share

Corinna Vinschen cygwin@cygwin.com
Tue Jul 10 14:21:00 GMT 2001


On Tue, Jul 10, 2001 at 07:47:13PM +0200, Corinna Vinschen wrote:
> On Mon, Jun 25, 2001 at 08:48:59PM +0200, Dirk Freyer wrote:
> > Under Linux this file works fine. Two tasks increments one counter :-)
> > With Windows 98 i have the problem that the two tasks increments two counters :-(
> > 
> > Why ???
> > 
> > I use cygwin 1.8.2
> > 
> >   int fd;
> >   void *ptr=NULL;
> >   int *counter;
> >  
> >   if ( (fd=open("tmem.sm",O_RDWR)) == -1) printf("File Error\n");
> >   else 
> >   if((ptr = mmap (NULL,sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == -1)
> >         printf("mmap Error\n");
> >   close(fd);
> >   printf("File=%i Ptr=%i\n",fd,ptr);
> >   counter=ptr;
> >   while (TRUE) {
> >       printf("counter=%i\n",(*counter)++);
> > 	...
> >   }
> 
> Ok, I just had an extensive test on this. It happens only on
> 9x/ME, not on NT/W2K. For some reason the Win32 API call MapViewOfFile()
> doesn't return the same address in both processes as it's described
> in MSDN. The mapped objects have no clue of each other. Even synching
> using the FlushViewOfFile() function inside of msync() doesn't show
> an effect since the other process happily overrides that the next time.
> 
> I have an idea to implement sth. especially for 9x/ME since I suspect
> a special undocumented behaviour. Don't know if that will work but if so,
> I will apply that to Cygwin and keep the list informed.

Ok, it works. I have applied the patch to Cygwin so it's part
of the next developers snapshot (and the next official release of
course).

The whole thing is that 9x/ME only allows sharing of mapped objects 
between processes when the first process _creates_ the mapping giving
an object name to CreateFileMapping() and all following processes have
to _open_ the object by using that name in an OpenFileMapping() call.
If different processes all _create_ a mapping object, they are all
disjunctive. This is different on NT/W2K where the mapping is bound
to the real object which is mapped and not to the virtual object which
is created by CreateFileMapping().

The tricky part is to create a unique name for a file due to the
way the file information is passed to mmap. The used method isn't
bullet proof but it should work most of the time. One failing
situation I can think of (I didn't test it, though) is the following:

- 2 processes, both are in the same dir "/bar":

  - P1:		fd = open("foo");
  		mmap(fd);

  - P2:		fd = open("foo");
  		cd("..");
		mmap(fd);

while that works:

  - P2:		fd = open("/bar/foo");
  		cd("..");
		mmap(fd);

Hope it helps, nevertheless,
Corinna

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Developer                                mailto:cygwin@cygwin.com
Red Hat, Inc.

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/



More information about the Cygwin mailing list