当前位置:网站首页>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 :
边栏推荐
- 【刷题篇】有效的数独
- 【jmeter】jmeter脚本高级写法:接口自动化脚本内全部为变量,参数(参数可jenkins配置),函数等实现完整业务流测试
- 深潜Kotlin协程(二十一):Flow 生命周期函数
- Error in composer installation: no composer lock file present.
- 为季前卡牌游戏 MotoGP Ignition Champions 做好准备!
- 【剑指 Offer】66. 构建乘积数组
- 【剑指 Offer】61. 扑克牌中的顺子
- 【剑指 Offer】62. 圆圈中最后剩下的数字
- Iphone14 with pill screen may trigger a rush for Chinese consumers
- How does win11 change icons for applications? Win11 method of changing icons for applications
猜你喜欢
How was the middle table destroyed?
【 brosser le titre 】 chemise culturelle de l'usine d'oies
腾讯音乐上线新产品“曲易买”,提供音乐商用版权授权
Win11 prompt: what if the software cannot be downloaded safely? Win11 cannot download software safely
Solution of vant tabbar blocking content
The survey shows that the failure rate of traditional data security tools in the face of blackmail software attacks is as high as 60%
Games101 notes (II)
中国广电正式推出5G服务,中国移动赶紧推出免费服务挽留用户
Clear restore the scene 31 years ago, volcanic engine ultra clear repair beyond classic concert
解决CMakeList find_package找不到Qt5,找不到ECM
随机推荐
【剑指 Offer】62. 圆圈中最后剩下的数字
【剑指 Offer】63. 股票的最大利润
[first lecture on robot coordinate system]
国产芯片产业链两条路齐头并进,ASML真慌了而大举加大合作力度
干货!半监督预训练对话模型 SPACE
Games101 notes (I)
Get ready for the pre-season card game MotoGP ignition champions!
采用药丸屏的iPhone14或引发中国消费者的热烈抢购
Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
The first EMQ in China joined Amazon cloud technology's "startup acceleration - global partner network program"
Writing method of twig array merging
Jarvis OJ webshell analysis
The two ways of domestic chip industry chain go hand in hand. ASML really panicked and increased cooperation on a large scale
Learnopongl notes (II) - Lighting
[team PK competition] the task of this week has been opened | question answering challenge to consolidate the knowledge of commodity details
Games101 notes (II)
腾讯音乐上线新产品“曲易买”,提供音乐商用版权授权
Fleet tutorial 09 basic introduction to navigationrail (tutorial includes source code)
Is it safe to open futures accounts online? Will there be more liars online? Doesn't feel very reliable?
Apple has abandoned navigationview and used navigationstack and navigationsplitview to implement swiftui navigation