当前位置:网站首页>Self cultivation of programmers
Self cultivation of programmers
2022-07-28 15:32:00 【Flying_】
Problems in running applications directly on physical memory :
1、 Address space is not isolated
2、 Memory efficiency is low
3、 The program running address is uncertain
MMU( Memory management unit ) Map virtual memory to physical memory
Thread scheduling : The behavior of constantly switching different threads on the processor is called thread scheduling
Threads also have at least three states :
function
be ready
wait for
A running thread has a period of time to execute , This period of time is called time slice (Time Slice), When the time slice runs out , The process will enter the ready state .
Windows Atomic operation in API
InterlockedExchange // Exchange two values atomically
InterlockedDecrement // Atomically reduce by one value
InterlockedIncrement // Add a value atomically
InterlockedXor // Perform XOR operations atomically
CPU Possible problems caused by disorder :
Double check lock in singleton mode
Volatile T* pInst = null;
T* GetInstance()
{
If(pInst == null)
{
Lock();
If(pInst == NULL)
{
pInst = new T;
}
unlock();
}
return pInst;
}
This code , There is no problem leaving logic behind , But there are actually hidden problems , The source of the problem is CPU Out of order execution of
C++ Inside new There are two steps :
1) Allocate memory
2) Call constructor
So here pInst = new T; There are three steps
1) Allocate memory
2) Call the constructor at a location in memory
3) Assign the address of memory to pInst
In these three steps 2 and 3 It can be reversed . in other words pInst Memory has been allocated , And also assigned to pInst, But at this time, the constructor has not been completed , There is another thread calling , At this time, it will return to the constructor that has not been executed pInst Object to provide users with . At this time, whether the program will crash depends on the design of this class .
To solve this problem , have access to CPU An instruction provided ,barrier()
Volatile T* pInst = null;
T* GetInstance()
{
If(pInst == null)
{
Lock();
If(pInst == NULL)
{
T* pTemp = new T;
Barrier()
pInst = pTemp;
}
unlock();
}
return pInst;
}
because barrier The existence of , The structure of the object must be barrier Complete before execution , Because when pInst When assigned , The returned object is always constructed .
precompile :
gcc -E hello.c -o hello.i
compile
gcc -S hello.i -o hello.s
assembly
gcc -c hello.s -o hello.o
What did you do in the precompile phase ?
1、 Process all precompiled instructions #if #elif #else #endif #define #include
2、 Delete all comments
3、 Add line number and filename ID , So that the compiler can prompt the line number of errors or warnings
4、 Keep all #pragma Compiler Directives
compiler The process is to perform a series of parsing on the preprocessed file 、 After semantic analysis and optimization, the corresponding assembly code files are produced .
Assembler Is to convert assembly code into instructions executed by one of the machines , Almost every assembly statement corresponds to a machine instruction .
What did the compiler do ?
The most direct description compiler is a tool for translating high-level languages into machine languages .
The compilation process can generally be divided into 6 Step :
scanning 、 Syntax analysis 、 Semantic analysis 、 Source code optimization 、 Code generation 、 Target code optimization
Scanner
parsers : Parsing the marks generated by the scanner , Generate a grammar tree
semantic analyzer
Source level optimizer
Linking process :
Modules rely on symbols to communicate, similar to jigsaw puzzles , The module defining the symbol has one more area , The module that references the symbol is just missing that area , The two are spliced , Just the perfect combination . The splicing process of this module is the link (Linking)
The static linking process is to compile the source code file of each module into an object file through the compiler (object File The general extension is .o or .obj), Target files and libraries (Library) Link together to form the final executable .
A library is actually a set of packages of target files , Some of the most commonly used codes are compiled into object files and packaged for storage .
Compiled object file
The file generated after the compiler compiles the source code is called the object file . The object file is structurally , It's a compiled executable file format , It's just that it hasn't been linked yet , There may be some symbols or some addresses that have not been adjusted . In fact, it is stored in executable file format , It's just slightly different from the real executable file in structure .
The format of the target file :
windows Under the PE(Portable Executable)
linux Under the ELF(Executable Linkable Format)
Object files are intermediate files that are compiled but not linked (windows Of .obj and Linux Under the .o), It is very similar to the content and structure of executable file , Therefore, it is generally stored in one format together with the executable file format .
边栏推荐
- 分享一下一二线大厂HR面经分享
- .net core version 2.2 cross domain configuration
- 7/13 (pond sampling)
- QCustomPlot绘图工具常用方法
- Pycharm - output exception of program run and default comment of added function
- Here comes the full open source free customer service system
- No files or folders found to process
- Leetcode - number of operations, non repeating numbers, diagonal traversal, Joseph Ring
- Write a standard character device driver with your hands
- 电压继电器DY-28C
猜你喜欢
![[jspwiki]jspwiki installation deployment and configuration](/img/3c/81a201bb80dcbb17d1c97b1a5bb215.png)
[jspwiki]jspwiki installation deployment and configuration

Understanding of entropy and cross entropy loss function

MPLS LDP的原理与配置

crmeb标准版附带的客服系统
软件测试的流程规范有哪些?具体要怎么做?

What functions will be added to crmeb Standard Version 4.4

MIT指出公开预训练模型不能乱用

PMP practice once a day | don't get lost in the exam -7.28 (including agility + multiple choices)

2022年最火的十大测试工具,你掌握了几个

R introduction example details
随机推荐
Leetcode - number of operations, non repeating numbers, diagonal traversal, Joseph Ring
Grpc frequently asked questions
有奖活动分享:使用WordPress搭建一个专属自己的博客后最高可领取iPhone13
根据输入target,返回数组的两个下标。
手把手带你编写一个规范的字符设备驱动
subst命令将一个文件夹镜像成本地的一个磁盘
Principle and configuration of MPLS LDP
【通道注意力机制】SENet
Multi merchant mall system with relatively clean code
封装统一返回对象MessageResult
10、相关数据累积任务实现
Installing CONDA and configuring Jupiter
PMP practice once a day | don't get lost in the exam -7.28 (including agility + multiple choices)
最小堆提升每次排序的效率
HJS-DE1/2时间继电器
crmeb 标准版window+phpstudy8安装教程(三)
.net core version 2.2 cross domain configuration
.net core 2.2 版本跨域配置
设置结构体字节对齐
Collation of MySQL error prone knowledge points (to be updated)