mirror of
https://github.com/gatieme/LDD-LinuxDeviceDrivers.git
synced 2026-09-24 05:53:54 +08:00
mmap
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -1,45 +1,57 @@
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <linux/fb.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <linux/fb.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user