当前位置:网站首页>Embedded UC (UNIX System Advanced Programming) -3

Embedded UC (UNIX System Advanced Programming) -3

2022-07-05 17:04:00 Orange peel does not stop school

One 、 Memory

1. Memory :

Virtual memory : Is the address space , Virtual storage area , All applications access is virtual memory .

( The memory we access in the program is basically not real , It's virtual. , In fact, it is stored in physical memory , Through memory mapping , Complete the storage from virtual to physical .)

Physical memory : Storage space , Actual storage area , Only the system kernel can access physical memory .

There is a correspondence between virtual memory and physical memory , When an application accesses virtual memory , The system kernel will find the corresponding physical memory according to this correspondence . The above correspondence is stored in the kernel Memory mapping table in .

Physical memory includes semiconductor memory and paging file :

When semiconductor memory ( Equivalent to memory module ) Not enough time , Some long-term idle code and data can be cached from the semiconductor memory to the page change file ( It's like a hard disk ) in , This is called Page out . Once you need to use the replaced code and data , Then restore them from the page feed file to the semiconductor memory , This is called Page change in . therefore , The virtual memory in the system is much larger than the semiconductor memory .

2. Process mapping :

Each process has its own 4G Bytes of virtual memory , Are mapped to different physical memory areas .

Memory mapping and swap in / out are both in page units ,1 page =4096 byte .

4G In virtual memory :

High address 1G Code and data areas mapped to the kernel , this 1 individual G Share between processes . Users' applications can only be accessed directly Low address 3 individual G Virtual memory , Therefore, this area is called user space , and High address 1 individual G Virtual memory is called kernel space .

Code in user space can only directly access data in user space , If you want to access code and data in kernel space, you must use special system call complete .

User space 3G Virtual memory can be further divided into the following areas :

  Use the code to see how the actual address is :

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
const int const_global = 10; //  Constant global variable 
int init_global = 10; //  Initialize global variables 
int uninit_global; //  Global variables are not initialized 
int main(int argc, char* argv[]) {
    //  Constant static variable 
    const static int const_static = 10;
    //  Initialize static variables 
    static int init_static = 10;
    //  Static variable not initialized 
    static int uninit_static;
    //  Constant local variable 
    const int const_local = 10;
    int prev_local; //  Pre local variable 
    int next_local; //  Post local variable 
    //  Front heap variables 
    int* prev_heap = malloc(sizeof(int));
    //  Post heap variables 
    int* next_heap = malloc(sizeof(int));
    //  Literal constant 
    char const* literal = "literal";
    extern char** environ; //  environment variable 
    printf("----  Command line parameters and environment variables  ----\n");
    printf("          environment variable :%p\n", environ);
    printf("        Command line arguments :%p\n", argv);
    printf("--------------  Stack  ------------\n");
    printf("        Post local variable :%p\n",
        &next_local);
    printf("        Pre local variable :%p\n",
        &prev_local);
    printf("        Constant local variable :%p\n",
        &const_local);
    printf("--------------  Pile up  ------------\n");
    printf("          Post heap variables :%p\n", next_heap);
    printf("          Front heap variables :%p\n", prev_heap);
    printf("------------- BSS ------------\n");
    printf("  Global variables are not initialized :%p\n",
        &uninit_global);
    printf("  Static variable not initialized :%p\n",
        &uninit_static);
    printf("------------  data  ------------\n");
    printf("    Initialize static variables :%p\n",
        &init_static);
    printf("    Initialize global variables :%p\n",
        &init_global);
    printf("------------  Code  ------------\n");
    printf("        Constant static variable :%p\n",
        &const_static);
    printf("        Literal constant :%p\n", literal);
    printf("        Constant global variable :%p\n",
        &const_global);
    printf("              function :%p\n",
        main);
    printf("------------------------------\n");
    return 0;

give the result as follows :

  It can also be done through size Command to view the code area of an executable program 、 Data area and BSS The size of the area .

size maps 

The user space of each process has an independent mapping from virtual memory to physical memory , It is called the memory barrier between processes .

namely : We have two processes at the same time , The previous one writes a number into the virtual memory , Then hang it aside , The latter process also writes a number to the same virtual memory , But let the previous one read the number of this memory again , It's still the previous number , Will not be changed by the later process , There is Memory barrier , That is, use the same virtual memory , Nor in the same physical memory .

3. The allocation and release of memory :

3.1 Allocate or release virtual memory incrementally

Distribute : mapping + occupy

      mapping :  In the address space ( Virtual memory ) And storage space ( Physical memory ) Between Establish a mapping relationship      

      occupy : Specify the attribute of memory space

Release : Give up possession + Unmap

    Give up possession : Release the ownership constraint of memory space

    Unmap : Eliminate address space ( Virtual memory ) And storage space ( Physical memory ) Mapping between

Allocate or release virtual memory incrementally :

#include <unistd.h>    // You need to use the header file of the function

void* sbrk(intptr_t increment);

Successfully return the heap top pointer before calling this function , Failure to return -1.

increment Parameter description :

>0 - The heap top pointer moves up , Increase heap space , Allocate virtual memory

<0 - The heap top pointer moves down , Reduce heap space , Free virtual memory

=0 - Do not allocate or free virtual memory , Just return the current heap top pointer

The system kernel maintains a pointer , Point to Top of heap memory , That is, the next position of the last byte in the effective heap memory .sbrk Function based on incremental parameters increment Adjust the position of the pointer , At the same time, return the original position of the pointer , During this period, if memory is exhausted or idle , Automatically append or cancel the mapping of the corresponding memory page .

3.2 Allocate or release virtual memory in the form of absolute address :

int brk(void* end_data_segment);

Successfully returns 0, Failure to return -1.

end_data_segment

> Current heap top , Allocate virtual memory

< Current heap top , Free virtual memory

= Current heap top , Empty operation

The system kernel maintains a pointer , Point to the current heap top ,brk Function based on pointer parameters end_data_segment Set the new position of the heap top , During this period, if memory is exhausted or idle , Automatically append or cancel the mapping of the corresponding memory page .

3.3 Establish the mapping from virtual memory to physical memory or files

#include <sys/mman.h>

void* mmap(void* start, size_t length, int prot,

    int flags, int fd, off_t offset);

Successfully returns The starting address of the virtual memory of the mapping area , Failure to return MAP_FAILED(void* Type of -1).

start - The starting address of the virtual memory of the mapping area ,NULL Indicates automatic selection

length - Number of bytes in the mapping area , Automatically round by page

prot - Access right , Take the following values : 

PROT_READ - Can be read

PROT_WRITE - Can write

PROT_EXEC - Executable

PROT_NONE - inaccessible

flags - Mapping flags , Take the following values :

MAP_ANONYMOUS - Anonymous mapping : Map virtual memory to physical memory , Then the last two parameters of the function fd and offset Be ignored

MAP_PRIVATE - Private mapping , Map the virtual memory into the memory buffer of the file instead of the disk file

MAP_SHARED - Share mapping , Map virtual memory to disk file

MAP_DENYWRITE - Write reject mapping , The mapped area in the file cannot have other write operations

MAP_FIXED - Fixed mapping , If in start Cannot create mapping on , The failure ( Without this sign, the system will automatically adjust )

MAP_LOCKED - Lock mapping , It is forbidden to be swapped out to a page change file

fd - File descriptor

offset - File offset , Automatic page alignment

3.4 Unmap virtual memory from physical memory or files

int munmap(void* start, size_t length);

Successfully returns 0, Failure to return -1.

start - The starting address of the mapping area

length - Number of bytes in the mapping area

Compared with malloc Release , This is better ,malloc By free It is all released , If not, the length of the released space .

Examples of use :

Release example :

原网站

版权声明
本文为[Orange peel does not stop school]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140511475726.html