当前位置:网站首页>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 .
边栏推荐
- Start from scratch 10- background management system development
- TC software outline design document (mobile group control)
- Es composite query workload evaluation
- 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!
- Mythical games announced its cooperation with kakao games, a leading Korean game publisher, to promote business expansion in the Asia Pacific Region
- 注解的定义以及注解编译器
- TensorFlow,危!抛弃者正是谷歌自己
- Is it safe for Guojin securities to open an account?
猜你喜欢

燎原之势 阿里云数据库“百城聚力”助中小企业数智化转型

Introduction to ground plane in unity

New programmers optimize a line of code on Monday and are discouraged on Wednesday?

Huawei releases wireless innovative products and solutions to build 5gigaverse Society

Performance optimization - image compression, loading and format selection

字符串

Will the thunderstorm of Celsius be the "Lehman moment" in the field of encryption?

ESP8266/ESP32 +1.3“ or 0.96“ IIC OLED指针式时钟

TC软件详细设计文档(手机群控)

About Alipay - my savings plan - interest rate calculation instructions
随机推荐
Why does C throw exceptions when accessing null fields?
【云原生 | Kubernetes篇】Kubernetes 配置(十五)
字符串
Eureka的TimedSupervisorTask类(自动调节间隔的周期性任务)
WCF RestFul+JWT身份验证
K-means introduction
Mid 2022 Summary - step by step, step by step
The execution process before executing the main function after the DSP chip is powered on
Dapr advanced-01-debug dapr
AI越进化越跟人类大脑像!Meta找到了机器的“前额叶皮层”,AI学者和神经科学家都惊了...
DSP online upgrade (3) -- how to burn two projects in the on-chip flash of a DSP chip
Vuforia引擎支持的版本
Embedded software project process and project startup instructions (example)
Original code, inverse code, complement calculation function applet; C code implementation;
Es composite query workload evaluation
TC软件详细设计文档(手机群控)
【云驻共创】企业数字化加速“新智造”
How to convert mindspire model to onnx format and use onnxruntime reasoning - development test
New year's Eve, are you still changing the bug?
并发底层原理:线程、资源共享、volatile 关键字