Process and thread)
1. Process Features
(1) The process has an initial entry point for memory units during execution, and it always has an independent memory address space during process survival;
(2)Lifetime of the processStatuses include creation, readiness, running, blocking, and death;
(3) The Running commands sent from an application process to the CPU during execution are in different forms.Process states are divided into user and core states.. In the user State, processes execute application commands, and in the core State, application processes execute operating system commands.
2. Thread Introduction Purpose: introduce threads and processes on the basis of a serial program to improve program concurrency and program running efficiency and response time.
3. Similarities: they are all executed in a specific order.Command Sequence/Have your ownExecution Control Block--> Register, status, and scheduling policy.
4. Relationships and differences
(1)The process exists in the operating system.And corresponds to what the user can see as a program or application. On the other hand,The thread exists in the process.. Therefore, a thread is also called a lightweight process ". A process includes at least one thread. Generally, this thread is called the main thread. A process starts from the execution of the main thread and creates one or more additional threads. This is called multithreading.
(2)Multiple processesAllows the computer to execute multiple tasks at a time. WhileMultiple ThreadsSo that the process can be decomposed for parallel execution. On a multi-processor computer, processes or threads can run on different processors. This makes real parallel processing possible.
(3) parallel processing is not always successful. Sometimes the thread must be synchronized. One thread may have to wait for the results of another thread, or one thread may need to exclusively access the resources being used by another thread. Synchronization is a common cause of bugs in multi-threaded applications. Sometimes the thread may eventually wait for resources that will never become available. This leads to"Deadlock".
(4) A process has an independent address space. After a process crashes, it will not affect other processes in protection mode. The thread has its own stack and local variables, but there is no separate address space between threads. If a thread dies, the whole process will die. Therefore, multi-process programs are more robust than multi-threaded programs. However, during process switching, resources are greatly consumed, Which is inefficient at merging and distributing.
5. Practical application:
(1) For example, if a Web server processes webpage access requests from different users concurrently, you can create a parent process and multiple child processes to process the requests, however, creating a process consumes a lot of system overhead and resources. Except that these different user sub-processes involve context switching during execution. Context switching is a complex process. Therefore, in order to reduce the overhead of process switching and creation, improve execution efficiency and save resources, the concept of "Thread" is introduced in the operating system.
(2) for developers, data sharing between threads is much easier than data sharing between processes. On UNIX, data sharing between processes is achieved through communication mechanisms such as pipelines, sockets, shared memory, and traffic signals. A similar mechanism is also provided in windows. To share data between threads, you only need to share global variables. Multi-process is undoubtedly more robust than multi-threaded programs, but the cost is relatively high, especially when the number of processes or threads is large.
6. Example
Example 1: A process is like a Huaihai battle, and a thread is like a battle in the Huaihai campaign.
(1) A process is just a collection of resources and has address space, registers, scheduling policies, etc. The thread is the real execution unit (using these resources). It is like the Huaihai campaign is just a collection of resources, it has resources such as the combat location, barracks, and strategies, and the battle is the real performer of this battle.
(2) The process has an independent memory unit during execution, and multiple threads share the memory (only the memory in the process can be shared ); it is like the Huai Hai Campaign has its own independent weapons and soldiers (the pingjin campaign cannot use these resources), while the Huai Hai Campaign can share these weapons and soldiers with each battle. Supplement ①: for the use of an artillery or Special Forces (temporarily expressed by cache), the object cannot be copied and cannot be separated. When a combat (thread a) uses this cache, battle B (line B) must wait in queue until the execution of battle a is completed before using this cache. If the fight of battle a is dark, battle B will not be able to wait for this cache, the subsequent battles cannot be carried out, thus forming a "deadlock ". Supplement ②: The process can share memory, but it has its own stack space and independent execution sequence, that is, the thread basically does not own system resources, only one resource (such as program counters, a set of registers and stacks) is essential for running ). The flow of troops in the iron strike barracks cannot be shared.
(3) Each Independent thread contains the entry of the program running, the sequence of execution, and the exit of the program. Because of this multithreading, multiple parts can be concurrently executed in the application. However, this does not mean that the thread can be used independently, and the thread must rely on the scheduling control of the process. It is like every battle in Huaihai can form an independent unit, they all have attack, defense, and marching actions. Because of this, multiple battles can be executed concurrently. However, a single battle must have unified scheduling control before it can win. Otherwise, it will be broken by the enemy, but the whole battle (the whole process) will crash.
(4) from the user's perspective, a process is an execution process of an application. From the core perspective of the operating system, processes represent the basic units of the memory allocated by the operating system, CPU time slice, and other resources. They are the runtime environment provided for running programs. It is like the Huaihai campaign, from our perspective, the Huai Hai Campaign is a battle execution process. From the General's perspective, the Huai Hai Campaign represents how many troops are allocated to each war zone and how long it will take to take down the hill, is to provide an execution environment for the entire battle.
Example 2: A thread refers to several channels that can be opened when your CPU is running, just as there are several lanes on the road. The original CPU can open a thread, that is to say, we can only do one thing at the same time, taking a car. Now the CPU is much more advanced. Intel's dual-thread technology is capable of driving two roads, so it is more advanced than a single lane! The process is for your system. The process is a running program. It is like a road. This road can have a channel (single-threaded process) you can also have multiple channels (multi-threaded processes ).
Example 3: In Windows 3. under X, the process is the smallest operating unit. Every time you start a program, you start a process. In Windows 3. after X, threads are the smallest unit. Each process can start multiple threads (they share the virtual space in the process, so multiple threads may operate on one piece of memory ), for example, you can open a separate thread for each downloaded file.
7. Differences between processes and programs
A program is a set of commands. It is a static file stored in the hard disk of a computer system and has no meaning of execution. A process is a dynamic entity with its own lifecycle. Generally, a process must correspond to only one program, but one program may have multiple processes or none of them. Simply put, a process is a part of a program. A process is generated when the program runs.
8. Conclusion: A thread is a part of a process and a process is a part of a program.