Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 24 Dec 1999 00:18:53 -0500 (EST)
From:      Mikhail Teterin <mi@kot.ne.mediaone.net>
To:        questions@freebsd.org
Subject:   questions about mmap()
Message-ID:  <199912240518.AAA18438@rtfm.newton>

next in thread | raw e-mail | index | archive | help
Hello!

The included  simple program,  tries to mmap  stdin until  possible, and
tries  to reference  the  beginning  of the  chunk.  In  all honesty,  I
expected mmap() to fail, if the offset is beyond the file's size. But it
does not fail.  Instead, the attempt to reference the  beginning of that
extra chunk results in "Bus error":

	mi@rtfm:~/bz (1214) ./m < /kernel
	0x180e5000, 2039808Bus error (core dumped)

How can I find  out when the last block of the file  was read (ok, I can
be catching the  SIGBUS -- yuck), and  where is the file's  last byte in
it? That is, I'm trying to avoid using fstat(2) :-) Thanks!

	-mi

#include <sys/types.h>
#include <sys/mman.h>
#include <unistd.h>
#include <stdio.h>

main() {
	char *p;
	off_t offset;

	for(offset = 0;
		(p = mmap(NULL, 512, PROT_READ, 0, STDIN_FILENO, offset))
			!= MAP_FAILED;
		offset+=512) {
			fprintf(stderr, "%\r%p, %d", p, offset);
			fprintf(stderr, ", %d", *p);
			munmap(p, 512);
	}
	perror("\nmmap");
}


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199912240518.AAA18438>