*BSD News Article 16104


Return to BSD News archive

Path: sserve!newshost.anu.edu.au!munnari.oz.au!spool.mu.edu!howland.reston.ans.net!ira.uka.de!news.dfn.de!news.uni-bielefeld.de!rubb.rz.ruhr-uni-bochum.de!urmel.informatik.rwth-aachen.de!acds.physik.rwth-aachen.de!kuku
From: kuku@acds.physik.rwth-aachen.de (Christoph Kukulies)
Newsgroups: comp.os.386bsd.misc
Subject: mmap
Date: 16 May 1993 16:12:21 GMT
Organization: I.Physikalisches Institut RWTH-Aachen
Lines: 172
Distribution: world
Message-ID: <1t5p55$f1p@urmel.informatik.rwth-aachen.de>
Reply-To: kuku@acds.physik.rwth-aachen.de
NNTP-Posting-Host: acds.physik.rwth-aachen.de
Mime-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit


I'm wondering whether mmap under 386BSD is broken or if it's just again
my ignorance. Below is some sample code. It does not do what my
simple mind expects. Could anyone look at it and tell me if I'm doing something wrong or whether 386BSD is illbehaving?


# This is a shell archive.  Save it in a file, remove anything before
# this line, and then unpack it by entering "sh file".  Note, it may
# create directories; files and directories will be owned by you and
# have default permissions.
#
# This archive contains:
#
#	README
#	t.c
#	tt.c
#	w.c
#
echo x - README
sed 's/^X//' >README << 'END-of-README'
XThese three programs are used to test mmap functionality 
Xunder 386BSD.
X
XI could not get them to work as expected. Either I was doing something
Xwrong or the mmap routines are broken under 386BSD.
X
X
Xprograms are:
X
X
Xtt(tt.c)    creates a file of 1024 zeros named "dump" in the current
X            directory.
X
Xt (t.c)     usage:
X                    t dump
X            
X            mmaps the file 'dump' into memory
X            and continuously reads location zero (longword) until
X            it's different from 0.
X
Xw (w.c)     writes to location zero of that file ("dump"). until
X            it's different from 0.
X
XUse as follows:
X
Xcc -o tt tt.c
X
Xcc -o t t.c
X
Xcc -o w w.c
X
X./tt
Xrun ./t dump  in the background or a different window.
X
X./w dump    and type a number != 0
X
XI would expect that program t would find that the mmapped file was
Xmodified but it does not seem so.
X
X
X
XChristoph Kukulies
X16-May-93
END-of-README
echo x - t.c
sed 's/^X//' >t.c << 'END-of-t.c'
X#include <stdio.h>
X#include <fcntl.h>
X#include <sys/types.h>
X#include <sys/mman.h>
X
Xmain(argc,argv)
Xchar **argv;
Xint argc;
X{
X    int fd;
X    caddr_t memory;
X    if(argc!=2){
X	exit();
X    }
X    fd=open(argv[1],O_RDWR);
X    if(fd== -1){
X	perror("open");
X	exit(1);
X    }
X    printf("file dump opened successfully\n");
X    if((memory=mmap(0,1024,PROT_READ|PROT_WRITE,MAP_FILE,fd,0)) == -1){
X	perror("mmap");
X	exit(1);
X    }
X    printf("file dump mapped successfully\n");
X    fflush(stdout);
X    while(1){
X	printf("%08lX\r",(long)(*memory));
X        fflush(stdout);
X        sleep(1);
X        if((long)(*memory) != 0){
X		close(fd);
X		munmap(memory,1024);
X        	exit(0);
X	}
X    }
X}
END-of-t.c
echo x - tt.c
sed 's/^X//' >tt.c << 'END-of-tt.c'
X#include <fcntl.h>
Xstatic char buf[1024]={0};
Xmain()
X{
X    int fd=open("dump",O_RDWR|O_CREAT,0666);
X    if(fd==-1)
X	exit();
X    write(fd,buf,1024);
X    close(fd);
X}
END-of-tt.c
echo x - w.c
sed 's/^X//' >w.c << 'END-of-w.c'
X#include <stdio.h>
X#include <fcntl.h>
X#include <sys/types.h>
X#include <sys/mman.h>
Xchar str[80];
X
Xmain(argc,argv)
Xchar **argv;
Xint argc;
X{
X    int fd;
X    caddr_t memory,mmap();
X    if(argc!=2){
X	printf("use: w dump\n"),exit(1);
X    }
X    fd=open(argv[1],O_RDWR);
X    if(fd== -1){
X	perror("open");
X	exit(1);
X    }
X    printf("file dump opened successfully\n");
X    if((memory=mmap(0,1024,PROT_READ|PROT_WRITE,MAP_FILE,fd,0)) == (caddr_t)(-1)){
X	perror("mmap");
X	exit(1);
X    }
X    printf("file dump mapped successfully\ninput a number:");
X    fflush(stdout);
X    while(1){
X        (int)(*memory)=atoi(gets(str));
X    	msync(memory,1024);
X	printf("%08lX\r",(long)(*memory));
X    	fflush(stdout);
X    	sleep(1);
X	if((long)(*memory) != 0){
X            close(fd);
X            munmap(memory,1024);
X            exit(0);
X        }
X    }
X}
END-of-w.c
exit
--


--Chris
Christoph P. U. Kukulies
kuku@acds.physik.rwth-aachen.de
*** Error code 1
 
Stop.