Linux-function-mmap,mmap64,munmap;

Source: Internet
Author: User

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 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 and then the kernel choses the address in which to create the mapping; The most portable method of createing a new

Mapping. If addr is isn't NULL, then the kernel takes it as a hint on where to place mapping; On Linux, the mapping is created at a Nerby

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

The contents of a file mapping. is 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 the 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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.