当前位置:网站首页>Multithreading (I) processes and threads
Multithreading (I) processes and threads
2022-07-05 18:05:00 【Worry free*】
Catalog
2、 How the operating system manages processes
3、 ... and 、 Similarities and differences between process and thread
One 、 process
1、 What is a process
A process is an abstraction of the operating system for a running program , In other words , You can think of a process as a running process of a program .
In the file , We can often see some suffixes as .exe file , These documents are called " Executable file ". Once such documents are executed , The operating system will load the file into memory ,CPU Will execute some instructions inside this file , At this point, these running applications are processes .
2、 How the operating system manages processes
(1) Describe a process
Specify some related attributes of a process , Mainly through the use of the operating system c/c++ Of " Structure " To describe ( In the operating system, it is called process control block PCB).
(2) Several processes
Typical implementation : Use a two-way linked list to put the PCB String together , It is convenient to add, delete, check and modify
among ," Create a process " Is to create PCB, Then take it. PCB Add to the two-way linked list ;" Destruction process " Is to find the... On the linked list PCB, Delete from the linked list ;" View Task Manager " Is to traverse the linked list .
(3) Interprocess communication
Processes are independent of each other , But it can also interact through some means . The operating system provides many " public space "( Generally file operation and network operation ), process A Put data in public space , The process then B Then take it away , This is interprocess communication .
(4)PCB Some properties in
① pid: It's the process id, Process identification id.
② Memory pointer : Indicates the code to be executed by the process / Where are the instructions in memory , And where the data that the process depends on (.exe The operating system will put .exe Load into memory , Become a process ).
③ Document descriptor table : As long as every file opened by the process , An item will be added to the file descriptor table . The file descriptor table can be treated as an array , Each of these elements is a structure . A process just starts , The system will automatically open the standard input , standard output , Standard error three files .
④ state : Describes how the current process should be scheduled next .
Ready state : Can be executed at any time .
Blocked state : You can't go to... For the time being CPU On the implementation .
⑤ priority : When we have many tasks , Which task to allocate time to first , Which task will you assign time to ; Which task has more time , Which task has less time , That's the priority .
⑥ Billing information : Count how long each process has been executed 、 What instructions were executed 、 How long did you wait in line . Provide guidance for process scheduling .
⑦ Context : Indicates that the last time the process was scheduled CPU The state of , In the next process CPU You can return to your previous state when you , Then continue to execute . Similar to archiving + Reading .
The process is called out CPU Before , First of all CPU The data in all registers in is saved to memory , Equivalent to archiving
The next time the process is scheduled CPU When , It can be recovered from memory to register , Equivalent to reading files
(5) Process scheduling
Process scheduling : The process scheduler is the real core of the operating system , It is directly responsible for CPU The distribution of . All processes in the system are in CPU Running on , Process scheduling is their switch
Two 、 Threads
1、 What is thread
A thread is an internal part of a process , The process contains threads . If you think of the process as a factory , So thread is the production line inside the factory . A factory can have a production line , There can also be multiple production lines .
2、 Why have threads
Because the system supports multitasking , So we need to do " Concurrent programming ".
Although concurrent programming can also be achieved through multiple processes , But frequent creation / The cost of the destruction process is very high , And the cost of frequent scheduling process is also very high ( Because the creation process requires the application of resources , The destruction process requires the release of resources , This in itself is very inefficient )
resolvent : Using threads to implement concurrent programming . Threads are lighter , Each thread performs a task , You can also program concurrently . establish 、 The destruction 、 The cost of scheduling threads is lower than that of processes ( The process needs to apply for the release of resources , Threads are included in the process , Share the same resource with other threads , Re create threads without reallocating resources . But it's not that the more threads, the better , When there are few tasks , Multithreading will reduce efficiency )
3、 ... and 、 Similarities and differences between process and thread
1、 Processes contain threads . At least one thread per process exists , The main thread .
2、 There is no shared memory space between processes . Threads of the same process share the same memory space .
3、 Operating system creation process , To allocate resources to a process , Process is the basic unit of resource allocation in the operating system ;
Thread creation operating system , Is to be in CPU Scheduled execution on , Thread is the basic unit of operating system scheduling thread execution .
4、 The process is independent . Each process has its own virtual address space , There is a problem with a process , No impact on other processes . Multiple threads in the same process use the same memory space , If a thread has a problem , It is likely to affect other threads , It may also directly lead to problems in the whole process .
边栏推荐
- Delete some elements in the array
- Cmake tutorial Step3 (requirements for adding libraries)
- mybash
- Use of print function in MATLAB
- tkinter窗口预加载
- Maximum artificial island [how to make all nodes of a connected component record the total number of nodes? + number the connected component]
- Eliminate the writing of 'if () else{}'
- 【PaddleClas】常用命令
- EPM related
- Sentinel flow guard
猜你喜欢

Cmake tutorial Step2 (add Library)

Tencent music launched its new product "quyimai", which provides music commercial copyright authorization

About Estimation with Cross-Validation

IDC report: Tencent cloud database ranks top 2 in the relational database market!

Sophon base 3.1 launched mlops function to provide wings for the operation of enterprise AI capabilities

第十届全球云计算大会 | 华云数据荣获“2013-2022十周年特别贡献奖”

Anaconda中配置PyTorch环境——win10系统(小白包会)

Cmake tutorial step1 (basic starting point)

Sophon CE社区版上线,免费Get轻量易用、高效智能的数据分析工具

Can communication of nano
随机推荐
Ten top automation and orchestration tools
华夏基金:基金行业数字化转型实践成果分享
热通孔的有效放置如何改善PCB设计中的热管理?
第十届全球云计算大会 | 华云数据荣获“2013-2022十周年特别贡献奖”
Introduction to Resampling
Matlab reference
nacos -分布式事务-Seata** linux安装jdk ,mysql5.7启动nacos配置ideal 调用接口配合 (保姆级细节教程)
[paddleclas] common commands
在一台服务器上部署多个EasyCVR出现报错“Press any to exit”,如何解决?
「运维有小邓」用于云应用程序的单点登录解决方案
Access the database and use redis as the cache of MySQL (a combination of redis and MySQL)
删除数组中的某几个元素
Redis基础
Sophon KG升级3.1:打破数据间壁垒,解放企业生产力
集群部署如何解决海量视频接入与大并发需求?
pytorch yolov5 训练自定义数据
mybash
Sophon kg upgrade 3.1: break down barriers between data and liberate enterprise productivity
Cmake tutorial Step3 (requirements for adding libraries)
图像分类,看我就够啦!