mmap does not 'update' the file.

mmap does not 'update' the file.

Post by TRAN PHAN A » Sat, 18 May 1996 04:00:00



Hello,

I played a little bit with mmap/munmap() but I cannot get the thing to
work.  I want to map a file to memory, write to that mapped region and
see updates in the file itself.

The output as I run the program is correctly:
Z:a

But when I do a 'more' on the "test.mem" Z is still there, and I cannot see
'a' and 'b'.  Thanks for all help.

Anh

PS.  Oh, I stripped the error checking I had.  The code does run without
any error.  I am running this on ALPHA/Digital Unix 3.2.

#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <sys/stat.h>

#include <errno.h>
#include <unistd.h>

extern char *sys_errlist[];

int main(int argc, char *argv[])
{
  int fd;
  caddr_t m;
  int pagesize;

  fd=open("test.mem",O_RDWR|O_CREAT);
  lseek(fd,32767,SEEK_SET);
  write(fd,"Z",1);
  lseek(fd,0,SEEK_SET);

  m=mmap((caddr_t)0,32768,PROT_READ|PROT_WRITE,
         MAP_FILE|MAP_VARIABLE|MAP_PRIVATE,fd,0);

  close(fd);

  printf("%c:",m[32767]);
  m[32767]='a';
  printf("%c\n",m[32767]);

  m[1]='b';
  m[2]='c';

Quote:}

 
 
 

mmap does not 'update' the file.

Post by Andrew Gier » Sun, 19 May 1996 04:00:00




Quote:>Hello,

>I played a little bit with mmap/munmap() but I cannot get the thing to
>work.  I want to map a file to memory, write to that mapped region and
>see updates in the file itself.

>The output as I run the program is correctly:
>Z:a

>But when I do a 'more' on the "test.mem" Z is still there, and I cannot see
>'a' and 'b'.  Thanks for all help.

>Anh

>PS.  Oh, I stripped the error checking I had.  The code does run without
>any error.  I am running this on ALPHA/Digital Unix 3.2.

>#include <sys/types.h>
>#include <sys/mman.h>
>#include <fcntl.h>
>#include <sys/stat.h>

>#include <errno.h>
>#include <unistd.h>

>extern char *sys_errlist[];

>int main(int argc, char *argv[])
>{
>  int fd;
>  caddr_t m;
>  int pagesize;

>  fd=open("test.mem",O_RDWR|O_CREAT);
>  lseek(fd,32767,SEEK_SET);
>  write(fd,"Z",1);
>  lseek(fd,0,SEEK_SET);

>  m=mmap((caddr_t)0,32768,PROT_READ|PROT_WRITE,
>        MAP_FILE|MAP_VARIABLE|MAP_PRIVATE,fd,0);

                               ^^^^^^^^^^^

Quote:>  close(fd);

>  printf("%c:",m[32767]);
>  m[32767]='a';
>  printf("%c\n",m[32767]);

>  m[1]='b';
>  m[2]='c';
>}

MAP_PRIVATE implies that changes made to the mapped region are not
visible in the original file or in any other mapping of the region.

To change the data globally, you want MAP_SHARED instead.

You should also check up on msync(); implementations differ as to
when changes made in a mapped region become visible in the backing
file unless msync() is called at appropriate times.


"How is this place run - is it an anarchy?"
"No, I wouldn't say so; it is not that well organised..."

 
 
 

mmap does not 'update' the file.

Post by ISHIGAMI Hidetos » Fri, 24 May 1996 04:00:00



Quote:> The output as I run the program is correctly:
> Z:a

> But when I do a 'more' on the "test.mem" Z is still there, and I cannot see
> 'a' and 'b'.  Thanks for all help.
(snip)
>   m=mmap((caddr_t)0,32768,PROT_READ|PROT_WRITE,
>     MAP_FILE|MAP_VARIABLE|MAP_PRIVATE,fd,0);

  You should use MAP_SHARED instead of MAP_PRIVATE.

Ishigami.
--
---------------------------------------------------------------
ISHIGAMI Hidetoshi                          TOSHIBA Corporation
NASU Works             System & Software Engineering Department
---------------------------------------------------------------

 
 
 

1. mmap'd file ends with '!', copy doesn't

I'm sorry, I don't have an answer to your problem but wanted to comment (and
ask) about something..

 #define DIE(X) do { fprintf(stderr, "%s: %s '%s' failed: %s\n", progname,
X, \
file, strerror(errno)); exit(EXIT_FAILURE); } while (0)

Why do people do this? Is there some advantage ?

It looks odd but maybe just because I've never used it (nor have I seen it
used that much) :-)

I've seen it a lot lately in some code I've been looking over and have
wondered what the advantage of defining small functions like that is (if
any)..

Thanks!

-Mitch

2. Good book for Linux??

3. 'uname' is not updated after installing new '2.4.20' kernel??

4. FIFO Question

5. Is e2label 'dangerous' when done on a 'live' file system?

6. Need Drivers for External (Paradrive) CD-ROM Drive CAPHMO?

7. Odd Solaris problem: file is seen by 'vi' but not 'ls' ???

8. Do I need to keep unzipped patch directories?

9. SLS update: 'a' - 'c'

10. 'cat file' but only if 'file' exist

11. 'select' failure or signal should not update timeout

12. Coverting '.so' files to '.o' files

13. File size of 'tmp' files for 'vi'