当前位置:网站首页>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 :

边栏推荐
- Enter a command with the keyboard
- 网站页面禁止复制内容 JS代码
- 网上办理期货开户安全吗?网上会不会骗子比较多?感觉不太靠谱?
- Do sqlserver have any requirements for database performance when doing CDC
- 【beanshell】数据写入本地多种方法
- Solution of vant tabbar blocking content
- Games101 notes (III)
- [729. My Schedule i]
- Detailed explanation of use scenarios and functions of polar coordinate sector diagram
- 麻烦问下,DMS中使用Redis语法是以云数据库Redis社区版的命令为参考的嘛
猜你喜欢

【jmeter】jmeter脚本高级写法:接口自动化脚本内全部为变量,参数(参数可jenkins配置),函数等实现完整业务流测试

Browser rendering principle and rearrangement and redrawing

Jarvis OJ Flag

Android privacy sandbox developer preview 3: privacy, security and personalized experience

Solution of vant tabbar blocking content

【性能测试】jmeter+Grafana+influxdb部署实战

Keras crash Guide

Summary of methods for finding intersection of ordered linked list sets

Cs231n notes (bottom) - applicable to 0 Foundation

PHP talent recruitment system development source code recruitment website source code secondary development
随机推荐
Fleet tutorial 09 basic introduction to navigationrail (tutorial includes source code)
Learnopongl notes (I)
【729. 我的日程安排表 I】
[brush questions] effective Sudoku
How to install MySQL
Get ready for the pre-season card game MotoGP ignition champions!
[Jianzhi offer] 61 Shunzi in playing cards
Twig数组合并的写法
[729. My schedule I]
Is it safe to open a securities account by mobile phone? Detailed steps of how to buy stocks
C how TCP restricts the access traffic of a single client
It is forbidden to copy content JS code on the website page
American chips are no longer proud, and Chinese chips have successfully won the first place in emerging fields
Yarn common commands
【Web攻防】WAF检测技术图谱
拷贝方式之DMA
手机开证券账户安全吗?怎么买股票详细步骤
PHP strict mode
Sentinel-流量防卫兵
Deeply cultivate 5g, and smart core continues to promote 5g applications