[001 Operating system] What are threads and processes? What is the difference between thread processes?

1. What are threads and processes?

Process : A process is the basic unit of resource allocation. It is an instance of a program during execution and is created when the program is running.

In the Linux environment, each process has its own independent 4G address space, and everyone does not interfere with each other. Different processes are mapped to independent storage spaces on the physical memory through page table mapping. Under the scheduling of the operating system, they take turns occupying the CPU to run, without interfering with each other, without affecting each other, and even without knowing each other. In the eyes of each process, the CPU is its entire world. Although it is kept sleeping, once it resumes operation, it wakes up as if nothing happened, thinking that it owns the entire CPU and has been occupying it.

for example:

For the operating system, a task is a process (Process). For example, opening a browser starts a browser process, opening a notepad starts a notepad process, and opening two notepads starts two notepads. In this process, opening a Word starts a Word process.

Thread : A thread is the smallest unit of program execution, an execution flow of a process, and a process is composed of multiple threads.

In a process, there may be multiple threads, and each thread is similar to each tenant of a shared lease. Multiple threads in the same process can share resources such as code segments, data segments, and open files, but each thread Each has an independent set of registers and stacks, which ensures that the control flow of threads is relatively independent. In threads, various synchronization mechanisms of locking and unlocking can also be used to prevent conflicts between multiple threads accessing shared resources, such as mutexes, condition variables, read-write locks, etc.

for example:

Some processes do more than one thing at the same time, such as Word, which can perform typing, spell checking, printing and other things at the same time. Inside a process, if you want to do multiple things at the same time, you need to run multiple "subtasks" at the same time. We call these "subtasks" in the process threads (Thread).

Personal understanding:

When a program is executed, the operating system creates a process for it and allocates a certain amount of physical memory to the process. The MMU maps this memory to the virtual address space, that is, maps a small piece of physical memory to 0-4G The virtual address space, of which 0-3G belongs to the user space (private process), and the highest 1G belongs to the kernel space (shared between processes). Each process divides the virtual memory space into 4 segments. The code segment, data segment, bss segment, heap area and stack area all belong to the user space of the process.

The operating system can run multiple programs at the same time, and each program corresponds to a process. Different processes "alternately occupy" the CPU, which actually means that different threads in the process are alternately occupying the CPU. When a process is scheduled to run on the CPU, the operating system assigns the CPU's time slice to a thread in the process and lets it execute for a period of time. If there are multiple threads in the process, the operating system will allocate time slices to different threads in turn according to the scheduling algorithm, so as to realize the concurrent execution of multiple threads.

How does a small piece of physical space map to a 4G virtual address space?

The following figure demonstrates the mapping relationship between the virtual memory space and the physical memory space. They are associated through the page mapping table (Page Table) , where the colored parts in the virtual memory space are mapped to the corresponding colored parts in the physical memory space. The gray part in the virtual memory space indicates that there is no corresponding part in the physical memory space, that is to say, the gray part is not mapped into the physical memory space. This is also based on the guiding ideology of "on-demand mapping" , because the virtual memory space is very large, and many parts of it may not need to be accessed during a program run, so there is no need to map these parts of the virtual memory space to the physical memory space.

 Detailed explanation of process virtual memory, physical memory, shared memory_process memory_zimuya's blog-CSDN blog

Mapping relationship between virtual address and physical address_Virtual address to physical address mapping_qq_ccc's blog-CSDN blog


Second, the difference between threads and processes?

  1. What is the basic unit: a process is the basic unit of resource allocation ; a thread is the basic unit of program execution , both of which can be executed concurrently.
  2. Whether it has an independent address space: a process has its own address space, and every time a process is started, the system will allocate an address space for it; threads have nothing to do with CPU resource allocation, and multiple threads share resources in the same process and use the same address space.
  3. Composition and affiliation: A thread can only belong to one process, and threads depend on processes to exist. And a process can have multiple threads, but at least one thread.
  4. Switching overhead: The overhead of process switching is also much greater than that of thread switching .
  5. Note: As long as one thread of a multi-threaded program dies, the entire process will also die, and the death of one process will not affect the other process, because the process has its own independent address space.

Because when a process is created or canceled, the system must allocate or reclaim resources for it, such as memory space, I/O devices, and so on. Therefore, the overhead paid by the operating system will be significantly greater than the overhead when creating or destroying threads. Similarly, during process switching, the preservation of the entire current process CPU environment and the setting of the CPU environment of the newly scheduled running process are involved. However, thread switching only needs to save and set the contents of a small number of registers, and does not involve memory management operations.


3. When to use multi-process and multi-thread?

  1. When the requirements for resource management and protection are high, and the overhead and efficiency are not limited, multi-process is used.
  2. When high efficiency is required, when switching frequently, resource protection and management requirements are not very high, multi-threading is used.


Four, the four characteristics of the operating system?

1. Concurrency

2. Sharing

3. Virtual

4. Asynchronous

Among them, concurrency and sharing are the two most basic features, and the two are conditions for the existence of each other.


5. Reference content

1. Process thread (1) - basic knowledge, what is a process? What are threads? _Process Thread_Init's Blog in the Pocket-CSDN Blog

2. Finally someone explained the process and thread clearly - Zhihu

3. The concept and difference between process and thread and communication between process and thread - Tencent Cloud Developer Community - Tencent Cloud

Guess you like

Origin blog.csdn.net/qq_41709234/article/details/131865821