Subject: mmap problem ?
To: None <netbsd-users@NetBSD.ORG>
From: Joerg Ueberla <ueberla@signal.dra.hmg.gb>
List: netbsd-users
Date: 04/23/1997 21:32:56
Hi,
I am having a problem when trying to use mmap to modifiy an existing
file and was wondering whether anybody could help me with it.
I mmap a file for read and write access and change some of it. I
then observe two problems
1) when the file is very large (just over 218Meg), some of the modifications
seem to be written, some not. (this only seems to be a pb on networked files,
it works on a local file).
2) the fact whether I look at some of the fields under gdb or not, seems to
influence whehter it works or not. For example, even on a small (12 byte)
local file, it doesn't work when I print those bytes from within gdb (with
p *PInt@3) before I unmap it, but works fine if I don't print it [the
print statements in all cases gives the correct value, it just doesn't seem
to go through to the file].
Now am I doing s.th. really stupid (e.g. not use the mmap call in the right
manner) or is there a bug? I'll append the small pg I am using (<50 lines)
at the end of this mail. It works fine when compiled f.ex. on alphas, but not
on netbsd. any help would be much appreciated - since I am not a regular
reader of this list, it would be great if you could email your comments
to ueberla@signal.dra.hmg.gb
cheerio,
Joerg Ueberla
---------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
main(int argc, char **argv)
{
int *PInt, NewVal, i, NbBytes, Ret;
char FName[500];
struct stat stat_buf;
caddr_t Map;
FILE *FP;
//sprintf(FName, "/home/ueberla/proj/src/lib/f1");
sprintf(FName, "%s", argv[1]);
NewVal=atoi(argv[2]);
FP=fopen(FName, "r+");
fstat(fileno(FP), &stat_buf);
NbBytes=stat_buf.st_size;
#if defined(__NetBSD__)
Map=(caddr_t)mmap(NULL, NbBytes, PROT_WRITE|PROT_READ, MAP_FILE|MAP_SHARED, fileno(FP), 0);
#else
Map=(caddr_t)mmap(NULL, NbBytes, PROT_WRITE|PROT_READ, MAP_FILE|MAP_VARIABLE|MAP_SHARED, fileno(FP), 0);
#endif
if(Map==(caddr_t)-1){
exit(-1);
}
PInt=(int *)Map;
for(i=0;i<NbBytes/((int)sizeof(int));i++){
if(i%1000000==0){
fprintf(stdout,"i=%d\n", i);
fflush(stdout);
}
PInt[i]=NewVal;
}
fprintf(stdout,"PInt[0]=%d\n", PInt[0]);
Ret=munmap(Map, NbBytes);
if(Ret!=0){
exit(-1);
}
Ret=fclose(FP);
if(Ret!=0){
exit(-1);
}
}