From: Joe Mario <mario@zk3.dec.com>
Subject: Re: mmap problems
Date: 1999/07/02 [thread overview]
Message-ID: <377D060A.3859A6C2@zk3.dec.com> (raw)
In-Reply-To: 377B0D0B.FD37DD2@compaq.com
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)
> ...
prev parent reply other threads:[~1999-07-02 4:00 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
1999-07-01 4:00 mmap problems Frederick Ros
1999-07-02 4:00 ` Joe Mario [this message]
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