From: "Linh Dang" <linhd@nortel.com>
To: linux-kernel@vger.kernel.org
Subject: mmap (2) vs read (2)
Date: Sat, 17 Sep 2005 12:10:35 -0400 [thread overview]
Message-ID: <wn58xxvhdz8.fsf@linhd-2.ca.nortel.com> (raw)
Hi, how come reading memory from /dev/mem using pread(2) or mmap(2)
will give diffent results?
I run the little prog below 10 times and it always give the following
results
# for i in `seq 1 10`; do ./bar 0x4000000 ; done
04000000: 00000000
pread(04000000): 00cc7d83
04000000: 00000000
pread(04000000): 00cc7d83
04000000: 00000000
pread(04000000): 00cc7d83
04000000: 00000000
pread(04000000): 00cc7d83
04000000: 00000000
pread(04000000): 00cc7d83
04000000: 00000000
pread(04000000): 00cc7d83
04000000: 00000000
pread(04000000): 00cc7d83
04000000: 00000000
pread(04000000): 00cc7d83
04000000: 00000000
pread(04000000): 00cc7d83
04000000: 00000000
pread(04000000): 00cc7d83
thanx
----------------------------------- bar.c ---------------------------------
#include <unistd.h>
#include <stdio.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <errno.h>
int
main(int argc, char** argv)
{
uint32_t *addr, item;
uint32_t offset, val;
int fd, update = 0;
if (argc < 2)
return 1;
offset = strtoul(argv[1], NULL, 0);
if (argc > 2) {
val = strtoul(argv[2], NULL, 0);
update = 1;
}
fd = open("/dev/mem", O_RDWR, 0666);
addr = mmap(NULL, 4096, PROT_READ|PROT_WRITE, 0x8080|MAP_SHARED, fd, offset);
if (addr)
{
printf("%8.8x: %8.8x", offset, *addr);
if (update) {
*addr = val;
msync(addr, 4096, MS_SYNC);
printf("-> %8.8x\n", *addr);
} else
printf("\n");
pread(fd, &item, sizeof(item), offset);
printf("pread(%8.8x): %8.8x\n", offset, item);
munmap(addr, 4096);
}
close(fd);
}
next reply other threads:[~2005-09-17 16:10 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-09-17 16:10 Linh Dang [this message]
2005-09-17 18:26 ` mmap (2) vs read (2) Arjan van de Ven
2005-09-18 16:47 ` Linh Dang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=wn58xxvhdz8.fsf@linhd-2.ca.nortel.com \
--to=linhd@nortel.com \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox