当前位置:网站首页>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 :
边栏推荐
- 【刷題篇】鹅廠文化衫問題
- 阈值同态加密在隐私计算中的应用:解读
- 外盘期货平台如何辨别正规安全?
- If you can't afford a real cat, you can use code to suck cats -unity particles to draw cats
- [brush title] goose factory shirt problem
- 网站页面禁止复制内容 JS代码
- Little knowledge about C language (array and string)
- 网上办理期货开户安全吗?网上会不会骗子比较多?感觉不太靠谱?
- Data verification before and after JSON to map -- custom UDF
- Jarvis OJ simple network management protocol
猜你喜欢
Jarvis OJ Telnet Protocol
【Web攻防】WAF检测技术图谱
美国芯片傲不起来了,中国芯片成功在新兴领域夺得第一名
Android privacy sandbox developer preview 3: privacy, security and personalized experience
如何将mysql卸载干净
Enter a command with the keyboard
干货!半监督预训练对话模型 SPACE
Etcd build a highly available etcd cluster
高数 | 旋转体体积计算方法汇总、二重积分计算旋转体体积
Summary of methods for finding intersection of ordered linked list sets
随机推荐
ECU简介
二叉树相关OJ题
[61dctf]fm
C language to get program running time
【 brosser le titre 】 chemise culturelle de l'usine d'oies
JSON转MAP前后数据校验 -- 自定义UDF
Jarvis OJ Webshell分析
Deep dive kotlin synergy (XXI): flow life cycle function
[deep learning] [original] let yolov6-0.1.0 support the txt reading dataset mode of yolov5
easyNmon使用汇总
Use byte stream to read Chinese from file to console display
Benji Banas membership pass holders' second quarter reward activities update list
国内首家 EMQ 加入亚马逊云科技「初创加速-全球合作伙伴网络计划」
Is it safe for qiniu business school to open a stock account? Is it reliable?
Browser rendering principle and rearrangement and redrawing
Excuse me, is the redis syntax used in DMS based on the commands of the redis community version of the cloud database
Hiengine: comparable to the local cloud native memory database engine
What else do you not know about new map()
Flet tutorial 12 stack overlapping to build a basic introduction to graphic and text mixing (tutorial includes source code)
中国广电正式推出5G服务,中国移动赶紧推出免费服务挽留用户