当前位置:网站首页>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与码云进行绑定
- Openpcd installation process record
- centos8使用docker安装wordpress+mysql配置文件中WORDPRESS_DB_HOST的理解
- 有一种密码学专用语言叫做ASN.1
- 1.4-dos
- [advanced C language] - analyze the storage of micro data in memory [2] (floating point storage)
- 3.2- random numbers
- Methods, functions
- Connect other computers to local MySQL
- OpenMV(三)--实时获取摄像头图片
猜你喜欢

Encapsulation, inheritance, polymorphism
![[C language note sharing] character function and string function (recommended Collection)](/img/0a/90cd617e3622d95f9bfc3e945a298a.png)
[C language note sharing] character function and string function (recommended Collection)

3D point cloud processing series - ---- PCA

Openmv (III) -- get camera pictures in real time
![[machine learning notes] regularization: ridge regression](/img/94/9d1e126554fac0713937381253f9c9.png)
[machine learning notes] regularization: ridge regression

centos8使用docker安装wordpress+mysql配置文件中WORDPRESS_DB_HOST的理解

Internal class, common class

2.2- data type

From 0 to 1: Development notes of voting applet based on cloud development

如何安装ps的滤镜插件
随机推荐
webview里面$(document).width()都是一个值
数字滤波器(二)--最小相位延时系统和全通系统
关于图片的像素、分辨率、尺寸问题的解答,以及显示器的显示大小。
The solution to the problem that the computer cannot be charged
MySQL基本查询和运算符
Leetcode systematic question brushing (3) -- binary tree, binary search
Methods, functions
On the non recursive and recursive implementation of finding the nth Fibonacci number respectively
[advanced C language] - Advanced pointer [i]
2.2- data type
如何安装ps的滤镜插件
移动端overflow失效问题
Tips--解决No module named matlab.engine的问题
Understanding of virtual (virtual method) in C and its difference from abstract (abstract method)
视频号更像是2.0版的公众号
3.2- random numbers
[p5.js] actual copy - chess board
3.2-随机数
视频号如何将公域流量将用户导入私域
How to bind idea with code cloud