当前位置:网站首页>Running view of program
Running view of program
2022-06-21 10:34:00 【Longcheng deficit】
In the previous section , This paper introduces the formation of executable file , And what is in the executable file . from CPU perspective , We use in memory compilers and linkers , Process the project files on the disk , The processing itself is done in memory . After processing, an executable file is generated , Store it on disk . For large projects , You may end up with multiple files , For example, there are various libraries and resource files . Through such a compilation and linking process , The code finally completes the gorgeous turn . In this part , Let's have a look , Code after turning around , How to layout into memory and change dynamically , That is, the complete process of loading and running the program , Including how the loader loads the executable related files on the disk into memory , And the scenario Display of the program changes during the memory operation .

Pictured above , Source code files on disk , After in memory compilation and linker processing , Become an executable program file , Back to disk . Executable on disk , After the loader loading process in memory , Become a running process .
To better demonstrate this change , We look at this change from different perspectives .
The first angle , Physical view
Just like the word "physics" in the name , In this part, we show the actual loading process and the establishment process of the preliminary running environment , Look at the complex process of executable program running , Mainly look at memory 、 disk 、CPU The relationship between .
Program loading execution , It can be triggered in many ways , For example, click a shortcut icon to start the program , This is the most common way to start a graphics system . You can enter the name of the program to be executed on the command line , from Shell To trigger execution , It can also be during the execution of a program , Through the system call interface , Start another program . Either way , Most of them need to give the name and location of the program directly or indirectly . Of course , If you pass based on the current process fork Part company each going his own way , In this way, the parent and child processes share the same code segment . But here, , Let's discuss the general situation .
Know where the program to be run is on the disk , What's your name , You can start the program through the system call interface . Take path and name as parameters , Pass to exec system call . The system call starts from the disk location specified by the path , Find the executable file , Load the executor .
So called load , The main thing is to transfer the contents of the executable file , Copy from disk to memory . This copy is not an irregular copy from beginning to end , Because on the one hand , Some parts of the executable file , Does not participate in the actual code run , It only provides auxiliary information for the loader to load the program ; On the other hand , The executable is divided into many sections , Need to remap in memory , Not necessarily physically continuous ( about Linux System , Can pass /proc/ process ID/mmap file , View the virtual memory space usage of the process ), It can be said that it is discontinuous in most cases . To complete the loading of the program , The operating system loader first needs to rely on the auxiliary information in the executable file , such as , Assisted relocation segment , Link library information, etc . By reading this information first , Finish loading the dependent libraries , Mapping of shared libraries , And the determination of relocation address value when necessary . Once the program is ready to run , You can give control to the code , the CPU Of PC The pointer jumps to the code entry , Start actually executing user code logic . If during execution , Found that the code is not in memory , Then the missing instruction loading is completed through the page missing interrupt ; In turn, , If memory is tight , The temporarily unused memory space data can be moved to external storage , Free up the corresponding memory space ( The mapping relationship ), To achieve small memory to run large programs . Actually , From this description, we can see , Because of the widespread use of virtual memory technology , In many cases , Relocation information is not determined during loading , But in the process of operation , Determine again according to the needs of the call . therefore , Find auxiliary modules for library locations , It needs to always be accompanied by the running of the program , With the whole life cycle . Here's the picture :

A more detailed diagram from the network is shown below :

Once the operating system gives control to the program itself , The logic implemented by the program begins to be realized . however , As shown in the figure above , The code and data of the program are not all loaded into memory , This is what copy rules should pay attention to . This has been described in the operating system process and memory section . In order to be in limited memory , Running multiple processes , The operating system implements the virtual memory mechanism , This allows the program not to load all of its memory , You can execute . therefore , In actual memory , Not all available parts of the program are loaded .
Actually , This part , There are some tails left from the upper part . such as , Is the program really executed from the main function ? After knowing that the compiler and connector are inserted horizontally in the middle , We actually understand , Everything is possible . They can be run before , Insert your own code , Anyway , When you jump to the user program , Just start with the main function . image C++ Inside , The global object may have been created before the main function starts . This is not in line with our intuitive feelings , But that's the reality , It is the compiler that implements C++ The concept of , Completed this action . Understand this , It helps us understand some definitions of language 、 Implementation of some concepts , Better combine theory with practice . This extra processing , It does not affect the logic flow of the program itself .
The discussion so far , There is still a long way to go , Because many details have been ignored . Among them , The key is the core function of the operating system , Process management and memory management . Load a program , The operating system needs to create a process structure for it , Manage , meanwhile , Memory management is also required , Provide mapping management , Ensure the creation of process virtual memory space . and , While the program is running , It is inevitable to use the services provided by the operating system , For example, file system related , Network related and so on . This process , It also includes the steps to establish the process running environment introduced in the operating system startup section . below , Just add this Prelude , Improve the loading and running of the program .
During the system call loader , Before the operating system is actually loaded , The details of the work done include : Establishment of process data structure , That is, the establishment of archives ; Establishment of operating environment , Same as Shell Process establishment process ; be based on fork Copy operation , Share a lot of operating system content , Modification of many structures involving operating system data segments in memory . in addition to , There is also the related content of virtual memory , Include : The starting address assigned by the compiler to the code ; Memory mapping structure of the whole process space, etc .
The whole process of creating process files and virtual memory space in the prelude is summarized in the following figure :( To be refined )

Here we are , To complete the whole process of setting up the environment required for the operation of a program . here , Look again. CPU The control pointer jumps to the program entry , The whole context and process , It's clear . After the program gains control , Began to run . In operation , If you find that some symbols have not been bound to the real address , Then the loader assists in these tasks , After binding , Then the flow of the program is executed ; Again , In operation , Some content has not been loaded into the memory page , It triggers a page failure interrupt , The operating system assists in loading the required memory pages , Then the program continues to run . Look at it this way , The running process of the program , It's also a feeling of stumbling .
In addition to these external factors that cause program flow interruption , Process switching cannot be avoided within the program itself , This is the use of operating system services . Unless it's an extremely simple program , A program with complete functions , In addition to its own logical implementation , It is difficult to avoid the use of operating system services . I also introduced , Operating system services are implemented through soft interrupts , Once the operating system service interface is invoked , The program flow switches to the code area of the operating system . After the operating system completes the logic of the interface , And then return by interrupt , Jump to the program code and run . The operating system is for all processes on the upper layer , It looks like a global shared library .
above , That is, we look at the operation of the program from the actual physical perspective . Look at “ fragmentation ” The scattered physical memory pages are virtualized into a complete memory space through the mapping table , In this complete memory space , Not only the code of our program 、 data , There are also many shared libraries , Like standards C library ,C++ library , There are also dynamic load libraries that are responsible for loading dynamic libraries , And the stack area , Of course, there are code snippets of the operating system kernel , Data segments and stacks, etc . All these are virtually distributed in linear space ( It is also paragraph by paragraph ), But the actual distribution is discontinuous ( It can be imagined as piecemeal ) In the physical page . This is the program in memory from the physical point of view .
Second angle , Logical view
If you want to analyze one program at a time , The whole process should be drawn as above , That would be too much trouble . and , Entangled in the details of the operating system , It will affect the focus on program functions . Besides , Let the code part exist in memory , Some exist on disk , It is also not conducive to understanding program functions . Understand the details of the operating system , It helps us understand the loading and running process of the program , Once the process is straightened out , It will dispel our doubts , thus , You can put more energy into functional implementation analysis . After all , The whole loading process is like a channel , Opened the , You can focus on the world on the other side of the channel . This is also consistent with the cognitive process .
Say more . The operating system does these complicated and tedious tasks , I understand , Is to open the channel , Analysis of practical problems , Or should we prefer the abstract and logical point of view . however , In turn to see , Understand access , It is helpful for the analysis of real complex problems , And with the aid of access , Form a process that you can easily understand and an image in your mind . among , One is the logic block diagram , One is linear CPU Add memory sequence execution diagram , Associate the two , Give full play to each other's advantages , Thus, it is more conducive to the solution of the problem . This is from abstraction to reality , From reality to abstraction .
Based on the above understanding , Back , No more details about the operating system , But think CPU What you see is the entire program that exists in memory , Ignore the existence of that part of the disk . further , Take out the operating system , As CPU Packaging , In this way , A logical program diagram , Just before us .

here , There are also code snippets in the block , Data segments, etc , This level , It is a process of intermediate evolution .
Third angle , Abstract view
Since the second level throws away many details of the operating system , This time, , Throw more . Simply will CPU Imagine being your own brain , The brain is loaded with all the logic , And in memory , Just like the paper here , Save only the data that needs to be recorded , and , Data can also be restored to what it looked like when it was written , Data structure . disk , It can also be imagined as something else , In this way , View of the entire operation , It can be abstracted as the following .

This is a way closer to language and people's understanding of the world .
One side is simulated by the brain CPU, Read the instruction tape ; One side is memory , Operating data structures .
If during the analysis , Some parts are hard to understand , You can go back from the third level to the second level , If not , From the second level to the first level , Use the underlying details to deepen your understanding .
Then continue our discussion .
Fourth angle , Further extension of the abstract view
What we introduced earlier , There is a question , Just don't see the complexity of the program itself . Even for a super simple program , For example, there is only one sentence printf Printed c Program , The operating system loads the process that runs it , It is also a step that cannot be omitted . The process , It has its own complexity . When we throw away the details , After stepping into the program itself , Pure abstract views can really help understand programs , But as the complexity of the program increases , This level of view also gradually shows the characteristics of being too low-level .
however , A good sign is , Even the most complicated program , Its complexity is mostly limited to the program itself , At the operating system level , That is, from the physical view and the logical view , It's not much different from the simplest program , Not because the program itself is complex , This leads to complex loading and running . It's just a few more dynamic libraries .
that , The above loading part is discarded , How to solve the complexity of the program itself ? This needs to grasp a key point -- Man himself . Because no matter how complex the program is , For now , It's all written by people , So people understand the process of things , It should also be applicable to the understanding of the procedure . The foundation of this is the abstraction that cannot be overemphasized in the following architecture introduction . Language itself is also working in this direction . such as , People's understanding of themselves , Including the system 、 organ 、 organization 、 cells ... And so on . Create these concepts , To facilitate disassembly and assembly . From the system to the cell , It's decomposition , From cell to system , It's assembly . Both the heart and the skin are made of cells , But the cells that make up them are different . Except for human beings , People's understanding of other things in the material world , It's the same thing . This abstract and modular cognitive approach , It is also the foundation of complex program construction .
Back to the question itself , obviously , For complex programs , We can no longer focus too much on a particular data structure , But just like the cognition of other things , Need to further throw away too fine things , See the more essential side , In this way, it can be abstracted into a concept , On the link of communication . such , To have a holistic view . Our study of the operating system itself is a good example . That's the only way , Can be combined with the underlying operation , Achieve three-dimensional mastery . On the basis of serial binary execution , Three dimensional parallel multi module concurrency . It is like assembling the elements in the program design again in memory , Or to put it another way , It can be imagined as a building block in the memory . This cartoon on the Internet has this meaning :( come from http://turnoff.us/)

Another example is the browser , The internal composition is very large and complex , We can't understand it with data structure . however , We can think of some of these functional modules as independent blocks , Building blocks , Finally, assemble the browser you understand . This level , Everyone can have their own understanding , That is to say, it is easy to understand , As long as it can correspond to the next level . Here is my debugging Android Browser layer , For the sake of understanding , A picture drawn :

This arrangement , Ignored a lot of details , We need to pay attention to . I hope that when I encounter problems , Be able to grasp the whole first , After figuring out all the processes , Then we will go into the details , Branches and leaves .
The fourth perspective , The basic use is imagination , Imagine the program running , Think CPU Busy doing this and that ...CPU Can be a memory Porter , Can be a video storage Porter , You can achieve almost everything you want , There is a lot of room for innovation , It can be said that only the unexpected , No, I can't . therefore , This is a very interesting thing , It can also become a very meaningful thing .
Come here , You can lead to the next chapter : Dynamic extension of program . The so-called complexity is a necessity .
It's like include The role in programming , Many system level designs are composed of small design modules 、 Nesting and other processes have expanded . This is an inevitable requirement for the trend towards enlargement and complexity . Let's take a look , How does a program support its own continuous expansion as a platform , To meet future needs , Cater to the needs of business .
边栏推荐
- Vuforia引擎支持的版本
- Start from scratch 10- background management system development
- 中国国际电子商务中心与易观分析联合发布:2021年4季度全国网络零售发展指数同比增长0.6%
- 嵌入式远程岗位、兼职、接单、众包平台
- Polymorphic & class object & registered factory & Reflection & dynamic proxy
- WCF RestFul+JWT身份验证
- Positive results of enx-101 clinical 1b study published by English topics
- 性能优化——图片压缩、加载和格式选择
- AI越进化越跟人类大脑像!Meta找到了机器的“前额叶皮层”,AI学者和神经科学家都惊了...
- Introduction to ground plane in unity
猜你喜欢

Why does C throw exceptions when accessing null fields?

China international e-commerce center and Analysys jointly released: the national online retail development index in the fourth quarter of 2021 increased by 0.6% year on year

Xidian AI ranked higher than Qingbei in terms of AI major, and Nantah ranked first in China in terms of Software Science in 2022

Matplotlib two methods of drawing torus!

Three elements of basic concepts and methods of machine learning

字符串

程序员新人周一优化一行代码,周三被劝退?

Performance optimization - image compression, loading and format selection

性能优化——图片压缩、加载和格式选择

The execution process before executing the main function after the DSP chip is powered on
随机推荐
异常
The more AI evolves, the more it resembles the human brain! Meta found the "prefrontal cortex" of the machine. AI scholars and neuroscientists were surprised
Matplotlib two methods of drawing torus!
Eureka的TimedSupervisorTask类(自动调节间隔的周期性任务)
How to convert mindspire model to onnx format and use onnxruntime reasoning - development test
Quick sorting, simple and easy to understand principle description, with C code implementation,
Odd number of characters异常
触摸按键控制器TTP229-BSF使用心得[原创cnblogs.com/helesheng]
TC software outline design document (mobile group control)
Mythical Games宣布与韩国领先游戏发行商Kakao Games合作,推动亚太区业务扩张
Black Monday
TC software detailed design document (mobile group control)
Three elements of basic concepts and methods of machine learning
中部“第一城”,网安长沙以何安网?
China international e-commerce center and Analysys jointly released: the national online retail development index in the fourth quarter of 2021 increased by 0.6% year on year
Xidian AI ranked higher than Qingbei in terms of AI major, and Nantah ranked first in China in terms of Software Science in 2022
optional类,便利函数,创建Optional,Optional对象操作以及Optional流
Ccs7.3 how to erase only part of the flash sector when burning DSP on-chip flash (two projects of on-chip flash burning of a DSP chip)
Original code, inverse code, complement calculation function applet; C code implementation;
TensorFlow,危!抛弃者正是谷歌自己