当前位置:网站首页>Deadlock, thread and process explanation
Deadlock, thread and process explanation
2022-07-19 10:06:00 【Hua Weiyun】
One 、 Under what circumstances will the operating system deadlock ?
1.1 The necessary conditions for deadlock
- mutual exclusion : That is, a resource can only be occupied by one process for a period of time , Cannot be occupied by two or more processes at the same time , Such exclusive resources as CD-ROM Driver , Printers, etc , It must be released after the process occupying the resource takes the initiative , Other processes can occupy this resource , This is determined by the attributes of the resource itself .
- Conditions that cannot be preempted : The resources obtained by the process are not used up , Resource applicants cannot forcibly seize resources from occupiers , It can only be released by the owner process of the resource .
- Possession and application conditions : The process already occupies at least one resource , But he applied for new resources . Because the resource is already occupied by another process , The process is blocked at this time ; But it is waiting for new resources , Continue to occupy existing resources .
- Loop wait condition : There is a process waiting sequence {P1,P2,……,Pn}, among P1 wait for P2 Part of the resources occupied ,P2 wait for P3 The possession of a resource ,……, and Pn wait for P1 The possession of a resource , Form a process loop waiting ring .
1.2 Deadlock prevention
- Break the mutually exclusive condition : That is, allow the process to access some resources at the same time . however , Some resources are not allowed to be accessed at the same time , Such as a printer .( This is determined by the attributes of the resource itself , So this method has no practical value .)
- Break the inviolable conditions : That is, allow the process to forcibly obtain some resources from the owner . That is to say , When a process already has some resources , It applies for new resources , But not immediately satisfied , It has to release all the resources it has , Apply again later . The resources it releases can be allocated to other processes . This is equivalent to the hidden possession of the resources occupied by the process . This method of preventing deadlock is difficult to realize , Will degrade system performance .
- Break possession and apply for conditions : Can achieve Resource pre allocation strategy . That is, the process applies to the system for all the resources it needs at one time before running . If all the resources needed by a process cannot be met , No resources will be allocated , This process is not running , Only when the system can meet all the resource requirements of the current process , To allocate all the applied resources to the process at one time , Because the running process has occupied all the resources it needs , So there will be no phenomenon of occupying resources and applying for resources , So there is no deadlock . however , This strategy also has the following disadvantages :
- unpredictable : in many instances , A process cannot know all the resources it needs before execution , This is because the execution of the process is dynamic , unpredictable .
- Low resource utilization : No matter when the allocated resources are used , A process can only be executed after it occupies all the resources it needs . Even if some resources are only used once by the process , But the process has occupied them throughout its lifetime , Cause the situation of long-term occupation . This is obviously a great waste of resources .
- Process concurrency decreases : Because of limited resources , Plus there is waste , The number of processes that can allocate all the required resources is bound to be less .
- Break the cycle of waiting : implement Orderly resource allocation strategy . Use this strategy , That is, the resources are classified and numbered in advance , Assign by number , So that the process will not form a loop when applying for resources . All processes' requests for resources must be made strictly in the order of increasing resource serial numbers . Process occupation ( It should be used up ) Small resources , To apply for large resources , There will be no loops , So as to prevent deadlock . This strategy is compared with the previous , Resource utilization and system throughput have been greatly improved . But there are also some disadvantages :
- Limits the process's requests for resources , At the same time, it is also difficult to number all resources in the system reasonably , And increases the overhead of the system .
- In order to follow the order of application by number , Resources not used for the time being also need to be applied in advance , This increases the resource consumption time of the process .
1.3 Deadlock avoidance
- Avoid one thread acquiring multiple locks at the same time ;
- Avoid a thread taking up multiple resources in the lock at the same time , Try to make sure that each lock occupies only one resource ;
- Try using a time lock , Use
lock.tryLock(timeout)Instead of using the internal locking mechanism ;- For database locks , Locking and unlocking must be in a database connection , Otherwise, the unlocking will fail ;
1.4 Practical application methods
- Security sequence
- Banker Algorithm
Two 、 How to understand distributed locks ?
Distributed lock is a way to control the synchronous access of shared resources between distributed systems . In distributed systems , It's often necessary to coordinate their movements . If different systems or different hosts of the same system share one or a group of resources , So when you visit these resources , Mutual exclusion is often needed to prevent mutual interference to ensure consistency , under these circumstances , You need to use distributed locks .
3、 ... and 、 There are several ways of interprocess communication ?
Be careful : Interprocess communication is through the operating system , The communication between threads is based on processes , No need to go through the operating system .
- The Conduit : Pipes can be used for communication between processes with kinship , Allow a process to communicate with another process that has a common ancestor .
- Command pipeline : Named pipes overcome the limitation that pipes have no names , therefore , In addition to the functions of pipeline , It also allows unrelated processes to communicate , Named pipes have corresponding file names in the file system . Named pipe through command mkfifo Or system call mkfifo To create .
- The signal : Signal is a more complex way of communication , Used to notify the receiving process of an event , Except for interprocess communication , The process can also signal the process itself .
- Message queue : Message queue is the connection table of messages , Include Posix Message queue 、systern V Message queue . Processes with sufficient privileges can add messages to the queue , The process that is granted read permission can read the messages in the queue 、 Message queuing overcomes The signal carries less information , The pipeline can only carry unformatted byte streams and buffer size limitations .
- Shared memory : Enable multiple processes to access the same block of memory space , Is the fastest available IPC form . It is designed for the low efficiency of other communication mechanisms . Often with other communication mechanisms , If used in combination with semaphores , To achieve synchronization and mutual exclusion between processes .
- Memory mapping ; Memory mapping allows multiple processes to communicate , Each process using this mechanism implements it by mapping a shared file to its own process address space .
- Semaphore : Mainly as a means of synchronization between processes and between different threads of the same process .
- Socket (socket) More general interprocess communication mechanism , It can be used for process communication between different machines .
Four 、 The relationship between thread synchronization and blocking ? Must synchronization be blocked ? Does blocking have to be synchronized ?
- Thread synchronization has nothing to do with blocking .
- Synchronous and asynchronous focus on Message communication mechanism . So called synchronization , When you make a call , Before we get results , The call does not return . But once the call returns , You get the return value . Asynchrony is the opposite , After the call is made , This call directly returns , So no results returned . let me put it another way , When an asynchronous procedure call is issued , Callers don't get immediate results , But after the call is issued , The callee passes through the State , Notify the caller , Or the callback function handles the call .
- Blocking and non blocking focus on Program waiting for call result ( news , Return value ) The state of . Blocking call refers to the time between the return of call results , The current thread will be Hang up , The calling thread can only return after getting the result . A non blocking call is one that occurs before an immediate result cannot be obtained , The call will not block the current thread .
5、 ... and 、 The difference between a process and a thread
A process is an executing application , A thread is an execution sequence within a process .
- Address space and other resources : Process independence , Resources are shared among threads of the same process , Threads within a process are not visible to other processes .
- signal communication : Interprocess communication
IPC( A standard unix Communication mechanism , It can be seen that the communication between processes above ), Threads can read and write directly Process data segment ( Such as global variables ) To communicate – Process synchronization and mutual exclusion are needed , To ensure data consistency .- Switch : Threads are much faster than process switching ;
- Dispatch : A process is a running program ( For example, an application :qq、 WeChat 、 Alipay ), A thread is an execution sequence within a process ( such as :qq One of the operations in , Chat write data , Check chat history ……)
边栏推荐
- [565. Array nesting]
- 壳聚糖包裹PCN224纳米粒子|金属-有机骨架Fe-MIL-88NH2|镍基MOF材料(Ni-MOF/NF)
- 浏览器的故事
- rhcsa 第二天 7.15
- Chapter 4 - first order multi-agent system consistency - > continuous time system consistency with time delay [program code]
- Fiddler replay attack, simple simulated replay attack
- Conversion between two-dimensional array and sparse array
- What is the product power of lantu dreamer?
- Chapter 4 - first order multi-agent system consistency - > switching topology system consistency
- 光辉使用输出
猜你喜欢

Chapter 4 - consistency of first-order multi-agent systems - > consistency of continuous time systems with time delays

Rhcsa jour 2 7,15

SSH connection to Huawei modelarts notebook

金属有机骨架/碳化氮纳米片(UiO-66/HOCN)复合材料|MIL-101负载Au-Pd合金纳米粒子|化学试剂mof定制

【C语言】字符串、转义字符及注释

mof定制材料|超薄MOF纳米带|磁性Fe3O4 @Cd-MOF纳米复合材料|ZIF-8/石墨烯复合纳米颗粒

Huawei wireless devices are configured with static load balancing

TLS四次握手

软件工程——软科中国大学专业排名

Relationship between standardization, normalization and regularization
随机推荐
空天地海协同应用综述
状态码的故事
齐岳供应负载亚甲基蓝的CuMOF纳米晶|原位生长在泡沫镍上FeMOF纳米片|氧化物纳米线/ZIF系MOFs糖葫芦状复合材料
Clwy permission management (I) -- project construction
506.相对名次
Implement word segmentation for text and draw word cloud
Redis cache avalanche
pfSense配置Tailscal站点到站点连接
自己创建的模块 使用cmd打开报 ModuleNotFoundError: No module named 解决方案
[C language] string, escape character and comment
在华为ModelArts运行YOLOV3_coco_detection_dynamic_AIPP样例
D. Mark and Lightbulbs
SSH Connection Huawei modelarts Notebook
负载型金属有机骨架ZIF-8/氧化石墨烯储氢材料|二氧化钛/ZIF-8复合材料|二氧化硅@ZIF8纳米材料
【C语言】字符串、转义字符及注释
es索引、类型(mapping)、文档、ik分词器
标准化、归一化和正则化的关系
ES Restful操作
第4章-一阶多智体系统一致性 -> 领航跟随系统一致性【程序代码】
实验1:使用Matlab工具箱进行相机标定实验