分类: LINUX
2008-08-20 18:28:12
#include#include#include#includeMODULE_LICENSE("GPL");MODULE_AUTHOR("Wheelz");MODULE_DESCRIPTION("mmapdemo");staticunsignedlongp=0;staticint__initinit(void){//分配共享内存(一个页面)p=__get_free_pages(GFP_KERNEL,0);SetPageReserved(virt_to_page(p));printk("<1>p=0xx\n",p);//在共享内存中写上一个字符串strcpy(p,"Helloworld!\n");return0;}staticvoid__exitfini(void){ClearPageReserved(virt_to_page(p));free_pages(p,0);}module_init(init);module_exit(fini);用户态程序---------------------------------#include#include#include#include#include#definePAGE_SIZE(4*1024)#definePAGE_OFFSET 0xc0000000#defineKERNEL_VIRT_ADDR 0xc5e3c000intmain(){char*buf;intfd;unsignedlongphy_addr;fd=open("/dev/mem",O_RDWR);if(fd==-1)perror("open");phy_addr=KERNEL_VIRT_ADDR-PAGE_OFFSET;buf=mmap(0,PAGE_SIZE,PROT_READ|PROT_WRITE,MAP_SHARED,fd,phy_addr);if(buf==MAP_FAILED)perror("mmap");puts(buf);//打印共享内存的内容munmap(buf,PAGE_SIZE);close(fd);return0;}