* mmap problems
@ 1999-07-01 4:00 Frederick Ros
1999-07-02 4:00 ` Joe Mario
0 siblings, 1 reply; 2+ messages in thread
From: Frederick Ros @ 1999-07-01 4:00 UTC (permalink / raw)
Hi,
Im\b'm trying to map a file in memory using mmap. Currently I'm using
Digital
Unix 4.0 ( u\b'uname -a' says : OSF1 tobago.vbe.dec.com V4.0 878 alpha)
with the
DEC C Compiler DEC C V5.6-075 on Digital UNIX V4.0 (Rev. 878)
I've the following program:
/*=================================================================*/
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <errno.h>
int main(void)
{
int fd, pagesize,i;
char *data;
fd = open("/tmp/mapdemofile",O_RDWR| O_CREAT, 0666);
data = mmap(NULL, 10, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if ( data == (caddr_t)-1)
{
fprintf(stderr,"%i - %s\n",errno,strerror(errno));
exit(1);
}
for (i =0 ; i< 10; i++)
data[i] = '0';
}
/*======================================================*/
When I compile this example ( cc -o shared -g shared.c), and run it, I
get a:
Segmentation fault (core dumped)
If I replace the filename with mapdemofile instead of /tmp/mapdemofile,
evrything works fine.
In fact, if I try to map /tmp/mapdemofile, the mmap call returns an
adress ( no
error), but I can't neither read nor write to this address.
If the file is in my personnal directory. there's no problem.
In fact my HOME is on another computer, and is link via NFS on my
computer
(tobago).
Does this problem come from the fact that my personnal account is on an
NFS
partition, and that I'm trying to mmap on another partition ??
If I create a new directory on the same partition as /tmp , the problem
still
occurs...
Is this normal, and if so why isn't it specified in the mmap man page ??
Best Regards.
Frederick Ros.
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: mmap problems
1999-07-01 4:00 mmap problems Frederick Ros
@ 1999-07-02 4:00 ` Joe Mario
0 siblings, 0 replies; 2+ messages in thread
From: Joe Mario @ 1999-07-02 4:00 UTC (permalink / raw)
Frederick:
The problem is you are mmap'ing beyond the end of the file.
The file you've opened is zero-length. The mmap() call didn't
return an error because there is some standard which requires Tru64
to not issue an error when mmap'ing beyond the end of a file.
The solution is to grow the file before you try accessing it.
One easy way is to lseek to some desired offset and then write a byte.
For example, add something like this to your program:
long fill = -1;
ret = lseek(fd,20,SEEK_SET);
if (ret < 0) {
perror("lseek error");
exit(1);
}
ret = write(fd,&fill,4);
if (ret < 0) {
perror("write error");
exit(1);
}
Joe Mario
Frederick Ros wrote:
> Im\b'm trying to map a file in memory using mmap.
> ...
> I've the following program:
>
> /*=================================================================*/
> #include <unistd.h>
> #include <fcntl.h>
> #include <sys/stat.h>
> #include <stdio.h>
> #include <sys/types.h>
> #include <sys/mman.h>
> #include <errno.h>
>
> int main(void)
> {
> int fd, pagesize,i;
> char *data;
>
> fd = open("/tmp/mapdemofile",O_RDWR| O_CREAT, 0666);
> data = mmap(NULL, 10, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
> if ( data == (caddr_t)-1)
> {
> fprintf(stderr,"%i - %s\n",errno,strerror(errno));
> exit(1);
> }
> for (i =0 ; i< 10; i++)
> data[i] = '0';
> }
> /*======================================================*/
>
> When I compile this example ( cc -o shared -g shared.c), and run it, I
> get a:
>
> Segmentation fault (core dumped)
> ...
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~1999-07-02 4:00 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1999-07-01 4:00 mmap problems Frederick Ros
1999-07-02 4:00 ` Joe Mario
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox