To understand the execution process of a program, The first thing to understand is what a compiler is. We useC Program written in language, Computers can only recognize machine language( Binary instruction), Computers don't understand. therefore, Must
takeC 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 speaking(*.obj) file,Linux Down for(*.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 as01 Composed bit sequence,8 Bit by byte, Each byte represents a text character, It is based onascii In the form of codes. Only byascii 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. withc 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 originalc program, such as#include<stdio.h> Tell preprocessor to read system header filestdio.h Content, And insert it directly into the program text, take#define And so on, And we got another onec program, Usually with.i As file extension.

  Compiler will text filehello.i Translate intohello.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, And save the results inhello.o in.hello.o File is binary, Because his bytecode is machine language instruction, notascii code. If you open it with a text editorhello.o You'll see a mess.

  Link stage, such ashello Used inprintf function, This is the standard.c Library functions, Exists in aprintf.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, Results obtainedhello file, It is an executable target file, Can be loaded into memory, Executed by system.

  After the conversion is completed, it is executed. stayunix 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-inshell command,shell Interpret it as an executable, For example, input../hello, It will load and run the file.hello Output information on screen, Then Output a prompt, Command line waiting for next input. The specific process is: Initial time,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 executablehello file, takehello 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 filehello Code and data in are loaded into main memory, Processor starts executionmain 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, So it happened.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, Includepc 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.