Recently, I have learned the multi-threading part of Java and learned about the concepts of processes and threads. Let’s introduce processes and threads below.
Table of contents
1. Process
2. Thread
3. Process vs. thread
1. Process
A process is the smallest unit of operating system resource allocation. The resources owned by a process include its own heap, stack, virtual storage space (page table), file descriptors, and other information. To understand a process from a programming perspective, you can think of it as a class or a PCB
(Process Control Block) The structure of the process control block.
A process is an abstraction of a running program by the operating system, and can be regarded as a running process of a program.
Program VS process:
· The program is an executable file on a static disk.
· The process loads the executable file into the system. Loading means putting information in memory, allocating some resources, and executing all instructions in the program.
The essence of a process: PCB (Process Control Block), similar to a class in java. Each PCB object represents a real running program, that is, a process. The PCB contains:
: Process ID, is the unique identity of the process.But it is not fixed and is dynamically allocated every time the process is started.
2. Process status:①New status (NEW) ②Ready status ③Operation status ④Blocking status ⑤Destroy status
3. Priority: Determine the execution order of the process
4. Accounting information: Ensure the fairness of process execution. It records the number of CPU calls and execution intervals, provides data support for the process scheduler, and avoids process starvation.
5. Context information: Save the execution status of this time for the next execution.
6. A set of memory: Specifies the resources that the process needs to use.
2. Thread
Threads are the smallest unit that the operating system can perform computational scheduling. It is contained in the process and is the unit that actually runs in the process. Multiple threads can be concurrent in a process, each thread performs a different task.
Although multiple processes can also implement concurrent programming, threads are more important than processesLighter。
Advantages of threads
· Creating threads is faster than creating processes
· Destroy threads faster than destroying processes
· Scheduling threads are faster than scheduling processes
3. Process vs. thread
1. The fundamental difference: a process is the smallest unit for the operating system to allocate resources, and a thread is the smallest unit for the operating system to perform operations and scheduling.
2. Different subordinate relationships: the process contains threads, and the thread belongs to the process.
3. Different overheads: the overhead of creating, destroying and switching processes is much greater than that of threads.
4. Different resources are available: each process has its own memory and resources, and threads in a process will share these memory and resources.
5. The control and influence capabilities are different: the child process cannot affect the parent process, while the child thread can affect the parent thread. If an exception occurs in the main thread, it will affect its process and child thread.
Different utilization rates: The CPU utilization of the process is lower because the context switching overhead is high, while the thread's CPU utilization is higher and the context switching speed is fast.
7. Different operators: the operator of the process is generally the operating system, and the operator of the thread is generally the programmer.