当前位置:网站首页>Processes, threads, semaphores, and mutexes
Processes, threads, semaphores, and mutexes
2022-07-28 17:58:00 【A bone loving cat】
process 、 Threads 、 Semaphores and mutexes
This article is from the blog 《 A simple explanation of process and thread 》
The core of a computer is CPU, It does all the computing . It's like a factory , Running at all times .

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 .

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 .

Threads are like workers in a workshop . A process can include multiple threads .

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

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 .

A simple way to prevent others from entering , Just add a lock to the door . 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 .

And some rooms , Can accommodate at the same time n personal , For example, kitchen. . 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 .

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 " Letter Number quantity "(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 .
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 ;
(2) In the form of multithreading , Allow a single task to run in different parts ;
(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 .
边栏推荐
- 如何使用IDEA将项目上传到码云
- 【Unity】Sprite九宫格到底怎么玩?
- Leetcode systematic question brushing (V) -- dynamic programming
- LeetCode--45. 跳跃游戏Ⅱ(贪心)
- Mysql5.7 compressed package installation tutorial
- OpenMV(四)--STM32实现特征检测
- How to upload a project to the code cloud using idea
- IO的操作
- $(document).Width() in WebView is a value
- Electrotechnics Volume II self study notes 1.23
猜你喜欢
随机推荐
Point cloud processing voxel filter
2.2-数据类型
1.4-dos
Point cloud processing - KD tree
如何安装ps的滤镜插件
企业微信和视频号的关联
Tips--解决No module named matlab.engine的问题
视频号上销量较好的品类
PS fast making full screen watermark
Understanding of virtual (virtual method) in C and its difference from abstract (abstract method)
数字滤波器(三)--模拟滤波器的设计
Leetcode systematic question brushing (I) -- linked list, stack, queue, heap
[advanced C language] - analyze the storage of micro data in memory [i]
[p5.js learning notes] local variable (let) and global variable (VaR) declaration
数字滤波器(五)--设计IIR滤波器
“3·8女王节”限时大促竟让场观、视频号销量翻倍?
Flutter:异常处理
Collection集合
视频号小白起号操作指南
MySQL详解



![[p5.js learning notes] local variable (let) and global variable (VaR) declaration](/img/37/82cdf73eb6527fb2da5bc550c33223.png)





