From: marc@synergy.Synergytics.Com (Marc Evans)
Subject: Re: using mmap() in OSF
Date: Tue, 16 Nov 1993 12:02:40 GMT [thread overview]
Message-ID: <CGL2sH.BH6@mv.mv.com> (raw)
In-Reply-To: CGK1xr.7wz@unix.portal.com
In article <CGK1xr.7wz@unix.portal.com>, 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 <unistd.h>
#include <fcntl.h>
#include <memory.h>
#include <sys/types.h>
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 <sys/stat.h>
#include <sys/mman.h>
#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|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
next prev parent reply other threads:[~1993-11-16 12:02 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
1993-11-15 22:46 using mmap() in OSF Stephen Reagan
1993-11-16 12:02 ` Marc Evans [this message]
1993-11-16 14:38 ` ivie
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox