当前位置:网站首页>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 .
边栏推荐
- Source code analysis of openfeign
- ZJCTF_ login
- Duplicate disc: what are the basic attributes of an image? What do you know about images? What are the parameters of the image
- J-3-point practice in the second game of 2022 Niuke multi school
- Introduction to database - Introduction to database
- 关于使用hyperbeach出现/bin/sh: 1: packr2: not found的解决方案
- Characteristics and determination scheme of Worthington pectinase
- A new paradigm of distributed deep learning programming: Global tensor
- Member array and pointer in banyan loan C language structure
- [regular] judgment, mobile number, ID number
猜你喜欢

04.在谷歌浏览器中安装模拟浏览器ChromeDriver的详细步骤

【无标题】JDBC连接数据库读超时

Process analysis of object creation

Application, addition and deletion of B-tree

Number of 0 at the end of factorial

Spark Learning Notes (IV) -- spark core programming RDD

GetObject call timing of factorybean

若依框架代码生成详解

【安卓小叙】Kotlin多线程编程(一)

Code review pyramid
随机推荐
C language introduction practice (12): find the value of natural constant e
easyui中textbox在光标位置插入内容
Characteristics and experimental suggestions of abbkine abfluor 488 cell apoptosis detection kit
Vector to SVG method
How to optimize MySQL
Programming implementation of eight queens
Principle understanding and application of hash table and consistent hash
LeetCode 第二十七天
About the solution of using hyperbeach to appear /bin/sh: 1: packr2: not found
MySQL underlying data structure
unity之二维数组实现正六边形地图
MySQL Chinese failure
Smart pointer shared_ ptr、unique_ ptr、weak_ ptr
Will this flinkcdc monitor all tables in the database? Or the designated table? I look at the background log. It monitors all tables. If it monitors
How to conduct 360 assessment
Chapter 5 决策树和随机森林实践
Feitengtengrui d2000 won the "top ten hard core technologies" award of Digital China
03.获取网页源代码
Csu18m91 is used as the master controller of the intelligent scale scheme
飞腾腾锐 D2000 荣获数字中国“十大硬核科技”奖