home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.c:12635 comp.unix.programmer:4402
- Path: sparky!uunet!mcsun!ieunet!!bsullivn
- From: bsullivn@sc.sni.ie (Bryan O'Sullivan)
- Newsgroups: comp.lang.c,comp.unix.programmer
- Subject: Re: memory map (mmap)
- Message-ID: <159@sc.sni.ie>
- Date: 17 Aug 92 10:10:30 GMT
- Article-I.D.: sc.159
- References: <1992Aug14.130645.26361@cbfsb.cb.att.com>
- Reply-To: bryan@sc.sni.ie
- Organization: Siemens-Nixdorf Software Development Centre, Dublin
- Lines: 81
- Followups-To: comp.unix.programmer
-
-
- X-Newsreader: Tin 1.1 PL3
-
-
- ddewar@cbnewsb.cb.att.com (derek.a.dewar) writes:
- :A quick questions about the mmap function in C....
-
- Minor semi-pedantic issue: your question should have been directed to
- comp.unix.programmer; it's got nothing much to do with the C language in
- general, as far as I can see. Anyway ...
-
- :After mapping the contents of a file into memory using the
- :mmap function, I tried to write something to the mapped segment
- :using write(2). I thought this would change the memory copy only,
- :but when I use the debugger I find that actaul contents of the file
- :have been changed as well as the memory image. My questions is,
- :what does "write" actually write to ? The memory image, or the file ?
- :(or both ?)
-
- Write(2) writes to the file; I'm not sure whether or not changes to the
- file in this fashion are reflected in the mapped pages (I think they
- are). You seem to have misinterpreted the use of mmap: it is used to
- write to a file without the direct overhead of a write() systems call
- for every change you make. It is _not_ intended to allow you to read in
- a file and change its image in memory only.
-
- : If it's the file, what function can I use to alter the contents of the
- : memory image only, to eliminate any need for disk I/O overhead ?
-
- You'll have to roll your own. Try something like the following:
-
- --------
-
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
-
- struct foo {
- off_t size;
- void *addr;
- };
-
- struct foo *readall(char *path)
- {
- struct stat buf;
- struct foo *bar;
- int fd;
-
- if ((fd = open(path, O_RDONLY)) < 0) {
- perror("open");
- exit(1);
- }
- if (fstat(fd, &buf) < 0) {
- perror("fstat");
- exit(1);
- }
- if ((bar = malloc(sizeof(struct foo)) == NULL) {
- perror("malloc");
- exit(1);
- }
- if ((bar->addr = malloc(bar->size = buf.st_size)) == NULL) {
- perror("malloc");
- exit(1);
- }
- if (read(fd, bar->addr, bar->size) < 0) {
- perror("read");
- exit(1);
- }
- return bar;
- }
-
- --------
-
- The above should read the entire file into a dynamically-allocated block
- of memory and return a pointer to a structure describing the allocated
- block. It does no checking to see if you're trying to read a fifo or
- special file (in which case buf.st_size is undefined).
-
- -- Bryan
-
- --
- Bryan O'Sullivan +353-1-767551 x225
- Siemens-Nixdorf Informationssysteme AG bryan@sc.sni.ie
- Software Development Centre "Theory like mist on eyeglasses.
- Dublin Obscure facts." -- Charlie Chan
-