Thread and process are both basic concepts of operating system, but they are very abstract and not easy for us to think clearly. We have read some articles recently, and the following is a record of our understanding of them.

Basic Concepts (from Wikipedia)

  • Process (process)
A process (English: process) is a program that is already running in a computer. Processes were once the basic operating unit of time-sharing systems. In systems designed to face processes (such as early UNIX, Linux 2.4, and earlier versions), processes are the basic execution entities of programs; In thread-oriented systems (such as most modern operating systems, Linux 2.6 and later), the process itself is not the basic unit of operation, but the container for the thread. The program itself is only a description of instructions, data and their organization; the process is the actual running instance of the program (those instructions and data).Copy the code
  • thread
A thread (English: thread) is the smallest unit in which an operating system can schedule operations. In most cases, it is contained within a process and is the actual operational unit of the process. A thread refers to a single sequential flow of control in a process. Multiple threads can be concurrent in a process, each thread performing different tasks in parallel. In Unix System V and SunOS, they are also called Lightweight processes, but more commonly referred to as kernel threads. User threads are called threads, and threads are the basic unit of independent scheduling and allocation.Copy the code

From the above description, we can conclude that an operating system can have multiple processes, and a process can have multiple threads, that is, processes are actually containers for threads.

  • A table comparing threads and processes:
S.no Process Thread
1. A process is a running program on a computer; Thread is a part of the process, is the basic unit of scheduling;
2, More costs to create, destroy, switch contexts, communicate; It costs less;
3, Consume more computer resources; Consume less computer resources
4, Interprocess independence; Memory can be shared between threads
5, Processes have their own stack and address space; Threads have their own process management blocks from which they get stacks and address Spaces

However, it is still very abstract. Recently, I read an article, a blog of Aiguo people used a more vivid example to illustrate the relationship between threads and processes. I feel very interesting, and I use his example and some of my own understanding to explain. The original 🔗

2. Figures of speech

  • The process is likened to a house, a small villa, which looks like the following.

  • Process:

Multi-process is what we call the villa, the room of a piece of ground, then the process is of a piece of computer resources, is a container, there are a lot of room in the house, contains many functions, kitchen, bathroom, bedroom, etc., this is the computer resources allocated to the process, process and process between, is one of two small villa villa, It is against the law to break into one another, but a house is a passive object, he can do nothing there, he can only be lived in and used;

  • Thread:

Since the process is the thread of the container, the men of the house is the thread, if you’re single is a single thread, if you marry and have children is multi-threaded, the residents of the house for house is subordinate relationship, I bought the house, I will have the right to use the function of the house, and for the people in the house is Shared, we can use the bathroom, bedroom, balcony and so on, These areas are called shared memory.

3. Explanation of nouns

Context We often hear terms like context and context switching. How do we understand the concept of context?

My understanding of the context is the need of environment, we should play such games, an account has many characters, a mage is 46 class, a soldier is a level 100, if you choose to be a mage, corresponding to the mage gear mounts and some information about clearance is the role of context, and if you play for a while the mage, suddenly want to play a warrior, You need to switch to the warrior’s point of view, which can be interpreted as a context switch, I don’t know if the metaphor is appropriate.

Process = running program = program code + the context of the code, corresponding to the above metaphor, the role is the program code, the code can run, you can also operate the game character to move or release moves, and the equipment level of the game character is the context of the character.

❓ What is the difference between context switching for processes and threads?

  • Process: According to the above metaphor, if we need to change the role, the whole scene of the game needs to be changed, and the mount level of the equipment, all these will have an essential change, and the virtual space will also change. Compared with the metaphor of the house, we change a house, and the CPU cache mechanism will also interfere, and all the roles will be changed. Did the information saved after that become obsolete in a flash?

  • Thread: Thread switching context can be understood as the same game role, but changed a game mode, such as knife battle, I only need to bring the knife, magic battle, I only need to bring the wand, the space is still the same, the house is still the same, so the consumption will be much less.

Details of threads and processes

❓ Why concurrency?

This is a way to squeeze the CPU, so we need to understand how the CPU works. With this question in mind, I looked up some information and found something called PC(Program Counter). Here is the introduction of Wikipedia:

A Program Counter (English: Program Counter, PC) is a register in the CPU that indicates the computer's position in its Program sequence. In most processors, instruction Pointers are added as soon as the program instruction is fetched; That is to say, the destination address of the jump instruction is obtained by the operand of the jump instruction plus the address of the next instruction after the jump instruction (the unit is bit or byte, depending on the shape of the computer).Copy the code
  • The CPU runs a program in three simple steps

1. The PC pointer points to a line of code and begins to evaluate;

2, PC pointer = current pointer +1(assuming no jump mode)

3. Execute the next code, THE PC pointer is moving;

But can the CPU do calculations all the time without resting? The answer is no, because there are IO operations, such as a print statement, IO operations are very slow compared to the CPU’s computing operations, such as a plane and a walk, then the CPU needs to wait, after the IO operation is completed, the following operations, but we need to squeeze the CPU. Can you make the CPU do other things while waiting? Let the CPU do other programs, so that he does not have time to rest, alternate running different programs, this is concurrency.