Linux-Function-mmap,mmap64,munmap;

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;

}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。