From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f8c9b,3319fd08e07c7b1f X-Google-Attributes: gidf8c9b,public X-Google-Thread: fee84,3319fd08e07c7b1f X-Google-Attributes: gidfee84,public X-Google-ArrivalTime: 1993-11-16 05:50:43 PST Xref: gmd.de comp.unix.osf.osf1:1528 comp.unix.questions:24440 Newsgroups: comp.unix.osf.osf1,comp.unix.questions Path: gmd.de!xlink.net!howland.reston.ans.net!usenet.ins.cwru.edu!eff!news.kei.com!world!mv!synergy.Synergytics.Com!marc From: marc@synergy.Synergytics.Com (Marc Evans) Subject: Re: using mmap() in OSF Nntp-Posting-Host: synergy.synergytics.com References: Sender: usenet@mv.mv.com (Robert Chesler) Organization: Synergytics Date: Tue, 16 Nov 1993 12:02:40 GMT Message-ID: Reply-To: Marc@Synergytics.Com In article , sdr@unix.portal.com (Stephen Reagan) writes: |> I would like to know if anyone has tried to use the mmap() function |> on OSF - I have heard rumors that it doesn't work (unfortunately, this |> is non-specific information). I have been looking at Stevens' book as |> a guide, but I cannot get enough information to pass any judgement. |> Basically I am interested in maintaining a very large disk file in order |> to take periodic snapshots of a real-time system that would entail saving |> information of up to 60K data points in a file that may be 15Mb or more. |> Is using mmap() in this case a reasonable idea? What if I don't know |> the exact file size at creation time, but can get it when I read the |> data from the file later? Any information, suggestions, or anecdotal |> information would be greatly appreciated. The attached hack works fine for me... #include #include #include #include long WriteFile(int ifd, int ofd) { register caddr_t addr, p; register long nb=0, s=0; struct stat st; long size; long pgsize = sysconf(_SC_PAGE_SIZE); #if UseMMAP #include #include #define MeOnly (PROT_NONE | PROT_WRITE) #define YouDecide (MAP_FILE | MAP_VARIABLE | MAP_PRIVATE) (void)fstat(ifd,&st); nb = st.st_size; size = ((nb / pgsize) + 1) * pgsize; if ((addr = mmap(NULL,size,MeOnly,YouDecide,ifd,0))) { (void)madvise(p,size,MADV_SEQUENTIAL); for (p=addr; p!=&addr[nb]; p+=(s=write(ofd,p,&addr[nb]-p))) { if (s < 0) { perror("loop error"); break; } } (void)munmap(addr,size); } else #endif /* UseMMAP */ { addr = (caddr_t)malloc(pgsize); while ((nb+=(size=read(ifd,p,pgsize))) > 0) { for (p=addr; p!=&addr[size]; p+=(s=write(ofd,p,&addr[size]-p))) { if (s < 0) { perror("loop error"); break; } } } (void)free(addr); } return(nb); } main(int argc, char **argv) { register int ofd = open(argv[1],O_WRONLY|O_CREAT|O_TRUNC,0777); register int i = 1; register int ifd; (void)printf("Outputing to %s (%d)\n",argv[1],ofd); while (++i < argc) { ifd = open(argv[i],O_RDONLY); (void)printf("Inputting from %s (%d)\n",argv[i],ifd); if (ifd >= 0) { WriteFile(ifd,ofd); } (void)close(ifd); } (void)close(ofd); exit(0); } The key thing to remember when using mmap() (at least on DEC OSF/1) is that you can NOT extend the length of files using it. Your particular problem space appears to be well suited for the use of mmap() (IMHO). What I would suggest doing is to write your program such that you use read/write to determine the length of the file and then write to the furthest byte, which will create what you need to be able to then mmap it. - Marc -- /| -----------------------. ~ . ~ X|=| Marc N. Evans |------------------. ~ . X| | Software Consultant | Synergytics |-------------------------------. X| | Marc@Synergytics.Com | 21 Hinds Lane | Specializing in: | X| | (603) 635-3857 | Pelham, NH, USA | - Unix internals/applications | X|=|______________________| 03076-3013 | - X11 internals/applications | X| (____________________| | X| (_________________________________| X| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^