[comp.unix.programmer] mmap

coleman@sundae9.DAB.GE.COM (Richard Coleman) (10/01/90)

-- 
I was looking at the system call mmap().  I not sure what
the purpose of this system call is.  Could someone help.


           Richard Coleman
           G.E. Simulation & Control Systems
           coleman@sunny.dab.ge.com

cpcahil@virtech.uucp (Conor P. Cahill) (10/02/90)

In article <5994@ge-dab.GE.COM> coleman@sundae9.DAB.GE.COM (Richard Coleman) writes:
>I was looking at the system call mmap().  I not sure what
>the purpose of this system call is.  Could someone help.

On systems that support it, it allows you to map a portion (or all) of a 
file into your memory space.

This allows you to change the contents of the file by simply assigning
to the data area in your mapped area.  For example, if mmptr is a pointer
to the memory mapped region, memset(mmptr,'\0',512); will clear the first
512 bytes of the file.


-- 
Conor P. Cahill            (703)430-9247        Virtual Technologies, Inc.,
uunet!virtech!cpcahil                           46030 Manekin Plaza, Suite 160
                                                Sterling, VA 22170 

symon@aesop.cs.unc.edu (James Symon) (10/03/90)

In article <5994@ge-dab.GE.COM>, coleman@sundae9.DAB.GE.COM (Richard Coleman) writes:
> 
> I was looking at the system call mmap().  I not sure what
> the purpose of this system call is.  Could someone help.
> 

Besides regular files, the file that you map to your program`s data
space can be some device. The call to mmap() will use the mmap routine
from the device driver. This method is often used with custom hardware
to allow direct access to device registers through program variables.
With root permission you can memory map directly to bus addresses, at
least on Suns, using the various address spaces such as vme24d16. If
you have Sun documentation look at the document about writing device
drivers. I`ve also seen a book about writing UNIX device drivers that
can help you understand mmap().

Jim Symon
symon@radonc.unc.edu
symon@cs.unc.edu
(919) 966-7710

pefv700@perv.pe.utexas.edu (05/10/91)

I'm trying to understand how to use mmap(2) by RTM and am stumped.
Of course, the whole concept here is probably escaping me (read the header,
I'm no CS major).

So does anyone have a short and sweet cat(1)-like example that will
broaden my system call knowledge (at least a little)?

Chris

pfalstad@phoenix.princeton.edu (Paul Falstad) (05/10/91)

pefv700@perv.pe.utexas.edu wrote:
>I'm trying to understand how to use mmap(2) by RTM and am stumped.

>So does anyone have a short and sweet cat(1)-like example that will
>broaden my system call knowledge (at least a little)?

Here's a quick version of cat, without error checking:

-----------------------------------------------------------------------------
#include <stdio.h>
#include <sys/file.h>
#include <sys/mman.h>

main(argc,argv)
int argc;char **argv;
{
char *buf;
int fd,pgsiz,len,off;

   pgsiz = getpagesize()*3;
   fd = open(argv[1],O_RDONLY);
   len = lseek(fd,0,2);
   off = 0;
   buf = NULL;
   for (;;) {
      buf = mmap(buf,pgsiz,PROT_READ,MAP_PRIVATE,fd,off);
      if (len < pgsiz) {
         write(1,buf,len);
         exit(0);
      }
      write(1,buf,pgsiz);
      off += pgsiz;
      len -= pgsiz;
   }
}
-----------------------------------------------------------------------------

This dumps the file in blocks of (pagesize*3).  I picked this value
arbitrarily; the only restriction is that pgsiz must be an integer
multiple of the system page size.

Note that the first time mmap() is called, buf is 0, so the system will
map the file wherever it wants.  After that, the system will map the
file into the same place as before (so we don't accumulate mappings).

--
              Paul Falstad  pfalstad@phoenix.princeton.edu
         And on the roads, too, vicious gangs of KEEP LEFT signs!
     If Princeton knew my opinions, they'd have expelled me long ago.