码迷,mamicode.com
首页 > 系统相关 > 详细

Linux-Function-mmap,mmap64,munmap;

时间:2014-12-11 01:32:59      阅读:351      评论:0      收藏:0      [点我收藏+]

标签:des   io   ar   os   sp   for   on   art   bs   

mmap,mmap64,munmap---map or unmap files or devices into memory

void *mmap(void* addr,size_t length, int prot, int flags, int fd, off_t offset);

void *mmap64(void* addr,size_t length, int prot, int flags, int fd, off_t offset);

int munmap(void *addr, size_t lenght);

  mmap function creates a new mapping in the virtual address space of the calling process. The sharting address for the new mapping is 

specipfied in addr. the lenght argument specifies the lenght of the mapping.

  If addr is NULL, then the kernel choses the address at which to create the mapping; this is the most portable method of createing a new

mapping.If addr is not NULL, then the kernel takes it as a hint about where to place mapping; on linux, the mapping will be created at a nerby

page boundary. The address of the new mapping is returned as the result of the call

  The contents of a file mapping. are initialized using lenght bytes starting at offset int the file referred to by the file descriptor fd. offset must

be a mutiple of the page size as returned by sysconf(SC_PAGE_SIZE).

  The prot argument describes the desired memory protection of the mapping.It is either PROT_NONE or the bitwise of one ro more of the following flags

Simple

int main()

{

  int fd = open("a.txt",O_CREAT|O_RDWR,0666);

  if(!fd)

    perror("open"),exit(-1);

  ftruncate(fd,sizoef(char)*6);

  void *p = mmap(0,sizeof(char*)*6,PROT_WRITE,MAP_SHARED,fd,0);

  char* pc = p;

  strncpy(pc,"Masato",6);

  munmap(p,sizeof(char)*6);

  close(fd);

  FILE* pf = fopen("a.txt","r");

  char buf[10] ={0};

  fgets(buf,sizeof(buf),pf);

  fwrite(buf,strlen(buf),1,stdout);

  fclose(pf);

  return 0;

}

Linux-Function-mmap,mmap64,munmap;

标签:des   io   ar   os   sp   for   on   art   bs   

原文地址:http://www.cnblogs.com/Masato/p/4156652.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!