To understand the execution process of a program , The first thing to understand is what a compiler is . We use C Program written in language , Computers can only recognize machine language （ Binary instruction ）, Computers don't understand . therefore , must
take C Language to machine language . Compiler is used to achieve this function . The compiler maps the source code file to binary instructions recognized by the range computer according to certain corresponding rules .
About source files , Target files and executables
source file ： Save source file （*.c, *.cpp)
Target file ： The machine language instructions created by the compiler according to the source file become the target code , The disk file containing the target code is the target file .
Generally （*.obj） file ,Linux Next is （*.o） file .
The target file generated after compiling the source code cannot be executed directly . In general, we write programs that reference basic library functions , Therefore, before running the program, you need to replace the library function with the defined object code , This process is called linking . It is to combine the target file of the source code and the target file of the library function , Generate final executable . Linking is done by linker .
Execution of general procedures ：
The program is first edited and created by the programmer and saved on the hard disk , The source program is represented as 01 Composed bit sequence ,8 Bit by byte , Each byte represents a text character , It is based on ascii In the form of codes . Only by ascii A file of characters is called a text file , All the others are called binaries .
In order for the machine to recognize and run the program , Each statement must be converted to a low-level machine language instruction , The instructions are then packaged in the form of executable target programs , And stored as binary disk file . with c Program as an example , The conversion process is roughly divided into pretreatment , compile , assembly , Link four steps . Let's explain in detail .
Preprocessor based on characters # The command at the beginning modifies the original c program , such as #include<stdio.h> Tell preprocessor to read system header file stdio.h Content of , And insert it directly into the program text , take #define And so on , And we got another one c program , Usually .i As file extension .
Compiler will text file hello.i Translated into hello.s, It contains an assembly language program . Every statement in an assembly language program exactly describes a low-level machine language instruction in a standard text format .
Assembler translates assembler into machine language instruction , Package these instructions into retargetable programs （relocateble object
program） Format of , And save the results in hello.o in .hello.o File is binary , Because his bytecode is machine language instruction, not ascii code . If you open it with a text editor hello.o You'll see a mess .
Link phase , such as hello Used in printf function , This is the standard c Functions of the library , Exists in a printf.o In the separately compiled target file of , This file must be merged into our compiled target file in some way . Linker （ld） The program is responsible for handling the merger , The result is hello file , It is an executable target file , Can be loaded into memory , Executed by system .
After the conversion is completed, it is executed . stay unix In the system ,shell Is a command line interpreter , Output a prompt , Wait for user input and execute command . If the first word entered is not a built-in shell command ,shell Interpret it as an executable , For example, input ./hello, It will load and run the file .hello Output information on screen , Then terminate .shell Output a prompt , Command line waiting for next input . The specific process is ： At the beginning ,shell Execute its instructions , Waiting for input . User input character creation “./hello” after ,shell Read the characters into the register one by one , Then store it in memory , After hitting enter ,shell Know that the user ends the command input . then shell Execute a series of instructions to load the executable hello file , take hello Copy the code and data of the target file from disk to main memory , Data contains the output string "HELLO,WORLD\n". Once the target file hello Code and data in are loaded into main memory , Processor starts execution main Machine language instructions for , Copy string from main memory to register , And output to the screen .
Because it involves a lot of main memory , disk , Register communication , Therefore, the cache Equipment with equal buffer speed , Reduce communication congestion .
In order to reduce the burden of users , The operating system abstracts the computer hardware resources , The process is generated , thread , Concepts such as virtual address . A process is an execution of a program , Is the unit in which the operating system allocates resources , Multiple processes can be executed concurrently , Concurrent execution is actually a process at every time , But the speed of inter process switching is relatively fast , It feels like concurrent execution . The operating system saves execution status information for each process , Called context , include pc And register file current value , Main memory content, etc . When switching processes , Context switch occurs . There can be multiple thread execution units in a process , Each thread runs in the context of the process , Share the same code and data , Due to the increasing demand for parallel processing in network servers and other applications , Multithreading model is becoming more and more important . The virtual address provides an illusion for each process , That is to say, every process has exclusive main memory , Each process sees consistent storage , Called virtual address space . Virtual address space is composed of a large number of precisely defined regions ,linux From low address to high address ： Program code and data ; heap ; Shared library ; Stack ; Kernel virtual memory .