2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
The need for computing has existed widely throughout human history, and its development has generally gone through a process from general computing tools to mechanical computers to current electronic computers.
Humanity's need for computing drives us to continuously invent and improve computers. This era is the era of "electronic computers", and the trend of development is: faster, more stable, and more miniaturized.
Recommended books: A Brief History of Computers
Point 1
Most modern computers comply withVon Neumann architecture
John von Neumann (December 28, 1903 - February 8, 1957) was a Hungarian-American mathematician and computer scientist.
Scientist, physicist, and one of the most important mathematicians of the 20th century. Von Neumann is a doctor of mathematics from the University of Budapest. He is a scientific all-rounder in the fields of modern computers, game theory, nuclear weapons, and biological and chemical weapons. He is called the "Father of Modern Computers" and "Father of Game Theory" by later generations.
Next, we will use a process from scratch to build a CPU step by step. This process can help us understand the working principles of the main computer components such as CPU and memory.
Electronic Switch – Mechanical Relay
By using electronic switches, we can implement 1 bit of seemingly useless logic operations, but at least it works, doesn't it?
We will further learn how to use electronic switches to combine truly useful logic components.
The essence of vacuum tubes and transistors in the future is to perform similar tasks, but the physical principles are more complicated, so we will not take you to read them in depth.
Next, we will learn how to use electronic switches to build some useful components – gate circuits. They can implement basic logic operations on 1 bit.
ALU is the core component for arithmetic and logical operations in a computer, and is the mathematical brain of the computer. Next, we use the logic gates built above to complete our own ALU, to learn and understand its working mode, so as to serve as the cornerstone for our further understanding of the principles of modern computers.
The arithmetic unit is responsible for all digital operations in the computer, such as the four arithmetic operations, but of course it can do much more than that. Next, we will show you how to implement an 8-bit adder to cover the whole process. Other arithmetic units will not be explained.
So far, we have built an 8-bit adder from scratch. The operations of the arithmetic unit are of course much more than that. By continuing to combine logic gates, the arithmetic unit can perform addition, subtraction, multiplication, division and even more arithmetic operations, but an adder is enough for demonstration. In fact, the difficulty of making multipliers and dividers is higher than that of adders and subtractors. If you are interested, you can try to learn more.
The logic unit is mainly used to perform logical operations. The most basic operations are AND, OR, and NOT operations, but not just single-bit comparisons.
经过我们的努力,通过基本的逻辑门电路,我们⼀步步地做出了⼀个 8 位(bits) ALU,甚至比 Intel 74181 还要强大,Intel 74181 只是⼀个 4 位(bits) ALU(😀)。当然现代的计算机中的 ALU 部件非常强大,复杂度远远超过了我们的想象,32 位 甚至 64 位基本已经普及全球了。但无论如何,再复杂的ALU 也是芯片工程师像我们这样,一层又一层, 一步又一步地将其抽象出来的。ALU 是第⼀次将人类历史上的数学和逻辑学学科有机地结合起来,可以视为人类智慧发展的现代巅峰.
ALU alone is far from enough. We cannot provide storage components for ALU, so next, we use gate circuits to briefly explain the production of storage. Note that although it is not clearly shown in the figure, these storages must remain powered on, that is, these storages are volatile.
We have hidden some implementation details in the middle, and the final effect is: when the disable line is set, the input is 1, and 1 is saved; when the input is 0, 0 is saved. When the enable line is not in place, the write is invalid.
We can use the door lock to build the registers and memory we need.
The construction of memory is a little more complicated than this, but the basic principle is the same. The memory constructed in this way is called RAM (Random Access Memory), which can support O(1) time complexity to access data at any location, which is why our array index access operation is O(1)
Hardware support.
We now have ALU and storage, but this is still not enough to make our computer work. We need a component to instruct the ALU to perform operations, and this component is the control unit (CU).
Point 2
The number of boxes in the above picture can be considered as the number of cores. As shown in the figure, it is a sixteen-core CPU.
Early CPUs were all single-core. As time goes by, we have higher and higher requirements for CPU processing speed. At this time, we need to make the CPU more integrated and contain more computing units to calculate faster. This requires the computing units to be smaller (CPU manufacturing process, such as 5nm, etc.). At this time, classical mechanics is invalid and we begin to enter the field of quantum mechanics. The challenges to the process are getting bigger and bigger.
At this time, the concept of multi-core was proposed.
The CPU's computing speed is described by frequency, which can be simply understood as how many instructions can be executed in 1 second (not rigorous). For example, the speed above can be understood as the CPU calculating about 3.4 billion times per second.
The CPU frequency changes dynamically according to the amount of tasks. As the frequency increases, more power is consumed and more heat is generated. In order to protect the CPU from burning out due to overheating, the frequency will be automatically reduced.
Point 3
First, let's introduce the instructions we need.
The so-called instruction is the command that instructs the CPU to work, which mainly consists of operation code + operand.
The opcode is used to indicate what action is to be taken, and the operand is the data to be operated by this instruction, which may be a memory address or a register number, etc.
The instruction itself is also a number, stored in a certain area of memory in binary form.
The programs we write in programming languages are ultimately translated into "binary instructions executed on the CPU". Instructions are the basic units of tasks completed by the CPU.
Instructions are also machine language (binary), assembly language. Machine language and assembly language are one-to-one corresponding.
Different CPUs support different instructions at the micro level, such as x86 instructions and arm instructions, which causes compatibility issues.
The instructions that a CPU can execute can be considered to have been specified when the CPU was originally designed. The picture above lists some simplified instructions.
The 8-bit instruction can be divided into two parts.
The first 4 bits are the operation code (opcode) indicating what the instruction does.
The last 4 bits are operands (similar to parameters)
The register names AB in the table above are fictitious. The actual CPU register names are like: eax, ebx, esp, ebp...)
There is a special register in the CPU, the "Program Counter" (which is automatically set by the system after the exe is loaded into memory), which stores the memory location from which the next instruction will be executed. At the same time, as the instructions are executed, the value of the "Program Counter" will also be updated. By default, it is a self-incrementing process of +1 (instructions are executed in sequence). If a jump statement is encountered, such as (if, while, for, function call...), it will be set to a different value.
We divide the execution of instructions into three stages
1) Fetch instruction, the CPU reads the instruction content from the memory into the CPU. (There is a special register to save the read instruction)
2) Analyze the instructions, identify the purpose of the instructions, and the corresponding functions and operands
3) Execute instructions
The execution of each instruction must go through the above three steps, and completing a task requires the execution of several of the above instructions. For example, an addition process may require three of the above instructions, which seems to be very troublesome, but because the CPU calculates very quickly (it can perform more than a billion operations in 1 second), it does not take much time for the computer.
A program is a set of instructions and the data that these instructions are to process. In a narrow sense, a program is usually represented by a set of files.
Program = instructions + data to be processed by the instructions.
This is an interesting story that was circulated earlier. Of course, this story is not true. But the earliest computers really needed
Programming with 0 and 1 (Σ(っ°Д °;)っ)
The picture below shows the Altair 8800 computer, one of the earliest microcomputers. Users had to control switches to input programs into the computer bit by bit.
If computer users were required to use binary programming, everyone would go crazy, as this would be too difficult to master. Therefore, programming languages came into being.
In order to improve programming efficiency, the concept of assembly language was first created. In fact, assembly language and machine language (that is, instructions) are completely one-to-one corresponding, but with respect to the numbers 0 and 1, some symbols were invented to help humans remember and understand them, which is what we see above, such as LOAD_A, LOAD_B, etc. After the programmer completes the programming, he needs to use an assembler to translate the assembly language into machine language.
Although assembly language reduces the programmer's memory cost, it still requires the program to master all the knowledge of computer hardware. Moreover, with the increasing number of computer manufacturers, the program written once is often only applicable to one type of computer. This is far from enough, so more advanced languages were born. High-level languages shield the hardware details and allow programmers to think about their business at a higher level. Here, taking C language as an example, after the programmer completes the writing of the program, he needs to use a compiler and a linker to translate the program into assembly language, and then use the assembler to turn it into the final machine language.
With the help of encapsulation, it is becoming easier and easier for us to learn programming. However, there are pros and cons. The high degree of abstraction leads many programmers to regard computers as black boxes and completely fail to understand how their programs work. I hope that we will not become such programmers.
The Java language we use is a little more advanced than the C language, but there is not much difference in the basic abstract principles, so we will not explain it in detail for the time being.
Note: A statement in a high-level language often requires many instructions to complete.
Point 4
An operating system is a general term for a group of software that manages computer resources. Currently, common operating systems include: Windows series, Unix series, Linux series, OSX series, Android series, iOS series, Hongmeng, etc.
Different systems run different programs (due to different KPIs, the programs are not compatible)
An operating system has two basic functions:
1) Prevent hardware from being abused by space-time applications; provide KPIs to applications and allow them to call and complete different functions.
2) Provide applications with a simple and consistent mechanism to control complex and often very diverse low-level hardware devices.
An operating system mainly does the following two things
1) Manage different hardware devices. Computers can access many devices, such as barcode scanners, medical equipment, B-ultrasound machines...
2) Provide a stable operating environment for the software. Modern operating systems need to run many programs at the same time, and it is hoped that these programs will not interfere with each other. If a program has a bug, it will not affect other programs.
When each application runs on a modern operating system, the operating system provides an abstraction that it is as if only this program is running on the system and all hardware resources are being used by this program. This illusion is achieved by abstracting the concept of a process, which can be said to be one of the most important and successful concepts in computer science.
A process is an abstraction of a running program by the operating system. In other words, a process can be regarded as the running process of a program. At the same time, within the operating system, a process is the basic unit of resource allocation of the operating system.
Point 5
Processes are some applications that are being executed on our computer. (When it comes to an application, there are two states. When it is not running, it is an exe file lying on the hard disk; when it is running, the exe will be loaded into the memory, and the CPU will execute the instructions inside.)
A process is the basic unit of resource allocation in an operating system.
Point 6
Because there are many processes on the system, they need to be managed
1) Describe the various properties of the process through structures/classes (mainstream systems are implemented in C/C++)
2) Organize the data structure, connect the above multiple structures (descriptions) in series, and further perform various additions, deletions, modifications, and queries...
For example, the Linux operating system uses a structure called "PCB" to describe process information. (PCB stands for process control block). Simply put, the above multiple PCBs are linked together in a linked list. Creating a process (double-clicking exe to run the program) is equivalent to creating a PCB structure. Destroying a process is to delete the PCB from the linked list and release the PCB structure. Viewing the process list is to traverse the linked list and display the corresponding information in sequence.
PCB is a very complex structure, which contains many attributes. Let's learn some key information below.
1.PID process identifier
At the same time, among multiple processes on a machine, PID is unique and will not be repeated. Many operations within the system are to find the corresponding process through PID.
2. Memory pointer (a group)
Describes where process dependent instructions and data are stored
The operating system, when running the exe, will read the instructions and data in the exe and load them into the memory. (Memory address)
This shows that the execution of the process requires certain memory resources.
3. File description table (sequential table/array)
Describes which files the process has opened, corresponding to the data on the hard disk
When a file is opened in the process, an item will be added to the sequence table for easy reading
The following items jointly determine the scheduling of the process, which is closely related to our daily development
Point 7
The key to operating system process schedulingTime Division Multiplexing
Current operating systems are all "multi-tasking systems" that can run multiple processes at the same time. Previous operating systems were called "single-tasking systems" and could only run one process at a time.
Understanding of time-sharing reuse: At a certain moment, the CPU runs process 1, runs for a while, runs process 2, and runs process 3 after a while... Because the CPU operation speed and switching speed are very fast, it is not noticeable to the naked eye. From a human perspective, it is equivalent to executing at the same time, "concurrent execution".
Parallel execution: Now with multi-core CPUs, each core can execute different processes simultaneously at the micro level.
Concurrency or parallelism is uniformly scheduled by the operating system kernel, and programmers/ordinary users cannot perceive it. Therefore, parallelism and concurrency are usually referred to as "concurrency", and the corresponding programming method is also called "concurrent programming".
4. Process Status
Ready state: The process can be scheduled to the CPU to execute instructions at any time
Blocked state: The process cannot be scheduled to the CPU to execute instructions. The reason for the blockage is that some other operations need to be performed, such as IO operations (reading and writing hard disks or reading and writing network cards, such as scan input, when the user needs to input, the process enters the blocked state)
The above two are the main process states, there are other states as well, I will not go into details...
5. Process Priority
Priority means literally, the order of processes
6. Process context
Time-sharing multiplexing: a process will be transferred from the CPU after a while of execution, and will be dispatched back to the CPU after a period of time, and will continue to execute according to the results of the last execution. The intermediate results of the previous execution (the values in various CPU registers) are saved for next use.
7. Accounting information of the process
With the priority, different processes may get more and more different resources...
The above attributes are used to support concurrent execution scheduling process
The operating system allocates memory resources in a spatial mode - different processes use different areas of memory and do not interfere with each other.
As mentioned above, a process is the smallest unit for resource allocation in the operating system, which means that processes cannot sense each other's existence. This is the original intention of the operating system to abstract the concept of process, which brings about "isolation" between processes.
However, modern applications often cannot complete a complex business requirement through a single process. They always need to cooperate with other processes to achieve the purpose of the application. Therefore, there is a need for "information exchange" between processes. The need for inter-process communication comes into being.
Currently, there is only one main way of inter-process communication in Java, through the network (socket)
The network is a relatively special IPC mechanism. In addition to supporting communication between two processes on the same host, it also supports communication between processes on different hosts within the same network.