In the computer program is how it works
University, computer science for four years but for this problem has not been in-depth understanding;
only a vague idea program needs to be compiled into a binary executable file on the computer can execute;
Recently, suddenly especially like to know specifically how kind?
So, thinking about writing an article about the record;
this question, I want to wait a few more years it will certainly have a deeper view.
If there is no place where understanding, please correction.
The program is run on a computer;
the application is running and can not do without call processing operating system;
applications are written in a programming language;
The first was written in: 23 February 2020
computer
Modern society can not do without the computer as a means of production already, the importance of self-evident;
Computer history
Short history of the computer, but the development is extremely rapid;
from large machines occupy a whole floor of the flat to the phone now.
Functions become more powerful, has become even more widespread use.
However, think about why this thing does have a computer?
We do not live too good in the absence of a computer?
Computer before developed, people can only rely on the process of calculating the manpower or simple tools (for example: abacus) to handle;
can not meet people's needs;
So we developed a computer in the efforts of their predecessors;
Learn a lot of techniques, it is based on real problems as a starting point.
Understand why we need this technology and the history of technology, to understand and use is crucial;
Composition of the computer
Von Neumann computer system has the following five parts and the corresponding real hardware.
- Calculator CPU
- Controller CPU
- RAM memory, a hard disk
- Input device keyboard, mouse, etc.
- An output display or the like equipment components
Wherein the CPU contains a register and arithmetic
We want to know the computer it can only handle binary data.
Most people do is start a binary way to go to the computer calculated;
People paper punch, is used to represent 0/1 and then to the computer to calculate;.
But this too human, it is troublesome.
People invented the assembly language;
assembly language, it can be regarded as humans can read and understand;
in general this is the following:
main:
.LFB1:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $16, %rsp
movl %edi, -4(%rbp)
movq %rsi, -16(%rbp)
movl $2, %esi
movl $1, %edi
call sum
movl $0, %eax
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
Although there has been some such code is readable, but also more disgusting to write.
And each CPU machine instructions are not the same, the corresponding assembly language is not the same.
Thus, people think of the invention can be cross-platform and language more conveniently handler;.
Thus, C language was invented out;
Extremely important appearance of the C language, which is the basis of many systems, programming languages.
C appears to reflect the language of 封装
the 分层
two important ideas;
people binary package into assembly language instruction, the machine will separate the people;
Further, it again compilation package, cross-platform program can be run on different hardware;
shielding the underlying implementation details transparent to the user.;
C is a universal language, the computer program for procedural languages. In 1972, in order to transplant and development of the UNIX operating system, Dennis Ritchie at the Bell Telephone Laboratories developed the C language design.
operating system
Using the computer operating system is the product of the process.
In the beginning, there is no computer operating system;
then the weak capacity of the computer, computer utilization can;
But with the continuous development of computer hardware; computer's ability to get a huge upgrade;
This creates a lot of waste of computing resources;
Why should the operating system
If there is no time operating system, productivity and efficiency of the human machine is relatively low.
So the operating system to provide users with a simple operation command, and to facilitate system software design language processing program, commissioning procedures.
Improve the utilization of computer resources, but also shield some of the implementation details;
after bare metal systems with operating systems and other software, it will become a command only understands, but also understands the various high-level language, easy to operate using the computer system.
Operating system to solve the problem
Computer's operating system is equipped with a large-scale system program, use it to achieve their own computer system hardware and software resources management.
- Increase computer resource utilization,
- Increase in response speed of the computer system, to enhance the performance of the computer system
- User-friendly
Linux operating system
Under normal circumstances, we have developed a program to run Linux操作系统
on;
therefore, the following examples are based on Linux;
Linux system is composed C语言
and 汇编语言
written;
follow the POSIX
system interface; guarantee the portability of the program;
Represents POSIX Portable Operating System Interface (Portable Operating System Interface of UNIX, abbreviated POSIX), POSIX standard defines a standard interface to the operating system must be provided by the application.
C language
C language is a product of the Unix time of writing;
Why should the C language
Computer software development process is a continuous optimization to enhance the efficiency of the process.
Unics invention is to simplify the complex task processing.
Meanwhile, in order to deal with the associated hardware and software to simplify and re-create a new language (C),
in order to achieve separation of software and hardware for the development of modern operating systems (Unix) laid a solid foundation.
C language to solve the problem
C language to solve
- Portability problems procedures; such a program may be run on different types of machines.
- With a better code readability
Bootstrap of language
Programs written in C language, by the compiler can be compiled into an executable file.
C language compiler
From the simple to the following procedure
- Pretreatment: Expand header / macro substitution / uncomment / conditions compiled file .i [preprocessor CPP]
- Compiler: converting the file into a pre-assembly language, generating a file .s [compiler egcs is]
- Assembly: A compilation becomes a target code (machine code) generated .o file [Assembler as]
- Link: Connection object code, executable program generated [Linker LD]
Pretreatment
This is a very simple C language code filename hello.c
#include <stdio.h>
int sum(int a, int b)
{
return a + b;
}
int main(int argc, char const *argv[])
{
int a = 5;
int b = 8;
int c = sum(a, b);
printf("%d \n",c);
return 0;
}
We can perform pre-activation, this document is not generated, while the output is redirected to a file which
gcc -E hello.c > hello.i
At this point we can get
# 1 "hello.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 31 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 32 "<command-line>" 2
# 1 "hello.c"
# 1 "/usr/include/stdio.h" 1 3 4
# 27 "/usr/include/stdio.h" 3 4
// 这里省去了很多引入的代码
extern void funlockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__));
# 944 "/usr/include/stdio.h" 3 4
# 2 "hello.c" 2
# 3 "hello.c"
int sum(int a, int b)
{
return a + b;
}
int main(int argc, char const *argv[])
{
int a = 5;
int b = 8;
int c = sum(a, b);
printf("%d \n",c);
return 0;
}
Compile
After pretreatment, the need for further converted to汇编代码
gcc -S hello.c
After eliminating some code
main:
.LFB1:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $32, %rsp
movl %edi, -20(%rbp)
movq %rsi, -32(%rbp)
movl $5, -4(%rbp)
movl $8, -8(%rbp)
movl -8(%rbp), %edx
movl -4(%rbp), %eax
movl %edx, %esi
movl %eax, %edi
call sum
movl %eax, -12(%rbp)
movl $0, %eax
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE1:
.size main, .-main
.ident "GCC: (Debian 6.3.0-18+deb9u1) 6.3.0 20170516"
.section .note.GNU-stack,"",@progbits
compilation
Only activate preprocessing, compilation, and assembly, that is, he only made the program obj file
compilation of documents after the process is called目标文件
gcc -c hello.c
Generates a binary file hello.o
However, this time the program was not able to run, still need to be linked;
link
Why the need to link the code after, before it can be executed by a machine?
Because the compiler will just write our own code into a binary form,
it needs and system components (such as the standard library, dynamic link libraries, etc.) together,
these components are necessary to run the program.
C standard library, is in the compiler, or operating system?
In 操作系统
the
Different operating systems offer different libraries
- It will be used to connect the linux gcc
libstdc++.so
- mac use when connecting the clang
libc++.so
Such operating system provides a number of mature functional modules known developers, and to a combination of a certain module, as a library;
developers using these connector modules incorporated in an becomes a program; they called link library;
This explains why, under different operating system installation package is not the same;
Link (Link) is actually a 打包
process,
it will target all forms of binary files and system components into a single executable file.
By linking process also requires a special software, called a linker.
With our in-depth study of the code we write more and more, they will eventually need to be dispersed to multiple source files,
the compiler can only compile a source file, an object file,
this time, in addition to the linker the object files and system components in combination, need to combine a plurality of compiler generated object files.
Is compiled for a source file, the number of source files you need to compile how many times, how many target file will be generated
Link refers to the final destination file generation可执行文件
For example: the hello.o
files and other library files, other files were added to the final generated file hello.out
in;
# -o 参数表示输出的文件名
gcc -o hello.out hello.c
Depending on the mode of the link, the link process can be divided into:
- Static links
- Dynamic Link
Static link library
.a
And .lib
it can be seen as a collection of a bunch of object files.
When one of these functions is called, put it in the object file it out, added to the connection process.
This can save a lot of time-consuming to compile, also avoid direct contact with the user library code.
Dynamic Link Library
Dynamic link library (.so and .dll) is different.
Connector After reading the dynamic link library, it will also, but does not copy the memory address space and assign the appropriate content to the executable file.
During program execution operating system DLL is loaded directly into memory, connected at run time.
In addition to the dynamic link library features can have a static link library, there are other strengths.
When multiple processes using the same dynamic link library,
the library is loaded only once and then they can be shared by these processes, not only saving space and save time.
However, without a fixed address, the efficiency of the dynamic link library may be some loss.
Program and the operating system
On a C language, we talked about 链接
is the need for 操作系统
library functions provided;
The execution of the program is also dependent on 操作系统
the resource allocation response;
Therefore, the operating system running the program can not be separated;
How the program is run on an operating system
First, we know the program is put 硬盘
in;
you do not run it, it is dead;
Only when you run it, the program is loaded into the operating system 内存
to go;
this program is considered the beginning of the run;
When running, the operating system give it a section of memory allocated for storing programs and data generated by the operation.
This memory has a start address and end address, such as from 0x1000 to 0x8000, the starting address is the smaller of the address, end address is larger that address.
And program memory
Heap
During program execution, the dynamic memory footprint request (such as new objects, or use malloc command),
the system will from among the pre-allocated memory that period, to set aside a portion of the user,
particularly from the starting address of the rule division (in fact, there will be a start address of static data, ignored here).
For example, users get 10 bytes of memory required, then from the start address 0x1000 assigned to him,
has been assigned to the address 0x100A, then if required to give 22 bytes, then assigned to 0x1020.
This because the user actively requests out of the divided memory areas, called the Heap (heap).
It starts from the start address to the upper (address) increase from the low (address).
An important feature Heap is not automatically disappear, you must manually release or may be recovered by the garbage collection mechanism.
Stack
In addition to Heap, other memory footprint is called Stack (stack). Simply put, Stack function is due to run temporary memory occupied area.
During the execution of the function;
we certainly will use to store temporary variables;
on these temporary variables on the stack;
An example to illustrate
#include <stdio.h>
int sum(int a, int b)
{
return a + b;
}
void printSum(a, b)
{
int num = sum(a,b);
printf("%d \n",num);
}
int main(int argc, char const *argv[])
{
int a = 5;
int b = 8;
printSum(a, b);
return 0;
}
Call Stack
From the main函数
start, to printSum(a,b)
encounter a function, this function into the call stack;
they met sum(a,b)
function, it is then placed in the function call stack;
After no known function, start return value, followed by the stack;
wherein the content comprises a context record function is栈帧
Procedures and processes
When a program is running, the operating system will open a process to manage the program;
Process is a program run event having individual functions on a data set.
It can apply to have the systems and resources, is a dynamic concept, it is an active entity.
It is not only the program code further comprising a current activity represented by a processing register contents and the value of the program counter
Threaded program
When our software, hardware requirements are getting higher and higher, a computer program to be run at the same time is more and more;
and CPU, during the process of switching, it will be a lot of waste of resources;
- Process is the resource owner, creation, there is a big space-time overhead of switching to withdraw, it is necessary to introduce lightweight processes;
- Because of the symmetrical multiprocessor (SMP) appears to meet the multiple operation units, while multiple processes in parallel overhead is too large.
So, in order to solve this problem: 线程
there have been
Thread (English: thread) is the smallest unit of an operating system capable of operation scheduling.
It is included in the process, the actual operation of the unit process.
A thread is in the process entity, a process can have multiple threads, a thread must have a parent process.
线程
The system does not have the resources, it must run only some of the data structure;
it is the parent's 其它线程
share owned by the process 全部资源
.
You can create threads and undo the threads in order to achieve concurrent execution of the program.
Thread has
- Ready
- Clog
- run
Reference article
C language compiler and linker Detailed
C standard library is still the operating system in the compiler
Introduction to assembly language - Ruan Yifeng