当前位置:网站首页>(Reprint) visual explanation of processes and threads
(Reprint) visual explanation of processes and threads
2022-06-26 00:05:00 【Falling ink painting snow】
process (process) And thread (thread) Is the basic concept of the operating system , But they're more abstract , It's not easy to master .
1.
The core of a computer is CPU, It does all the computing . It's like a factory , Running at all times . A factory may have multiple workshops to complete different tasks , For example, manufacturing workshop , Assembly workshop, etc , These different workshops correspond to many computer applications , This can be seen in the task manager .
2.
And we play games while listening to music on weekdays, which is really the use of multi processes , But actually CPU Just switch between different processes frequently , Let's feel CPU Running both programs at the same time . So back to the factory example , When there is only one workshop working in the whole factory , It is a single process .
3.
Suppose the power of the factory is limited , It can only be used in one workshop at a time . in other words , When a workshop starts , All other workshops have to be shut down . The meaning behind it is , Single CPU You can only run one task at a time , In the real computer ,CPU That's exactly what it did , The process is like the workshop of a factory , It represents CPU The single task that can be handled . Any moment ,CPU Always run a process , Other processes are not running .
In a workshop , There can be a lot of workers . They worked together on a task .
5.
Threads are like workers in a workshop . A workshop can include multiple workers , Of course, a workshop can have only one worker , For example, quality inspectors or maintenance personnel in highly automated production workshops , But a workshop cannot be without a worker on duty , Just like a process cannot exist without a thread , That is, we often say that a process can have multiple threads , A process must have at least one thread , That is, the main thread .
6.
The workshop space is shared by the workers , For example, many rooms are accessible to every worker . This symbolizes that the memory space of a process is shared , Each thread can use these shared memory , For example, pile .
7.
But , The size of each room is different , Some rooms can only hold one person at most , Such as toilet . When there are people inside , No one else can go in . This means that when a thread uses some shared memory , Other threads have to wait for it to finish , To use this memory , For example, stack .
8.
A simple way to prevent others from entering , Just add a lock to the door , This is very similar to the toilet on the train . First come lock the door , The next person sees the lock , Just line up at the door , Don't go in until the lock is open . This is called " The mutex "(Mutual exclusion, abbreviation Mutex), Prevent multiple threads from reading and writing a block of memory area at the same time .
9.
And some rooms , Can accommodate at the same time n personal , For example, kitchen. , Or more vividly, you go out to eat , A packed , You can only wait in line for a vacant seat . in other words , If the number of people is greater than n, The extra people can only wait outside . It's like some memory area , Only a fixed number of threads can be used .
10.
Solution at this time , Just hang it at the door n Key to . Those who go in take a key , Hang the key back when you get out . Those who arrived later found that the key was on the air , I knew I had to wait in line at the door . This is called " Semaphore "(Semaphore), To ensure that multiple threads do not conflict with each other .
It's not hard to see. ,mutex yes semaphore A special case of (n=1 when ). in other words , We can replace the former with the latter . however , because mutex It's simpler , And high efficiency. , So when we have to guarantee the exclusive use of resources , Or this design .
11.
The design of the operating system , So it comes down to three points :
(1) In the form of multi process , Allow multiple tasks to run at the same time , You watch movies and hang up playing games at the same time ;
(2) In the form of multithreading , Allow a single task to run in different parts , You can type and chat with others while sending files through social software ;
(3) Provide coordination mechanisms , On the one hand, it prevents conflicts between processes and threads , On the other hand, it allows resources to be shared between processes and threads .
( End ) Most of them are reprinted , A few added their own understanding , It's standing on the shoulders of giants , If the description is not in place , Please point out that .
Link to the original text : A simple explanation of process and thread
边栏推荐
猜你喜欢
iomanip头文件在实战中的作用
Establishment of multiple background blocks in botu software_ Old bear passing by_ Sina blog
支付宝支付接口沙箱环境测试以及整合到一个ssm电商项目中
One article explains R & D efficiency! Your concerns are
Installation of third-party library iGraph for social network visualization
Analyse des cinq causes profondes de l'échec du développement de produits
在win10下使用visual studio2015链接mysql数据库
ASA如何配置端口映射及PAT
【微信公众号H5】 生成带参数进入公众号关注页的二维码 监听用户关注公众号事件 自定义菜单栏 (服务端)
Unsigned and signed vernacular
随机推荐
请问可以不部署服务端实现图片上传吗?
Virtual and pure virtual destructors and their implementation in c++
Common problems encountered when creating and publishing packages using NPM
Problems encountered in Doris operation and maintenance
Using swiper to realize the rotation chart
Installation of third-party library iGraph for social network visualization
dhcp复习
[wechat official account H5] generates a QR code with parameters to enter the official account attention page to listen to user-defined menu bar for official account events (server)
详细讲解局部变量、全局变量、静态变量三种类型
利用VBScript连接mysql数据库_过路老熊_新浪博客
文献调研(一):基于集成学习和能耗模式分类的办公楼小时能耗预测
SSM整合学习笔记(主要是思路)
Let's talk about string today
Line height for small use
static关键字详解
树莓派开机发送热点进行远程登录
文献调研(四):基于case-based reasoning、ANN、PCA的建筑小时用电量预测
Find the minimum value of flipped array [Abstract bisection]
利用swiper实现轮播图
Sword finger offer 48 Longest substring without duplicate characters