Today Mayuyu encounters two more interesting functions, namely the mmap () and the fmemeopen () functions.
First look at the mmap () function, the header file for this function is #include <unistd.h> and #include <sys/mman.h>. function prototypes are as follows
Return value: If the mapping succeeds, it returns the memory start address of the mapping area, otherwise map_failed (-1) is returned, and the cause of the error is stored in errno.
Error code:
- The EBADF parameter FD is not a valid file descriptor.
- Eacces access rights are incorrect. If the file must be readable in the case of map_private, use map_shared to have prot_write and the file to be writable.
- EINVAL parameter start, length, or offset has an illegal.
- The Eagain file is locked, or too much memory is locked.
- ENOMEM Insufficient memory.
mmap () is used to map a file's contents into memory, and the value of that memory area is directly read-write to the contents of the file. A page table entry is established in memory, but the mmap () call does not immediately establish a page table entry in the page table, and the page is allocated space only when it is used to an address space. Due to the page replacement, it is necessary to have a certain amount of physical memory to support, if the memory is too small, then the newly displaced into the memory of the page has been displaced to disk,mmap performance greatly reduced.
The existence of mmap () is mainly a convenient operation method for the user program to access large files randomly. It also provides efficient means for sharing large volumes of data across processes. It provides an effective method for the processing of jumbo files.
Example:
#include <sys/mman.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h># Include <fcntl.h> #include <stdio.h>int main (int argc, char **argv) {struct stat st;//open file int fd = -1;if (fd = O Pen ("file", o_rdonly)) = =-1) {printf ("Open File error!\n"); return-1;} Remove file status According to file descriptor if (Fstat (fd, &st) = =-1) {printf ("Get St error!\n"); return-1;} mapping void *start = null;if (start = mmap (Start, st.st_size, prot_read| Prot_write, Map_private, fd, 0) = = map_failed) {printf ("mmap error!\n"); return-1;} printf ("%s\n", (char *) start); De-Map Relationship if (Munmap (start, st.st_size) = =-1) {printf ("Munmap error!\n"); return-1;} Close (FD); return 0;}
The above has a function of Fstat (FD, ST), which successfully returns 0, and the failure returns -1. It is based on the file descriptor FD Gets the file status St , and St is a struct, records a lot of file state information, the structure is defined as follows
In fact, there is a function similar to fstat () , that is, stat (). The difference is that the first parameter of stat () represents a file name, not a document descriptor.
mmap () system call is not entirely designed for shared memory, it provides a different way of accessing ordinary files, and the process can manipulate ordinary files like memory, of course mmap () is one of the most important applications of shared memory.
There are also a few functions related to the mmap () system call:
(1) Munmap () function
The function prototypes are:
The call unlocks a mapping relationship in the process address space,addr is the address returned by the call to mmap () ,len is the size of the map area, when the
, access to the original mapped address will result in a segment error. This function unlocks a successful return of 0, otherwise returns -1.
(2) Msync () function
The function prototypes are:
In general, the changes to the shared content of the process in the mapping space are not directly written back to the disk file, often after the call to Munmap ()
The operation is OK. You can implement the content of the file on disk by calling Msync () to match the contents of the shared memory area.
Addr: file maps to the address of the process space
len: size of the mapping space
Flags: refreshed parameter settings, can be value ms_async/ms_sync/ms_invalidate
which
When the value is Ms_async (asynchronous), the call returns immediately, not equal to the completion of the update;
When the value is Ms_sync (synchronous), the call waits for the update to return after completion;
Ms_invalidate (notifies the process using the shared area that the data has changed), after the shared content has changed, making the file
His mapping fails, allowing other processes that share the file to regain the latest value
This function returns 0if successful, and 1if it fails.
(3) Madvise () function
When calling mmap () , the kernel simply establishes a mapping table for the logical address to the physical address, and does not map any data to memory. In the number of you want to access
According to the time the kernel will check whether the data is in memory paging, if not, then issue a page break,Linux default paging is 4K, you can imagine
Read how many times a movie file of nearly 2G is going to be interrupted. The solution is as follows
Use madvise () and mmap () together to tell the kernel this piece of data before using the data I want to use, read it into memory at once, avoid
Interrupt. madvise () This function can suggest the use of mapped memory, which improves memory and greatly improves performance.
Function Prototypes:
Parameter description: addr The first address of the mapped address space obtained for the mmap () call.
Len is the size of the mapping space.
Behav about this parameter, please click here for details.
Execution successfully returns 0, otherwise returns -1. Use the following method
Code:
<strong ><span style= "FONT-SIZE:18PX;" > #include <sys/mman.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h># Include <fcntl.h> #include <stdio.h>int main (int argc, char **argv) {struct stat st;//open file int fd = -1;if (fd = O Pen ("file", o_rdonly)) = =-1) {printf ("Open File error!\n"); return-1;} Remove file status According to file descriptor if (Fstat (fd, &st) = =-1) {printf ("Get St error!\n"); return-1;} mapping void *start = null;if (start = mmap (Start, st.st_size, prot_read| Prot_write, Map_private, fd, 0) = = map_failed) {printf ("mmap error!\n"); return-1;} Use advise () to invoke if (Madvise (Start, st.st_size, Madv_willneed | madv_sequential) = =-1) {printf ("madvise error!\n"); return-1;} printf ("%s\n", (char *) start); De-Map Relationship if (Munmap (start, st.st_size) = =-1) {printf ("Munmap error!\n"); return-1;} Close (FD); return 0;} </span></strong>
About mmap () call Mayuyu has been described a lot, and then another system call, the Fmemopen (), is introduced.
The mmap () call is to map the file to memory, which means that the file can be used as memory through the mmap () call. The Fmemopen () call, in contrast, can be used by a fmemopen () call to use the memory as a file. The Fmemopen () call will be described in more detail next.
Header files: #include <stdio.h>
Function Prototypes:
parameter Description: This is more obvious, do not say, followed by examples to explain.
This is a lot of applications, such as some files do not support memory operations, but support file operations.
Code:
#include <string.h> #include <stdio.h>static char buff[] = "Mayuyu is from Japan"; int main (int argc, char **arg V) { int len = strlen (buff); FILE *fd = Fmemopen (buff, Len, "R"), if (fd = = NULL) {printf ("Get File error!\n"); return-1;} Char ch;while ((ch = fgetc (FD))! = EOF) printf ("%c", ch);p UTS (""); fclose (FD); return 0;}
Use of mmap () and Fmemopen ()