/* * proj1_tester.c * Description: test code for project 1 * Author: Sung-hun Kim (shkim@csl.skku.edu) * Date: 2016.03.28 */ #include #include #include #include #include #include #define MAPPED_SIZE 4096 #define OFFSET 0 int main(void) { int fdmem; int i; int *map = NULL, *map2 = NULL; const char dev_path[] = ""; //your device path /* open device file and check error */ fdmem = open( fake_dev, O_RDWR | O_SYNC ); if (fdmem < 0){ printf("Failed to open the %s!\n", dev_path); return 0; } else printf("open %s successfully !\n", dev_path); /* mmap() the opened device file */ map = (int *)(mmap(0, MAPPED_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fdmem, OFFSET)); map2 = (int *)(mmap(0, MAPPED_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fdmem, OFFSET)); /* use map pointer to access the mapped area! */ printf("map1\n"); for (i = 0; i < 10; i++) printf("content: 0x%x\n",*(map+i)); printf("map2\n"); for (i = 0; i < 10; i++) printf("content: 0x%x\n",*(map2+i)); /* access to mmaped region for modifying it */ printf("modify contents\n"); for (i = 0; i < 10; i++) *(map+i) = i; printf("map1\n"); for (i = 0; i < 10; i++) printf("content: 0x%x\n",*(map+i)); printf("map2\n"); for (i = 0; i < 10; i++) printf("content: 0x%x\n",*(map2+i)); /* unmap the area & error checking */ if (munmap(map, MAPPED_SIZE) == -1) printf("error occurred while un-mmapping map1\n"); if (munmap(map2, MAPPED_SIZE) == -1) printf("error occurred while un-mmapping map2\n"); /* close device file */ close(fdmem); return 0; }