What is the difference between a thread and a process?

First of all, a general summary: both processes and threads are descriptions of a time period, and are descriptions of the CPU working time period.

The background is detailed below :
CPU+RAM+various resources (such as graphics card, CD-ROM, keyboard, GPS, etc. peripherals) constitute our computer, but the operation of the computer is actually a matter between the CPU and related registers and RAM.

One of the most basic facts : CPU is too fast, too fast, too fast, registers can only catch up with him, RAM and other devices hanging on each bus are completely behind. So what to do when there are multiple tasks to be performed? Take turns? Or who has higher priority? No matter what the strategy is, in a word, in the eyes of the CPU, it takes turns.

A fact that must be known : The process of executing a piece of program code to realize a function is introduced. When the CPU is obtained, the relevant resources must also be in place, that is, the graphics card, GPS, etc. must be in place, and then the CPU starts to execute. Everything except the CPU here constitutes the execution environment of the program, which is the program context we define. When the program finishes executing, or the CPU execution time allocated to it runs out, it will be switched out and wait for the next CPU luck. The last step in being switched out is to save the program context, because this is the running environment for the next time he is hit by the CPU and must be saved.

The fact that they are connected in series : As mentioned earlier, in the view of the CPU, all tasks are executed in turn one by one. The specific rotation method is: first load the context of program A, then start executing A, save the context of program A, and call in The program context of program B to be executed next, and then start executing B, saving the context of program B. . . .

========= Important things appear ========
Processes and threads come out of this background , the two nouns are just descriptions of the corresponding CPU time period, and nouns are such functions .
  • The process is the sum of the program execution time for context switching = CPU loading context + CPU execution + CPU saving context

What is the thread?
The granularity of the process is too large, and each time there must be up and down transfer, save, and transfer. If we compare a process to a piece of software running on a computer, then the execution of a piece of software cannot be executed by a single logic, and there must be multiple branches and multiple program segments, just like the realization of program A, which is actually divided into a, b , c and other blocks are combined. Then the specific execution here may become:

Program A gets CPU => CPU loads the context, starts executing the a subsection of program A, then executes the b subsection of A, and then executes the c subsection of A, and finally the CPU saves the context of A.

Here, the execution of a, b, and c shares the context of A, and the CPU does not perform context switching during execution. Here a, b, and c are threads, that is to say, threads are smaller CPU time periods that share the context of the process. At the end of this full text, another summary: both processes and threads are descriptions of a time period, and are descriptions of the CPU working time period, but the particle sizes are different.







A process is an executing instance of an application. For example, when you double-click the Microsoft Word icon, you start the running Word process. A thread is a path within an executing process. Additionally, a process can contain multiple threads. When Word is started, the operating system creates a process and starts executing the main thread of that process.
Since a process can consist of multiple threads, threads can be considered "lightweight" processes. So the essential difference between a thread and a process is the work that each is used to accomplish. Threads are used for small tasks, while processes are used for more 'heavyweight' tasks - basic execution of the application.
Another difference between a thread and a process is that threads in the same process share the same address space, while different processes do not. So threads can read and write the same data structures and variables, facilitating communication between threads. In contrast, Inter-Process Communication (IPC) is difficult and consumes more resources.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325762871&siteId=291194637