Note: the following is a personal read a lot of articles, add their own understanding, if there is wrong please correct

Windows, Linux and macOs that we are familiar with are called operating systems, while wechat, Alipay and other software that we are familiar with are called programs that run on the operating system. There may be thousands of programs running on an operating system. But we only have one operating system, so how does the operating system manage these programs?

Let’s get one thing straight:

How does the code we write actually run on a computer?

We all know, the computer can only recognize 0 or 1 this binary data, the process of code execution is also the computer reads binary instructions and analysis of the process of instruction and execution, so we have to write code for, while, if these statements, they are simply impossible for computer identified these keywords. So the language we use today is called human language, and it goes through a series of translation operations from human language to machine language.

  • Some programming languages require that all source code must be converted to binary instruction at one time in advance, that is, to generate an executable program (.exe under Windows), such as C language, C++, Golang, Pascal (Delphi), assembly, etc. This programming language is called compiled language. The conversion tool used is called a compiler.
  • Some programming languages can convert as they go along, converting whatever source code is needed without generating executable programs, such as Python, JavaScript, PHP, Shell, MATLAB, etc. Such programming languages are called interpreted languages, and the conversion tools used are called interpreters.

Stole a picture on the Internet:

The reason why the process was introduced

From the above we can see that after a wave of manipulation, human language is transformed into machine language. Then there is a problem, for example, the person who developed wechat defined a series of variables, and the person who developed Alipay also defined a series of variables. So if we don’t limit the distinction between these variables, we can have naming conflicts. In some high-level languages, namespace fields are introduced to avoid naming conflicts. So how do you differentiate in an operating system?

This is one of the reasons for introducing processes. This is often referred to as the process of allocating resources to the smallest unit. You can assign a different memory space to each process, similar to the namespace function in high-level languages.

In the previous section, we briefly referred to instructions. The process of program running is the process of reading instructions. However, when some instructions are read, such as: IO operation, after reading the instruction is resolved to be an IO operation, and then the computer executes this operation, maybe this operation has been written to disk for an hour, so within this time, the CPU has not read the instruction. At this time, the CPU is completely idle. If there is no process, it can only wait for him to finish the execution and continue to read instructions. There is no process, all my instructions are in one big pot. If I skip it, I don’t know which command I skipped and executed later (even with temporary variables, you don’t know if the one after it has been executed).

But after a process, when I met a process of IO operations or other no longer read the operation instruction, I can hang him, until he IO operation is completed or other no longer read the instruction, waiting for the next CPU idle time, in the context of the cut back to him, to continue from hang after the state continued to read. You can ensure that you don’t waste idle CPU time.

Thus it is often said that processes are the smallest unit of resource allocation

The reason for introducing threads

What is a thread? To understand this concept, you need to understand some concepts related to operating systems. Most operating systems (such as Windows and Linux) employ the preemptive scheduling mode of time slice rotation. That is, after a task is executed for a short period of time, it is forced to suspend to execute the next task. Each task is executed in turn. A short period of time for task execution is called time slice, and the state during task execution is called running state. After a period of time, the task is forced to suspend to execute the next task, and the suspended task is in the ready state waiting for the arrival of the next time slice. So that each task can be performed, because the CPU execution efficiency is very high, time is very short, to quickly switch between the various tasks, in the sense that gives a person is more tasks “to” at the same time, this is what we call the concurrent (don’t think how advanced concurrency, its implementation is very complex, but the concept is very simple, is a word: Multiple tasks simultaneously). The multi-tasking process is illustrated below

From threads and processes from an operating system perspective

The program works because the CPU is constantly fetching and executing instructions from the instruction register IR. If you are using a process directly: when the current process is not running on the CPU, it is in the ready state. At this point, his instructions need to be stored on a private stack of their own. When the previous process is suspended, executing the current process will store the previous process’s instructions to its own private stack, then write my current process’s instructions to the instruction register IR, and then point the controller PC to the place where it was suspended last time and continue reading instructions.

A program to run up and must be interpreted or compiled into machine language, if a program is a large amount of code, directly from the process of writing instruction register IR, or hang, the instruction on the instruction register IR temporary private stack to the process, it is very time-consuming, and handle some unnecessary operation. I could have kept only one instruction for the function that my current code block executes.

Here’s an example:

function a() {
  console.log('123');
}
function b() {
  console.log('456');
};
a();
Copy the code

As you can see, if the machine language is generated, there must be instructions for function A and function B, but I only execute function A, so WHEN I switch, I only need to write the instruction of A into the instruction register. This way, there are fewer operations to hold or write when context switches are performed.

Image from: Threads and processes from an operating system perspective

Thus came threads, the smallest unit of CPU scheduling.