From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fee84,eeac0fcac5e55248 X-Google-Attributes: gidfee84,public From: Joe Mario Subject: Re: mmap problems Date: 1999/07/02 Message-ID: <377D060A.3859A6C2@zk3.dec.com>#1/1 X-Deja-AN: 496515273 Content-Transfer-Encoding: 7bit References: <377B0D0B.FD37DD2@compaq.com> Organization: UBPG Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 Newsgroups: comp.unix.osf.osf1 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'm trying to map a file in memory using mmap. > ... > I've the following program: > > /*=================================================================*/ > #include > #include > #include > #include > #include > #include > #include > > 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) > ...