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

边栏推荐
- easyNmon使用汇总
- composer安装报错:No composer.lock file present.
- 齐宣王典故
- 深潜Kotlin协程(二十一):Flow 生命周期函数
- Scratch colorful candied haws Electronic Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022
- Jarvis OJ Webshell分析
- 国内首家 EMQ 加入亚马逊云科技「初创加速-全球合作伙伴网络计划」
- Iphone14 with pill screen may trigger a rush for Chinese consumers
- How to set the WiFi password of the router on the computer
- Do sqlserver have any requirements for database performance when doing CDC
猜你喜欢

How to set the WiFi password of the router on the computer

Jarvis OJ 简单网管协议

如何安装mysql

The two ways of domestic chip industry chain go hand in hand. ASML really panicked and increased cooperation on a large scale

【机器人坐标系第一讲】
![[729. My Schedule i]](/img/e3/32914227d00cf7595ee850e60f2b72.png)
[729. My Schedule i]

Cs231n notes (bottom) - applicable to 0 Foundation

Jarvis OJ Telnet Protocol

【刷题篇】有效的数独

Android 隐私沙盒开发者预览版 3: 隐私安全和个性化体验全都要
随机推荐
Detailed explanation of use scenarios and functions of polar coordinate sector diagram
【组队 PK 赛】本周任务已开启 | 答题挑战,夯实商品详情知识
Yarn common commands
[Jianzhi offer] 62 The last remaining number in the circle
【剑指 Offer】66. 构建乘积数组
Jarvis OJ shell traffic analysis
American chips are no longer proud, and Chinese chips have successfully won the first place in emerging fields
【刷题篇】有效的数独
腾讯音乐上线新产品“曲易买”,提供音乐商用版权授权
The difference between searching forward index and inverted index
Jarvis OJ Telnet Protocol
Hiengine: comparable to the local cloud native memory database engine
为季前卡牌游戏 MotoGP Ignition Champions 做好准备!
美国芯片傲不起来了,中国芯片成功在新兴领域夺得第一名
挖财股票开户安全吗?怎么开股票账户是安全?
[brush title] goose factory shirt problem
Keras crash Guide
调查显示传统数据安全工具面对勒索软件攻击的失败率高达 60%
齐宣王典故
[729. My schedule I]