Linux programming memory ing and linux programming ing

Source: Internet
Author: User

Linux programming memory ing and linux programming ing

I. Overview

Memory ing is used to create a new memory ing in the virtual address space of the calling process.

Memory ing can be divided into two types:

1. File ing: maps all or part of a common file to the virtual memory of the process. After ing, the process can directly operate the file content in the corresponding memory area!

2. Anonymous ing: when there is no corresponding file for the anonymous ing or the corresponding file is a virtual file (for example,/dev/zero), after the ing, all the memory pages will be initialized to 0.

When multiple processes map to the same memory area, they share the same page of physical memory. The child process created through fork () also inherits the ing copy of the parent process !!!

If multiple processes operate in the same memory region, different actions are performed based on the ing feature. Ing features can be divided into private ing and shared ing:

1. Private ing: The ing content is invisible to other processes. For file ing, the content of a file changed by a process in the ing memory is not reflected in the mapped underlying file. The kernel uses the copy-on-write technology to solve this problem: As long as a process modifies the content in the page, the kernel creates a new page for the process and copies the content to be modified to the new page.

2. Shared ing: All operations performed by a process on the shared memory area are visible to other processes !!! For file ing, the Operation content is reflected in the underlying file.

Note: After the process executes the exec () call, the previous memory ing will be lost, and the child process created by fork () will inherit the features mapped by the parent process (private and shared) it will also be inherited.

Exception signal:

1. When the ing Memory attribute is set to read-only, if the write operation is performed, the SIGSEGV signal is generated.

2. When the number of bytes in the ing memory is greater than the size of the mapped file and exceeds the size of the current Memory Page of the file. If the accessed area exceeds the page size of the file, a SIGBUS signal is generated.

A bit of a detour. For example, the memory segment of the hypothetical internal maintenance is 4k(generally 4k,4096, and the size of a common file a.txt is 10 bytes. If you create a ing memory of 4097 bytes and map it to the file. Bytes, because the.txt size can be fully mapped using a single page, 10 bytes is much smaller than the 4096 bytes of a page, so the kernel will only give it a page. The memory address starts from 0, and the content in the 0-9 area matches the data in the.txt file. We can also access the 10-range. However, if the page size exceeds the limit of 4096, a sigbus signal is generated !!!

We will use a simple example to demonstrate the two exceptions.

Ii. Function Interfaces

1. Create a ing

1 #include <sys/mman.h>2 3 void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);

Addr: the virtual memory address to be stored after the ing. If it is NULL, the kernel will automatically help you choose.

Length: the number of bytes mapped to the memory.

Prot: Permission protection: PROT_NONE (inaccessible), PROT_READ (readable), PROT_WRITE (writable), PROT_EXEC (executable ).

Flags: ing features: MAP_PRIVATE (private), MAP_SHARED (shared), and MAP_ANONYMOUS. There are also some other man manuals that can be queried.

Fd: file descriptor to be mapped.

Offset: the offset of the file. If it is 0 and the length is the file length, the entire file is mapped.

2. unmap

1 #include <sys/mman.h>2 3 int munmap(void *addr, size_t length);

Addr: the starting address of the memory to be removed. If addr is not in the starting position of the ing area, the memory area may be divided into two parts after a portion is removed !!!

Length: the number of bytes to be removed.

3. Synchronization ing area

1 #include <sys/mman.h>2 3 int msync(void *addr, size_t length, int flags);

Addr: the start address of the memory to be synchronized.

Length: the length of the bytes to be synchronized.

Flags: MS_SYNC (execute synchronization file writing). In this operation, the kernel will directly write the content to the disk. MS_ASYNC (execute asynchronous file writing). In this operation, the kernel first writes the content to the kernel buffer and then writes it to the disk at an appropriate time.

Iii. File ing instance

1/** 2 * @ file mmap_file.c 3 */4 5 # include <stdio. h> 6 # include <stdlib. h> 7 # include <string. h> 8 # include <fcntl. h> 9 # include <signal. h> 10 # include <unistd. h> 11 # include <sys/mman. h> 12 13 # define MMAP_FILE_NAME "a.txt" 14 # define MMAP_FILE_SIZE 1015 16 void err_exit (const char * err_msg) 17 {18 printf ("error: % s \ n ", err_msg); 19 exit (1); 20} 21 22/* signal processor */23 void signal_handler (int signum) 24 {25 If (signum = SIGSEGV) 26 printf ("\ nSIGSEGV handler !!! \ N "); 27 else if (signum = SIGBUS) 28 printf (" \ nSIGBUS handler !!! \ N "); 29 exit (1); 30} 31 32 int main (int argc, const char * argv []) 33 {34 if (argc <2) 35 {36 printf ("usage: % s text \ n", argv [0]); 37 exit (1); 38} 39 40 char * addr; 41 int file_fd, text_len; 42 long int sys_pagesize; 43 44/* set the signal processor */45 if (SIGSEGV, signal_handler) = SIG_ERR) 46 err_exit ("signal ()"); 47 if (signal (SIGBUS, signal_handler) = SIG_ERR) 48 err_exit ("signal ()"); 49 50 if (file_fd = open (MMAP_FILE_NAME, O_RDWR )) =-1) 51 err_exit ("open ()"); 52 53/* system page size */54 sys_pagesize = sysconf (_ SC _PAGESIZE); 55 printf ("sys_pagesize: % ld \ n ", sys_pagesize); 56 57/* Memory read-only */58 // addr = (char *) mmap (NULL, MMAP_FILE_SIZE, PROT_READ, MAP_SHARED, file_fd, 0); 59 60/* ing is greater than the file length and greater than the file page size */61 // addr = (char *) mmap (NULL, sys_pagesize + 1, PROT_READ | PROT_WRITE, MAP_SHARED, file_fd, 0); 62 63/* normal allocation */64 addr = (char *) mmap (NULL, MMAP_FILE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, file_fd, 0); 65 if (addr = MAP_FAILED) 66 err_exit ("mmap ()"); 67 68/* Raw Data */69 printf ("old text: % s \ n ", addr); 70 71/* cross-border access */72 // addr + = sys_pagesize + 1; 73 // printf (" out of range: % s \ n ", addr); 74 75/* Copy new data */76 text_len = strlen (argv [1]); 77 memcpy (addr, argv [1], text_len); 78 79/* synchronize data in the ing zone */80 // if (msync (addr, text_len, MS_SYNC) =-1) 81 // err_exit ("msync () "); 82 83/* print new data */84 printf (" new text: % s \ n ", addr ); 85 86/* unmapped Region */87 if (munmap (addr, MMAP_FILE_SIZE) =-1) 88 err_exit ("munmap ()"); 89 90 return 0; 91}

1. Create a 10-byte file first:

1 $:dd if=/dev/zero of=a.txt bs=1 count=10

2. After the program is compiled and run, execute 2 in sequence to write:

The page size on the local machine is 4096 bytes. Nine bytes are written for the first time. The files originally created using the dd command are empty, and the old text is empty. The second write contains 4 bytes, which only overwrites the first 1234 bytes.

3. Verify that the existing paging memory can be accessed. Write Data exceeding 10 bytes:

We have written 17 bytes above, although the 64-row mmap () maps MMAP_FILE_SIZE = 10 bytes. But we can see from the input new text that we can still access the memory after 10 bytes, because the data is in a single page (4096. After catw.a.txt, only the first 10 words are written into a.txt.

4. Verify the SIGSEGV signal. Tune 64 lines of comments, open 58 lines, set the ing attribute to read-only, and access after compilation:

After the read-only attribute is set, write operations are performed on Row 1. Our custom signal processor captures this signal. If there is no custom signal processor, the terminal will output Segmentation fault

5. Verify the SIGBUS signal. Map the memory with 61 rows. Maps a page size plus 1 byte memory, and releases lines of code to point the pointer to a paging area. Compile and run:

The SIGBUS signal is captured by the custom processor. If there is no custom signal processor, the terminal will output a Bus error

Iv. Anonymous ing

There are two methods for anonymous ing:

1. Specify the flags parameter of mmap () as MAP_ANONYMOUS. The fd parameter value will be ignored after this value is specified in linux. However, in some UNIX systems, you need to specify fd as-1.

2. Open/dev/zero as a file descriptor. When reading data from/dev/zero, it will provide you with endless 0 s, write data to it, and it will discard. Discard is the same as/dev/null, but/dev/null does not provide data with you.

3. The usage of anonymous ing is similar to that of the preceding file ing. The example is not provided here.

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.