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=$(
MMAP () and munmap ()-Unix system functions are closer to the underlying
void* mmap(void* addr,size_t size,int prot,int flags, int fd, off_t offset)
The ADDR parameter can specify the first address of the ing. Generally, the value 0 is assigned to the kernel.
Size is the size of the allocated memory, in the unit of page for ing.
Prot is the permission to allocate memory. It is gener
.
#include
This reads the contents of the file through read (), ToUpper (), and invokes write () writeback file. Because the file is too small to reflect the disadvantage of Read ()/write (): Frequent access to large files requires multiple Lseek () to determine the location. Each time you edit the read ()/write (), double data in physical memory. Of course, it is not possible to ignore the cost of creating and maintaining mmap () data structures.
Linux system programming: inter-process communication-mmap, linux-mmap
Inter-process communication-mmap
#include
In essence, mmap synchronizes the memory with files on the hard disk. The content in a memory is synchronized to the hard disk file, that is, the file is mapped to the memory. Therefore, communication be
starting address of the process space and is typically assigned a null pointer, at which point the task of selecting the starting address is left to the kernel to complete. The return value of the function is the address that the final file maps to the process space, and the process can directly manipulate the starting address as a valid address for that value. The parameters of mmap () are no longer detailed here, and readers can refer to the
specified. At this time, the task of selecting the starting address is left to the kernel for completion. The Return Value of the function is the address mapped from the last file to the process space. The starting address of a process operation can be the valid address of the value. Here, we will not detail MMAP () parameters. You can refer to the MMAP () manual page for further information.
(1) Use the
call is done by calling the sync function of the ing file.
BRK (void * end_data_segement ):
Extend the data segment of a processEnd_data_segement specifies the address. This system call is similar to the MMAP implementation method. It also generates a VMA and then specifies its attributes.However, you need to check the validity of the address before this operation, for example, whether the address is great
| Prot_write, map_shared, FD, 0); There are many features and areas to be aware of in the way that shared memory is communicated through MMAP (), which we will specify in the example.(2) Use special files to provide anonymous memory mapping: Applies to relationships between processes; Because of the special affinity of a parent-child process, call Mmap () first i
write operation are reflected in the original file after a certain amount of time.
For 5000~8191 bytes, the process can read and write without error. However, the content is 0 before writing, and in addition, it is not reflected in the file after it is written.
For 8192~14999 bytes, the process cannot read or write to it and will report a sigbus error.
For bytes other than 15000, the process cannot read and write to it, and a SIGSEGV error is raised.
Scenario Three: A file init
Memory mapping is one of the most interesting features of modern Unix systems. As for drivers, memory mappings can be implemented to provide direct access to device memory by user programs.
A clear example of a mmap usage can be seen from a subset of the virtual memory areas that are viewed to the X Windows system server:
Cat/proc/731/maps
000a0000-000c0000 rwxs 000a0000 03:01 282652/dev/mem 000f0000-00100
, the page Disconnection will be majflt; otherwise, it will be minflt.
Principle of Memory Allocation
From the operating system perspective, there are two ways to allocate memory for processes: brk and mmap ).
1. brk pushes the highest address pointer _ edata of the data Segment (. data) to the high address;
2. mmap finds an idle virtual memory in the virtual address space of the process (in the middle of t
principles are described separately:
1. The basic principle of the ordinary document is: each file (mmap) defines the corresponding file operation data structure (file_operations), which defines the mmap operation, such as ext3 file system files corresponding to the file operation: Ext3_file_ Operations, the corresponding MMAP operation interface is: Generic_fil
, you need to specify the Map_anon in the flags parameter to indicate that anonymous mappings are in progress.
Offset: The starting point for the content of the mapped object. Typically set to 0, which represents the start of mapping from a file header.
The return value of the function is the address that the last file maps to the process space, and the process can manipulate the starting address directly to a valid address for that value. The system calls
memory in two ways:(1) Memory mapping provided with normal files: applies to any process; At this point, you need to open or create a file and then call Mmap (); The typical calling code is as follows:Fd=open (name, flag, mode); if (fdPtr=mmap (NULL, Len, prot_read| Prot_write, map_shared, FD, 0); There are many features and areas of communication that enable shared memory through
1. Introduction to shared memory
The shared memory zone is the fastest form of IPC. data transmission between these processes no longer involves the kernel. In other words, processes no longer transmit data by executing system calls that enter the kernel.
That is, each process address space has a shared storage ing area. When this area is mapped to the same real physical address space, data exchange can be performed through this area, for example, th
) TV. TV _usec;Printf ("using/" Read () and write ()/"to copyCosts % d microseconds/N ", time_end-time_start );}
The code is not very difficult.Some linuxc functions are used between them. If you do not understand them, you can view relevant information on your own. I want to use either of the following methods:The implementation of different copies compares the time spent andSome of your own ideas, buffer_size can be used to debug the programYou can change a number to indicate the number of cha
You can't simply say which method is highly efficient. Either method is nothing more than the OS that helps you cache your memory, and then the fl at some point in time.
To the memory. MMAP can easily simplify file operations and is more efficient than Directly Reading and Writing files.
Introduced a few paragraphs: "This problem is complicated and it cannot be simply said that MMAP is faster tha
, len, prot, flags, pgoff);up_write(¤t->mm->mmap_sem);if (file)fput(file);out:return retval;}
This function is used to obtain available virtual address space (vm_area_struct * VMA) from the current process and obtain VMA in mmap_region, then, call file-> f_op-> MMAP (file, VMA) and call the MMAP-supported driver for processing.
The following uses the binder driver as an
MMAP parameters. You can refer to the MMAP manual page or section 12.2 of UNIX netword programming for further information. Finally, let's end this section with an example. As mentioned in section 4.2, the fileinformation array is written in binary form into a file named inforindex. The code is similar to this when you want to access the fileinformation array:
S
Mmap Implementation Analysis
Mmap Implementation Analysis
This article does not introduce how to use mmap functions, but analyzes its kernel implementation. There is a lot of information available on the Internet. The essence of Mmap is to assign (or find) a suitable vma for the current process, and then set the cor
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.