Before you have a deep understanding and mastery of concurrency , First of all, we need to have a general understanding of the underlying operating system we are using , For us back-end developers, the two most important pieces of the operating system are CPU and Memory . So what we will focus on later is also on these two parts .
I've heard a lot about computer models , Our current computer models are based on this development .
When the computer is running , The controller reads the first instruction from memory , According to the instructions to the corresponding memory data for the corresponding operation , Write the result back to memory , Then take out the second instruction and complete the corresponding operation under the command of the controller , Repeat the above actions until the stop command is reached .
Programs are stored as data , Read instructions one by one , Automatically complete the corresponding action . It's the most basic model of a computer .
The controller is the central nervous system of the human brain , Control, coordinate and dispatch the work and operation of other components of the computer
- Arithmetic unit
As the name implies , Mainly responsible for the operation and processing of data .
Memory is used to store data , Data support has been provided when necessary .
- input device
Input and output devices are collectively referred to as peripherals , The input device is responsible for transferring data 、 Program and other original information is collected into the computer , Like a mouse 、 keyboard
- Output devices
The output device is responsible for the final display of the calculation results of the data , Such as the display screen 、 The printer 、 Projector, etc
The picture above is a more abstract concept , Corresponding to our modern computer, it is composed of the following figure
The due CPU 、 The system bus 、 Memory module etc.
So the point is to understand CPU The overall mode of operation between and memory
The control unit consists of instruction registers IR （Instruction Register）, Instruction decoder ID （Instruction Decoder）, Operating the controller OC （Operation Controller）. The control unit is an important part of the coordinated operation of the computer , It reads the user's pre compiled program instructions in turn , Stored in the instruction register , The instruction operation is determined by the instruction decoder , The operation controller sends the signal according to the determined time sequence to coordinate each unit to execute the related instruction operation .
The arithmetic unit is the core of the arithmetic unit , It can perform arithmetic operations as well as logical operations . It receives the signal from the control unit and carries out corresponding action , It's an execution unit .
The storage unit contains CPU Internal cache and registers , yes CPU Internal storage of data , The access to registers is the fastest .
CPU In order to balance the difference between direct read-write efficiency and memory, multi-level cache is set up
L1： The logic core is exclusive , It is logically divided into two parts Instruction cache 、 Data caching
L2： The physical core is exclusive , Logical core sharing
L3： Physical core sharing
Memory storage space size ： Memory >L3>L2>L1> register ;
The speed of memory is sorted ： register >L1>L2>L3> Memory ;
The cache is made up of the smallest block of storage - Cache line (cacheline) form , The cache line size is usually 64byte.
CPU The process of reading memory data
If CPU To access a variable A It starts with L1 Find variables in the cache A , If not found , To L2 In looking for , Still did not continue to look down L3 -> Memory Finally, data is read from memory , At this point, read from memory A Variable , Copy a copy to L3 in , And then make a copy to L2, Read the copy up , Last register from L1 Get the data and calculate it accordingly , Finally, the calculation results are written back to the main memory level by level .（ The entire read process will lock the corresponding cache line ）
meanwhile CPU When reading data, two rules are followed , The principle of spatial locality and The principle of time locality
The principle of spatial locality ：CPU It is considered that when reading a data, its adjacent data is likely to be read, so the adjacent data will be loaded into the cache when loading .
The principle of time locality ：CPU When you access a data, you will think that the data is likely to be accessed again in the next period of time , So the most recently accessed copy of the data is kept in the cache .
CPU There are four security levels at runtime
Its authority is decreasing step by step . Most operating systems use only ring0 and ring3 These two levels , General operating system internal program instructions need to run in ring0 Below grade , General user operations run in ring3 Level . For example, we want to create a thread CPU You need from ring3 Level first switch to ring0 Level creation thread , After successful creation, you can start from ring0 Level switches back to ring3 Level .
If you want to run some functions inside the system , Because the authority is not enough, you need to switch the security level , This operation is for CPU It's a heavy operation .
For our common operating systems ,Windows perhaps Linux Its memory also has a piece of management and partition .
Divided into user space and kernel space , This division is also for the sake of system security .
With 32 position 4 G Memory system example , Its 1 G Memory is divided into kernel space , rest 3 G Divided into user space .
Kernel space can only be accessed by system internal code , It means ,4 G The only memory available is 3 G It can be controlled by the user program . Processes and threads can only run in user mode or kernel mode , One stack is maintained in kernel space and one in user space , Don't share with each other , In other words, the thread will discard the previous stack when it switches the running mode , User calls run in user mode , System calls run in kernel mode .
There are also two thread models
- User thread model ULT
The creation, use and destruction of threads are controlled by the user , There is no need to thread User mode / Kernel state switching , The kernel has no sense of it , Fast , If a thread is blocked, the process will be blocked
- Kernel thread model KLT
The creation, use and destruction of threads are controlled by the operating system , Need to switch , It's slower than ULT, Thread blocking has no effect on process running
JVM Using the kernel thread model .
Each application is a process , Modern operating systems create a process for every program it runs , The process is OS （ operating system ） The smallest unit of allocation of resources
Thread is OS On CPU Minimum unit of scheduling , Also known as lightweight processes . A process can allocate multiple threads , Each thread has its own stack , Include local variables 、 Counter, etc , You can also access shared memory space ,CPU High speed switching among multiple threads , Let the user feel that multiple threads are running at the same time , This is concurrency . The similar concept is parallel , Parallelism is really running at the same time .
Let's talk about it briefly , Interested students can refer to the relevant information
Divided into two
- Stack instruction set architecture
- Register instruction set architecture
Java The typical stack instruction set is , With cross platform features