当前位置:网站首页>Threads and processes
Threads and processes
2022-07-27 03:54:00 【Half moon person】
First of all, let's make a figurative analogy :
CPU-> Equivalent to a project , Running all the time .
process -> Equivalent to the workshop of the factory , His representative CPU The single task that can be handled . Anytime ,CPU Always run a process , Other processes are not running .
There will be many workers in a workshop , They worked together on a task , Threads are equivalent to workers in the workshop , A process can contain multiple processes .
There can be more than one thread in a process , They share the resources of this process . Threads are also called mini processes , But it's easier to create than a process , It's also easier to undo .
Thread is the smallest unit of operation performed in a process , It is the basic unit of operating system execution processing mechanism . Each process has at least one thread , Threads can use the resources owned by the process to schedule and calculate .
But why do we often hear about multithreading instead of multiprocessing ?
Then let's talk about the progress :
The characteristics of a thread are similar to that of a process , He basically has all the processes , such as :
- The thread has a ready , Blocking , There are three basic states of operation , It also has the transition relationship between states .
- Threads can execute concurrently ,
- In a multiple CPU Under the environment of , Multithreading can also be assigned to different CPU Execute in parallel -> make the best of CPU
advantage : Multiple threads can exist simultaneously in the same process , These threads share the resources of the process , Process built communication must request operating system services ( because CPU To switch to kernel mode ), Costly , Between threads in the same process , No operating system is required to dare , It costs less .
however , Thread communication between different processes , You must also request operating system services
Concurrency between threads is less expensive than concurrency between processes , The system has been improved
Again , It should be noted that , Operate from threads of different processes , It will cause process switching ,, So it costs a lot .
These characteristics should lead to : Whether it's creating threads , Destruction of the thread , Scheduling threads are faster than processes
But the process also has advantages and disadvantages :
When a thread in a process crashes , It will cause all threads in the process to crash .
Since the introduction of processes and threads is so similar , So let's talk about putting threads and processes together
Before introducing threads , Process is the basic unit of resource allocation and independent scheduling . After introducing threads , Process is the basic unit of resource allocation , Threads are the basic unit of independent scheduling
Process and thread comparison
- Process is the basic unit of resource allocation in the operating system , All resources related to the process , Will be recorded in the process control block PCB in , All resources owned by this process have been indicated . All threads under the same process share all resources under the process
- Thread is the basic unit of allocating processor , It has nothing to do with resource allocation . in fact . The thread running on the processor , Not the process
- A process can only belong to one process , A process can belong to multiple threads , But at least one thread .
- Threads need to cooperate and synchronize when executing , Different processes should be synchronized by means of message communication .
The relationship between threads and processes
- Process will CPU Resource allocation to thread , That is true. CPU Running threads on
- The operating system is assigned to the process , All threads of the same process share all resources of the process
The difference between a process and a thread
- Threads in the same process share the same memory space , But processes are independent .
- The data of all threads in the same process is shared ( Process communication ), Data between processes is independent .
- For threads in the same process, other threads may be affected , But one process will not affect other processes
- Thread is a context execution instruction , A process is a cluster of resources related to operations .
- Threads of the same process can communicate directly , But the communication between processes needs to be realized with the help of intermediate agents .
- It's easy to create new threads , But creating a new process requires a copy of the parent process .
- One thread can operate other threads of the same process , But a process can only operate its child processes .
- Threads start fast , The process starts slowly ( But there is no comparability between the two ).
Multi process
At the same time , If two or more processes are allowed to run in the same computer system , This is multitasking ( Multi process ). Modern operating systems are almost all multi process operating systems , It can manage the running of multiple processes at the same time . The benefits of multiple processes are clear . However, multi processes have high requirements for system resources , The waste of resources is also serious . The most commonly used multi process scenarios are windows System , For example, open and run the software at the same time , Each software opening is equivalent to running a process .
Multithreading
In a complete piece of code , There is often a need for independent code modules , These independently running program segments are called “ Threads ”(Thread), The concept of programming with multiple threads is called multithreading ( Multithreaded programming ), Multithreading is to accomplish multiple tasks synchronously , It's not to improve operational efficiency , But to improve the efficiency of resource utilization to improve the efficiency of the system . Multithreading is implemented when a program needs to complete multiple tasks at the same time . The purpose of multithreading is only to improve the efficiency of resource utilization . Each thread performs its own task , These threads can ” At the same time “. Simultaneously, not at the same time , But in a certain period of time , Complete all tasks , There is a sequence of tasks .
边栏推荐
- LeetCode 第二十八天
- Member array and pointer in banyan loan C language structure
- Cocos game practice-04-collision detection and NPC rendering
- Banyan data model of Bairong
- Machine learning [Matplotlib]
- 数字孪生实际应用:智慧城市项目建设解决方案
- Contour detection based on OpenCV (1)
- 深圳家具展首日,金可儿展位三大看点全解锁!
- Add support for @data add-on in idea
- Contour detection based on OpenCV (2)
猜你喜欢
![[tree chain dissection] template question](/img/6b/7ec6f36d5f2373aee163c2cb766b29.png)
[tree chain dissection] template question

Code review pyramid

Duplicate disc: what are the basic attributes of an image? What do you know about images? What are the parameters of the image

The new version of Alibaba Seata finally solves the idempotence, suspension and empty rollback problems of TCC mode
![[Android synopsis] kotlin multithreaded programming (I)](/img/04/4349bacbd401868d73a3b05d018b66.png)
[Android synopsis] kotlin multithreaded programming (I)

Redis spike case, learn from Shang Silicon Valley teacher in station B

Design method and test method of APP interface use case

Basic concept and essence of Architecture

飞腾腾锐 D2000 荣获数字中国“十大硬核科技”奖

MySQL underlying data structure
随机推荐
基于OpenCV的轮廓检测(2)
Realization of regular hexagon map with two-dimensional array of unity
GetObject call timing of factorybean
How can you access the domestic server and overseas server quickly with one database?
Permutation and binary (Ji, DA) (day 84)
Introduction to redis
一种分布式深度学习编程新范式:Global Tensor
flinkSQLclient创建的job,flink重启就没了,有什么办法吗?
Redis spike case, learn from Shang Silicon Valley teacher in station B
04. Detailed steps for installing the simulated browser chromedriver in Google browser
477-82(236、61、47、74、240、93)
Plato farm has a new way of playing, and the arbitrage eplato has secured super high returns
Connman introduction
飞腾腾锐 D2000 荣获数字中国“十大硬核科技”奖
Plato farm brings a new experience to community users through the LAAS protocol elephant swap
Number of square arrays (day 81)
深圳家具展首日,金可儿展位三大看点全解锁!
客户端发送一条sql如何与服务器交互
九方智投是正规公司吗?一起聊聊九方智投
Feitengtengrui d2000 won the "top ten hard core technologies" award of Digital China