Recently, I came into contact with the concept of process and thread while studying computer architecture, so I wrote this article to record it

process

define

A process is a process in which an application is pulled up and loaded into memory by the operating system and executed from start to finish. Simple to understand: open an application is a process, such as browser, wechat, netease cloud music, are a process.

attribute

  1. Is a separate unit of resource acquisition (such as memory, time slice), meaning that resources are not shared between processes
  2. Independent scheduling and distribution
  3. The system will pay a lot of resource consumption when the process is created, destroyed and switched. Generally speaking, when we switch wechat to the background and open the browser, the computer actually has to do a lot of things
  4. The process itself does not get CPU time, only its threads do

thread

define

The idea of a thread is underneath a process. Threads are the underlying entities of an application/process.

attribute

  1. A process must have at least one thread, usually referred to as the master thread
  2. Threads belonging to the same process can share resources. Note here that not everything is shared, and in fact, some areas can only be handled by one thread at a time, while the rest of the threads have to wait
  3. Multiple threads in the same process can execute concurrently

Multithreading is an application/process that executes multiple small parts simultaneously. Realize the idea of “division of labor and cooperation” to complete a job.

The relationship between processes, threads, and cpus

At any given time, only one process is running on the CPU, and the other processes are not running. Threads are the basic unit of CPU scheduling and allocation. It’s kind of like when you call a function, you have to put everything in storage, and we know that function calls are expensive, and in this case, it’s expensive to reach out to the process, to switch between different processes.

A less precise but intuitive analogy:

CPU = factory

Process = the production line of a product

Thread = worker