There are a lot of ways to allocate and manage memory commonly used in C + +, such as smart pointers, STL containers, New/delete, Malloc/free, BRK, SBRK, and so on, and recently studied a kind of memory management method of Unix's lower level mmap/munmap, You need to maintain the allocated virtual memory entirely yourself, without any other auxiliary data structures to help maintain the memory space.First, enter the man
Mmap system call (function)void *mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)Memory mapping function Mmap, is responsible for the file content mapping to the process of virtual memory space, through the memory of the read and modify, to implement the file read and modify, and do not need to call read, write and other operations.Addr: S
Disk-to-memory mapping is a file mapping, before we say the swap, because This problem makes me very easy to think of Swap,linux swap is the meaning of the swap partition, in the case of memory not enough, the operating system first put the memory and the swap area of the disk to "map", and then the these memory freed up into memory, For the subsequent process of freeing up a piece of memory space, wait until the own process is awakened again, then the memory inside the disk in exchange. Ther
Title: Question about mmap? -- Linux general technology-Linux programming and kernel information. The following is a detailed description. At the beginning, I learned about linux programming. I don't know much about some basic issues.
I have read the original form of the mmap function void * mmap (void * addr, size_t length, int prot, int flags, int fd, off_t of
In this example, a character device is used. The device applies for a piece of memory, and file_operations has the MMAP function. In test. C, the MMAP memory can be modified by operating the user memory.
Driver code mmap_demo.c
#include
Makefile
KERNELDIR=/lib/modules/$(shell uname -r)/buildPWD=$(shell pwd)obj-m = mmap_demo.omodules:$(MAKE) -C $(KERNELDIR) M=$(PWD) modulestest: test.cgcc $
Test code
#inc
process is loaded. These system information contains the running space of the current process. When the user program calls Mmap. The function finds the appropriate VMA in the space of the current process to describe the area that it is going to map. The function of this area is to map the contents of the specific file that the file descriptor points to in the Mmap function.The principle is: the execution o
unsigned short *) __io_virt (addr) = (b))#define Writel (B,ADDR) (* (volatile unsigned int *) __io_virt (addr) = (b))#define MEMSET_IO (A,b,c) memset (__io_virt (a), (b), (c))#define MEMCPY_FROMIO (A,b,c) memcpy ((a), __io_virt (b), (c))#define MEMCPY_TOIO (A,b,c) memcpy (__io_virt (a), (b), (c))Finally, we want to emphasize the implementation of the MMAP function in the driver. Mapping a device with mmap
" This article declined to reprint , originally from http://990487026.blog.51cto.com"Linux System Development 5 interprocess Communication Pipe () FIFO () mmap () pipe () Pipeline Communication Introduction pipe () parent-child process communication case pipe () using piping has some restrictions pipe () pipe buffer size pipe () read-side non-blocking pipeline FIFO () Pipeline file FIFO () write end/Read End program pipeline file size on disk is 0mmap
is the process dynamic memory allocation start address (the start address of the heap), and a BRK (the current last address of the heap), which is the current terminating address of the dynamic memory allocation.The basic function of C language dynamic memory allocation is malloc (), and the basic implementation on Linux is called by the kernel's BRK system. BRK () is a very simple system call, simply changing the value of the member variable BRK of the MM_STRUCT structure.The
(inaccessible), the value is often set to Prot_read | Prot_write.Flags are specified by the following constant values: map_shared, Map_private, map_fixed, where map_shared (changes are shared, all processes are visible to the modified shared memory), Map_private (the change is private, Only visible to the process for shared memory modification is required, and map_fixed is not recommended for use.MUNMP () deletes the address mapping relationship, the function prototype is as follows: #inclu
/ * Use Mmap to operate the file * /#include #include #include #include #include #include #define FILELENGTHintMainvoid){intFD =-1;/ * The string that will be written to the file * / Charbuf[]="Quick brown fox jumps over the lazy dog";Char*ptr = NULL;/* Open the file mmap.txt and reduce the file length to 0, create it if the file does not exist, and read/write the permission to execute */FD = open ("Mmap.txt", O_RDWR/* Readable/writable */| O_creat/
void *mmap (void *addr, size_t length, int prot, int flags, int fd, off_t offset);function function: Map the contents of the file to a piece of memory (virtual memory), through the memory read and modify, to achieve the file read and modify.Parameter descriptionAddr: Specifies the starting address of the map, usually set to null (indicated by the system).Length: Represents how much part of the file is mapped to memory.PORT: How the Map area is protect
mmap,munmap--#include void*mmap(void*addr, size_tlength,intProtintFlagsintFD, off_t offset);intMunmap (void*addr, size_tlength);intMprotect (void*addr, size_t Len,intProt);intMsync (void*addr, size_tlength,intFlags); mmap, Munmap-Map orUnmap filesorDevices into memory.Storage mapping IO maps a disk file to a buffer in storage space. Then when the data is taken fr
#作用: Create a memory-mapped file instead of reading the content directlyText message content: as follows (name is Text.txt)Lorem ipsum dolor sit amet, consectetuer adipiscing elit. DonecEgestas, enim et consectetuer ullamcorper, lectus ligula rutrum Leo,A elementum elit tortor EU quam.Duis tincidunt nisi ut ante. NullaFacilisi.Sed tristique eros eu libero. Pellentesque velArcu. Vivamus purus Orci, iaculis ac, suscipit sit amet, pulvinar EU,Lacus.Praesent placerat tortor sed nisl. Nunc blandit di
Note: The sys/Mman. h header file does not have to be passed in Linux in windows.
Method Introduction:
MMAP maps a file or other objects to the memory. The file is mapped to multiple pages. If the file size is not the sum of the sizes of all pages, the unused space of the last page will be cleared.
Void * MMAP (void * Start, size_t length, int Prot, int flags, int FD, off_t offset); the parameters are
1. Write filesWith open ('?? ' ' r+b ' As F: With contextlib.closing (Mmap.mmap (F.fileno (), size, flags=mmap. map_shared, access=mmap. Access_write)) as mm: = ?? Mm.flush ()2. Key Reminders>>> mmap.ACCESS_READ1>>> mmap.ACCESS_WRITE2>>> mmap.ACCESS_COPY3>>> mmap.ACCESS_READ | mmap.ACCESS_WRITE3In and words, is the access=mmap.ACCESS_READ|mmap.ACCESS_WRITE same as access=mmap.ACCESS_COPY . What do
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" border:0px; "/>importmmapmmap_file=none# #从内存中读取信息, Defread_mmap_info (): globalmmap_filemmap_file.seek (0) ## Convert the binary to String info_str=mmap_file.read (). Translate (none,b ' \x00 '). Decode () print (INFO_STR) # #如果内存中没有对应信息, writes the information to memory for the next call to use Defget_mmap_info (): globalmmap_file# #第二个参数1024是设定的内存大小, units: bytes. If you have more content, you can turn it up a little bit mmap_f
Article Title: Linux mmap File Memory ing mechanism. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
When describing the concept of file ing, it is inevitable that virtual memory (SVR 4 VM) is involved ). in fact, file ing is the central concept of virtual storage. On the one hand, file ing provides users with a se
The second volume of UNIX Network Programming inter-process communication describes MMAP functions. This function has three main purposes:1. Map a common file to the memory, which is usually used when files need to be read and written frequently. In this way, I/O reading and writing is replaced with memory reading and writing to achieve high performance;2. Anonymous memory ing of special files can provide shared memory space for associated processes;3
I. Overview
Memory mapping, in short, is to map a section of memory area of user space to the kernel space, after the successful mapping, user changes to this area of memory can be directly reflected to the kernel space, the same, the kernel space for this section of the changes also directly reflect the user space. The efficiency is very high for both kernel space
The following is a schematic diagram of an area of memory that maps universal files to user space.
Figure I:
Second, the basi
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.