##Core part:CPU, memory
1.CPU internal structureControl unit:The command and control center of the entire CPU
Calculator core, performs arithmetic operations and logical operations. The arithmetic unit receives instructions from the control unit and performs actions
The place where data is temporarily stored in the CPU, includingCPU on-chip cache CacheandRegister group
#1.1.CPU cache structureThe three-level cache architecture of this machine is as follows: L1 Cache is divided into two types, instruction storage unit (storage instructions), and logical storage unit (storage logic). Theoretically, a machine can have multiple CPUs, determined by the slots. A CPU can have multiple cores, and a core can have multiple logical processors.
Registeris an internal component of the CPU and has very fast read and write speeds.
The CPU will only read data from the register. Each CPU has a unique register that cannot be accessed by other CPUs.Using registers can reduce the number of times the CPU accesses memory, thereby increasing the CPU's working speed.The closer to the CPU, the faster the reading speed
. According to Moore's Law, the CPU is developing at a rate of doubling every 18 months, while the development speed of memory and hard disk is much faster. Far from keeping up. In order tosolve the problem of mismatch between CPU computing speed and I\O speed, the CPU began to have a small amount of built-in cache Lx Cache (CPU space is limited and storage element size is limited).
Memory storage space size: Memory > L3 Cache > L2 Cache > L1 Cache > Register
CPU reading memory data process:- Memory read speed: Register > L1 Cache > L2 Cache > L3 Cache > Memory
- CacheLine
The cache is composed of the smallest storage Block ---
- consists of blocks. The cache line size is usually 64byte. The cache size of my machine L1 is 512K, which is composed of 512 * 1024/64 cache lines.
CPU can only obtain data directly from registers.Assuming that data x = 0 is in memory, its value acquisition process is as follows:Determine whether it exists in the register
If it does not exist, traverse the L1 Cache to see if it exists. There is no traversal of the L2 Cache, there is no traversal of the L2 Cache, and there is no traversal of the L3 Cache. If an intermediate process exists, the Cache line will be locked and copied to the upper level until it reaches the register. If it is not found in the Cache, it will be searched in the memory. It will first notify the memory controller to occupy the bus bandwidth, notify the memory to lock, initiate a memory read request, wait for the response, and copy the response data to the L3 Cache. Note: The entire process is locked until the CPU is unlocked.Locality principle: When the CPU accesses the storage device, whether it is accessing data or accessing instructions, they tend to gather in a continuous in the area.
There are two types of locality principles:
1.2.CPU operating security level
Kernel state
ring2
ring3User Mode
Linux and Windows only use two levels:ring0,ring3. Internal program instructions within the operating system usually run at the ring0 level. Third parties outside the operating system The program runs at the ring3 level. If a third-party program wants to call the internal functions of the operating system, because the operation security level is not enough, it must switch the CPU running state from ring3 to ring0, and then execute the system function and create a thread. Thread blocking and waking up are heavy operations. , because the CPU needs to switch operating states.
JVM thread creation is a CPU process:
Step 1: CPU switches from ring3 to ring0 to create a thread
The second step: After the creation is completed, the CPU switches from ring0 back to ring3
The third step: The thread executes the JVM program
Step 4: After the thread is executed, destroy it and switch back to ring0
Step 5: Destroy the thread and switch back to ring3
In order to ensure safe isolation and stability of program operation, the operating system has two concepts:User space
andKernel space
. Take the 4G memory space of a 32-bit operating system as an example:
Linux reserves several page frames for kernel code and data structures, and these pages will never be transferred. Out to disk (4GB memory space, 3GB available for user programs). As shown in the figure, the linear address in the green part can be referenced byuser code and kernel code(is user space). The linear addressin the yellow part can only be accessed by the kernel code(is the kernel space).
Processes and threads can only run inusermode(usermode)orkernelmode(kernelmode). User programs run in user mode, while system calls run in kernel mode.
Use a general stack (user space stack) in user mode, use a fixed size stack (kernel space stack, generally the size of a memory page) in kernel mode, that is, each process and thread actually There are two stacks, running inuser modeandkernel moderespectively.
The basic unit thread of CPU scheduling is also divided into:
##Kernel thread model (KLT):Used by Java , the kernel saves the status and context information of the thread, and thread blocking will not cause process blocking. On multiprocessor systems, multiple threads run in parallel on multiple processors. The creation, scheduling and management of threads are completed by the kernel, and the efficiency is slower than ULT and faster than process operations.
User thread model (ULT):Does not rely on the operating system core. The application provides functions to create, synchronize, schedule and manage threads to control user threads. No user mode/kernel mode switching is required, and the speed is fast. The kernel has no awareness of ULT. When a thread blocks, the process (including all its threads) blocks
Process:The smallest unit of operating system resource allocation, for example: start a Java program, the operating system will Create a Java process, which can contain multiple threads.
Thread:The smallest unit of the operating system scheduling CPU. Threads have their own attributes such as counters, stacks, and local variables, and can access shared memory variables. The CPU switches between these threads at high speed, allowing users to feel that these threads are executing at the same time (concurrency).
Thread switching up and down:Save the intermediate state of the previous thread running and execute the next thread
Serial:There is no overlap in time. The previous task is not completed, and the next task can only wait.
Parallel:There is overlap in time. , two tasks are executed at the same time without interfering with each other
Concurrency:Running two tasks interferes with each other. At the same time point, only one task is executed. Alternate execution
The above is the detailed content of What are the basic knowledge of Java's underlying operating system and concurrency?. For more information, please follow other related articles on the PHP Chinese website!