#include #include #include #include #include #include int main() { int page_size = getpagesize(); #define SIZE 512 int rwfd; int res, res1, res2; char *buf; rwfd = open("rwfile", O_RDWR); assert(rwfd != -1); res = ftruncate(rwfd, SIZE); assert(res == 0); /* Map rwfile with PROT_WRITE */ buf = mmap(0, page_size, PROT_WRITE, MAP_SHARED, rwfd, 0); assert(buf != (char *)-1); /* Try to make the kernel read from buf */ res1 = write(rwfd, buf, SIZE); printf("write(rwfd, buf, SIZE), res1 = %d, %s\n", res1, res1 < 0 ? strerror(-res1) : "OK"); /* Try to make the kernel write into buf */ res = read(rwfd, buf, SIZE); printf("read(rwfd, buf, SIZE), res = %d, %s\n", res, res < 0 ? strerror(-res) : "OK"); /* Try to make kernel read from buf again */ res2 = write(rwfd, buf, SIZE); printf("write(rwfd, buf, SIZE), res2 = %d, %s\n", res2, res2 < 0 ? strerror(-res2) : "OK"); printf("result: res1 %s res2\n", res1 == res2 ? "==" : "!="); }