From 1efe6a371ebbd136c7272ddb66797728bdd80725 Mon Sep 17 00:00:00 2001 From: gatieme Date: Sun, 5 Jun 2016 13:22:21 +0800 Subject: [PATCH] mmap --- study/kernel/memory/mmap/mmap_test.c | 8 +-- study/kernel/memory/mmap/test.c | 102 +++++++++++++++------------ 2 files changed, 60 insertions(+), 50 deletions(-) diff --git a/study/kernel/memory/mmap/mmap_test.c b/study/kernel/memory/mmap/mmap_test.c index 254f907..c873dfd 100644 --- a/study/kernel/memory/mmap/mmap_test.c +++ b/study/kernel/memory/mmap/mmap_test.c @@ -42,10 +42,7 @@ static int my_map(struct file *filp, struct vm_area_struct *vma) unsigned char i; unsigned long start = (unsigned long)vma->vm_start; - - - //unsigned long end = (unsigned long)vma->vm_end; - + unsigned long end = (unsigned long)vma->vm_end; unsigned long size = (unsigned long)(vma->vm_end - vma->vm_start); @@ -60,7 +57,6 @@ static int my_map(struct file *filp, struct vm_area_struct *vma) //往该内存写10字节数据 - for(i=0;i<10;i++) { buffer[i] = array[i]; @@ -102,6 +98,8 @@ static ssize_t hwrng_attr_current_show(struct device *dev, static DEVICE_ATTR(rng_current, S_IRUGO | S_IWUSR, hwrng_attr_current_show, NULL); + + static int __init dev_init(void) { int ret; diff --git a/study/kernel/memory/mmap/test.c b/study/kernel/memory/mmap/test.c index 3918d38..62a5bf3 100644 --- a/study/kernel/memory/mmap/test.c +++ b/study/kernel/memory/mmap/test.c @@ -1,45 +1,57 @@ -#include -#include -#include -#include -#include -#include -#include -#include - -#define PAGE_SIZE 4096 - - -int main(int argc , char *argv[]) -{ - int fd; - int i; - unsigned char *p_map; - - //打开设备 - fd = open("/dev/mymap",O_RDWR); - if(fd < 0) - { - printf("open fail\n"); - exit(1); - } - - //内存映射 - p_map = (unsigned char *)mmap(0, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED,fd, 0); - if(p_map == MAP_FAILED) - { - printf("mmap fail\n"); - goto here; - } - - //打印映射后的内存中的前10个字节内容 - for(i=0;i<10;i++){ - printf("%d\n",p_map[i]); - p_map[i] = i + 10; - } - - -here: - munmap(p_map, PAGE_SIZE); - return 0; -} \ No newline at end of file +#include +#include +#include +#include +#include +#include +#include +#include + +#define PAGE_SIZE 4096 + + +int main(int argc , char *argv[]) +{ + + int fd; + int i; + unsigned char *p_map; + + + //打开设备 + fd = open("/dev/mymap",O_RDWR); + + if(fd < 0) + { + printf("open fail\n"); + exit(1); + } + + + + //内存映射 + p_map = (unsigned char *)mmap(0, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED,fd, 0); + if(p_map == MAP_FAILED) + { + + printf("mmap fail\n"); + goto here; + } + + + + //打印映射后的内存中的前10个字节内容 + for(i=0;i<10;i++) + { + printf("%d\n",p_map[i]); + p_map[i] = i + 10; + } + + + + +here: + + munmap(p_map, PAGE_SIZE); + return 0; +}