Thread processes and coroutines are very important knowledge in the computer field, and they are often tested in interviews.

In this series of articles, I will break down the knowledge points one by one, and at the same time, I will try my best to simulate the important knowledge points through code.

Of course, due to the limited ability of the author, the understanding of some concepts may be biased. Welcome to discuss and communicate.

Principle of computer composition

Before I get into threading and coroutines, I think it’s worth doing a review of the basics of computer science.

Von Neumann system

The von Neumann system abstracted the computer into five core components, namely memory, arithmetic unit, controller, input device and output device. Since then, it has laid the foundation for modern computers, which have undergone tremendous changes, but are still essentially Von Neumann computers.

To be specific:

  • Memory, responsible for the storage of data. There are memory, disk, SSD, these three devices, among which memory, disk, SSD, speed, price and other features are different, each has its own role.
  • The arithmetic unit is responsible for the operation of data. Mainly CPU, graphics card. The CPU is responsible for the conventional operations, while the graphics card focuses on 3D, graphics matrix operations.
  • The controller is responsible for the overall coordination of computer operations. Mainly CPU, CPU in addition to responsible for routine operations, but also responsible for the total control of computer operation, so that the various components of the computer can be coordinated, orderly not turbulent operation.
  • The input device is responsible for receiving the input data. The main keyboard, mouse, respectively accept character input and graphics input.
  • Output equipment, responsible for the output of computer results. It is mainly the display and the sound box. The display is responsible for the output of video content and the sound box is responsible for the output of audio content.

CPU

The CPU mainly consists of three parts, the controller, the arithmetic unit and the cache.

The controller is responsible for coordinating and controlling the orderly operation of the computer components. Inside the controller, there are program counter, instruction decoder, timing generator and so on.

The ALU is the core component of the ALU, which is mainly responsible for data processing. The operations such as addition, subtraction, multiplication and division, and or not, and shift, which are usually used, all occur in the ALU.

Caching is exists within a high-speed CPU memory, in the process of CPU, CPU also need space to store the data, if every store need to deal with memory, will greatly reduce the performance of the CPU, so within the CPU, will integrate some memory, also known as register, it is greater than the memory read and write speed.

memory

In computer hardware, memory, disks, SSDS, and the internal cache of the CPU are all functionally classified as memory.

According to the different bit price and speed, memory can be divided into three levels: cache – main memory – auxiliary storage.

The typical characteristic of the cache is the high bit price, and the speed is fast, it is designed to adapt to the CPU operation speed and memory, has a very high read and write speed, the corresponding bit price is the highest, so the capacity is generally small, generally in MB as the unit.

Than main memory cache, the price is moderate, the speed is moderate, it is in order to solve the problem of the lack of CPU cache capacity, with the main memory, when the CPU in the process of operation from the cache can not find the data you need, can also be found in relatively quickly from the main memory, avoid loaded from the disk, it will take a long CPU time.

Auxiliary storage refers to mechanical hard disk, SSD and other peripheral storage devices, auxiliary storage is also called auxiliary storage, it is the largest capacity, read and write speed is the slowest, the lowest value of a kind of memory, in the process of using the computer, can put a large number of temporarily unused data stored in the auxiliary storage.

The operating system

Operating system content is more, in order to prevent the length of this article too long, I choose some key knowledge concepts to introduce.

User mode and kernel mode

User mode programs work in user space. User space refers to the storage space where user code programs and data are stored. It is a virtual space.

A program in user mode has the following characteristics:

  • Processes can only execute the user’s own code
  • The CPU runs in user code with the lowest level of privilege
  • The CPU can only access a limited amount of memory
  • The program is not allowed to access peripherals, such as disks, network cards, etc

The corresponding user mode is kernel mode. Kernel mode programs run in kernel space. Kernel space refers to the storage space where kernel code, programs and data are stored, and it is also a virtual space. A process in the user state is in kernel state when it enters kernel code execution by executing a system call.

A program in kernel mode has the following characteristics:

  • The CPU executes in the most privileged kernel code
  • The CPU has access to all memory and data
  • Programs allow access to peripherals

Concurrency and parallelism

A parallel occurs when two or more events occur at the same time interval.

Parallelism is when two or more events happen at the same time.

Synchronous and asynchronous, blocking and non-blocking

Blocking and non-blocking emphasize the state of a program while it is waiting for the result of a call, while synchronous and asynchronous describe how the program communicates. One is the state and the other is the communication mechanism.

Blocking corresponds to the state corresponding to the synchronous call. When the caller does not respond, because the caller will wait, at this time, the program can not do other work, so it is called the blocking state; The non-blocking state corresponds to the state of the asynchronous call. When the caller does not respond, the caller does not waste time waiting, but turns to do something else, and the program does not stop running. This state is called non-blocking state.

CPU intensive and IO intensive

A CPU-intensive task is a task that requires a large amount of CPU time. Logically, it is represented as a program that needs to perform complex calculations. The execution time of this task depends on the running time of the CPU.

IO intensive programs require frequent read/write operations on disks and networks. The completion speed of the programs depends on the running speed of the I/O device. The faster the disk speed or network bandwidth is, the faster the program is completed.