🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.sys.next.programmer
Article #1623

MMAP Works (was Re: mmap - System call in 3.0 still there ???)

#1623
From: Matthew Dillon
Date: Sat, 26 Sep 1992 20:23
88 lines
2318 bytes
>In article <1992Sep25.115239.12000@odi.com> benson@odi.com (Benson I. Margulies) writes:
>
>mmap works.
>
>But the rules are different.
>
>First, call vm_allocate to make the space.
>
>Then, call mmap, passing in the address you got.
>
>Don't expect to get the address back from mmap. MAP_FIXED is, of
>course, implied.
>
>--
>Benson I. Margulies

    Thank you sir!

    mmap() does indeed work when you use vm_allocate() ... and you can
    indeed share WRITABLE pages across unrelated processes.

    NeXT -- For Gods sake, DOCUMENT IT!

    Here's my test program...

					-Matt

/*
 *  MMAP()
 *
 *
 *  Create a junk file greater then 8K, bring up two shells and run
 *  this program in each one as follows:
 *
 *  % mmap junk_file
 *
 *  % mmap junk_file xxx
 *
 *  note that you can't simply use file descriptor 0 and redirect because
 *  the descriptor is open read-only.
 */

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

main(ac, av)
int ac;
char *av[];
{
    void *addr;
    long r;
    int fd;

    fd = open(av[1], O_RDWR);
    if (fd < 0)
	perror("open");

    if (vm_allocate(task_self(),(vm_address_t *)&addr,8192,TRUE)!=KERN_SUCCESS)
	perror("vm_alloc");
    printf("addr %08lx\n", addr);

    r = mmap(addr, 8192, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
    printf("r= %d\n", r);

    if (ac == 2) {
	for (;;) {
	    sleep(1);
	    printf("%d\n", *(volatile char *)addr);
	}
    } else {
	for (;;) {
	    sleep(1);
	    printf("write\n");
	    ++*(volatile char *)addr;
	}
    }
}



--

    Matthew Dillon		dillon@Overload.Berkeley.CA.US
    1005 Apollo Way		uunet.uu.net!overload!dillon
    Incline Village, NV. 89451	ham: KC6LVW (no mail drop)
    USA 			Sandel-Avery Engineering (702)831-8000
Technical Details
Message-ID: <dillon.0oe9@overload.Berkeley.CA.US>
Path: arc1-ca.pugleaf.net!sparky!uunet!overload!dillon
References: <1992Sep19.120245.1274@gamelan> <dillon.0oba@overload.Berkeley.CA.US> <1992Sep25.115239.12000@odi.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!overload!dillon
From: dillon@overload.Berkeley.CA.US (Matthew Dillon)
Newsgroups: comp.sys.next.programmer
Subject: MMAP Works (was Re:  mmap - System call in 3.0 still there ???)
Distribution: world
Message-ID: <dillon.0oe9@overload.Berkeley.CA.US>
References:  <1992Sep19.120245.1274@gamelan> <dillon.0oba@overload.Berkeley.CA.US> <1992Sep25.115239.12000@odi.com>
Date: 26 Sep 92 12:23:33 PST
Organization: Not an Organization
Lines: 89