当前位置:网站首页>Chapter 11 signal (I) - concept
Chapter 11 signal (I) - concept
2022-06-27 08:25:00 【yaoxin521123】
List of articles
Chapter 11 The signal ( One ) - Concept
background
Wikipedia has this definition of semaphore :“ In computer science , Especially in the operating system , A semaphore is a variable or abstract data type , It is used to control the access of multiple processes to common resources in parallel programming or multi-user environment .” Semaphores are different from mutexes ( Or lock ). Mutexes are most commonly used to manage access to a single resource by competing processes . When a resource has multiple copies of the same resource and each of these copies can be used simultaneously by a separate process , Will use semaphores .
Consider an office supply store . It may have several copiers for its customers , But each copier can only be used by one customer at a time . To control this , There is a set of keys that enable the machine and record its use . When a customer wants to copy a document , They asked the clerk for the key , Using machines , Then return the key , And pay royalties . If all the machines are in use , The customer must wait until the key is returned . Save the position of the key as a semaphore .
This example can be further extended to include different types of copiers , Perhaps they can be distinguished by the size of the copies they can make . under these circumstances , There will be multiple semaphores , If the replicator has any overlap in the size of the replication , Then customers who want to replicate a common size will have two resources to extract .
Introduce
Semaphores are shared objects , Used to provide fast... Between processes 、 Efficient communication . Each semaphore is a class %SYSTEM.Semaphore An example of . Semaphores can be modeled as a shared variable , It contains a 64 Bit nonnegative integer . An operation on a semaphore changes the value of a variable synchronously in all processes that share it . By convention , A change in value passes information between processes that share semaphores .
Although semaphores and locks seem to have a lot in common , But there are some advantages to using semaphores . One of the most important facts is , When semaphore is granted , Semaphores cause notifications to be sent . Therefore, a process using semaphores does not need to spend processor cycles or complex application logic polling the lock to check whether it has been released . Besides , The semaphore is in ECP Work transparently on the connection , And it is more efficient than the exchange required to check the lock in the same case .
Signal Overview
Signal name
Semaphores are identified by their names , These names are used as... When creating semaphores ObjectScript String provides . The names assigned to semaphores should conform to the rules of local and global variables . Semaphore names are only used to distinguish them from all other existing semaphores . Examples of valid name strings are :SlotsOpen, J(3), ^pendingRequest(""j"").
Usually , Semaphores are stored on the instance that created the semaphore , And visible to all processes on the instance . however , When the semaphore name looks like the name of a global variable , Semaphores are stored in mapped global variables ( Include Subscripts ) On the system . This allows such a semaphore pair to ECP All processes running on instances of the system are visible .
Be careful : The name of the semaphore is a string , But at run time, it can be controlled by such as "^" _ BaseName _ "(" _ (1 + 2) _ ")" Such an expression construct . If BaseName Include string “prters”, Then the semaphore name is ^prters(3).
Semaphore value
The semaphore value is stored as 63 Bit unsigned integer , Therefore, the semaphore value is always greater than or equal to zero .
maximal 63 Bit unsigned integer is 9,223,372,036,854,775,807((2**63)-1). Semaphores cannot be incremented beyond this value , Nor can it decrease below zero .
Semaphore instances and variables
Semaphores are derived from %SYSTEM.Semaphore Instance of class for . Once the semaphore is created and initialized , its OREF Usually stored in a Objectscript variable , So it can be used for other operations , Pass as a parameter , And finally deleted . Although the name of the variable containing the reference to the semaphore does not have to correspond to the name of the semaphore , But good programming habits indicate a relationship .
Like all object references to non persistent data , When the last semaphore reference is reclaimed , The underlying semaphore is also deleted .
Semaphore operation
Basics
Semaphore operations can be divided into two categories : The operations of directly operating semaphores and waiting for other processes to operate semaphores . The first group includes :
Create– Create a new semaphore instance and initialize it for useOpen—— Access and initialize existing semaphoresDelete- Make it unavailable to any process that knows the semaphoreIncrement- Adds the specified quantity to the value of the semaphoreDecrement—— If the semaphore value is zero , Then the operation waits for it to become positive . When the semaphore is positive , Subtract the decrement from the semaphore ( Or the value of the semaphore , Whichever is less ).GetValue– Returns the current value of the semaphoreSetValue– Set the current value of the semaphore to the non negative value provided
Manage multiple semaphores
The second set of operations involves managing the semaphore list as a group , And wait for each on The pending operation is completed :
AddToWaitMany– Add the given semaphore operation to the waiting listRemoveFromWaitMany– Deletes the specified semaphore operation from the wait listWaitMany– Wait for all semaphores in the wait list to complete their respective operations . This operation may time out .
Waiting queue
Each use WaitMany The process of coordinating multiple semaphores saves the semaphore decrement operation request in an internal list . The operation of the list is as follows :
- When calling
AddToWaitManyMethod to place a decrement operation in the list , The system will try to decrement at this time . If the semaphore value is non-zero , Then the decline succeeds . The amount subtracted is the smaller of the semaphore value and the requested amount . Any request larger than the semaphore value will be forgotten . - If the semaphore value is zero when the request is added to the list , Do nothing , And the request is considered pending . At some point in the future , If the target semaphore becomes non-zero , One of the processes will be selected , Its operation refers to the semaphore and performs its decrement operation . If the result of this operation is that the semaphore still has a non-zero value , The process will be repeated , Until there is no further request , Or the value of the semaphore becomes zero .
- When the process calls
WaitManyWhen the method is used , Each operation in the wait list is checked . For satisfied requests , Call the... Of the target semaphoreWaitCompleteMethod , Then delete the request from the waiting list . When it has processed all the satisfied requests , It returns the number of such requests to the caller ; If the wait timeout is exceeded , Then return to zero. . Pending and unsatisfied requests are still on the waiting list . - Because of the asynchronous nature of semaphores , Calling
WaitManyperiod , A pending request on the waiting list may be satisfied . At this timeWaitManyWhether this satisfied request is counted during the call , Or whether it will be counted in subsequent calls , It's not sure yet . - take OREF The process saved to the semaphore may also delete it . under these circumstances , What happens depends on whether the requested operation is satisfied .
- If the semaphore is the target of the satisfied operation , Then mark the request as having decremented the semaphore by zero . Unable to call
WaitCompleteMethod , Because the semaphore does not exist , However, the request is deemed to have been made inWaitManyThe returned value is satisfied . - If the request is still pending , Then just delete it from the waiting list .
- If the semaphore is the target of the satisfied operation , Then mark the request as having decremented the semaphore by zero . Unable to call
Callback
An instance of a semaphore inherits an abstract method WaitComplete, The user needs to implement this method . When processing requests that are satisfied on the waiting list ,WaitMany Call for each semaphore in the wait list WaitComplete Method , And the decreasing number of semaphores is passed as a parameter . When WaitComplete return ,WaitMany The request will be removed from the waiting list .
Other considerations
There are multiple decrement requests on the same wait list
It is not wrong to request decrement of the same semaphore multiple times in the same wait list . The added request processing method is as follows :
- If the first request is not satisfied , Add the reduction of the second request to the reduction of the first request .
- If the first request has been satisfied ( In whole or in part ), The second request is processed normally . in other words , If the semaphore value is non-zero , The decrement request is fully or partially satisfied . however , The actual quantity subtracted is added to the quantity obtained by the first request .
Calling WaitMany Before , The decrement will not be reported through the callback , So multiple requests for the same semaphore look like a combined request . This can lead to the following :
Semaphore
ASet to0.Yes
ADecrement request for4.Yes
AThe decrement request for is1; New decrement =5.Semaphore
ASet to4.Request satisfied ;
4grant .call waitmany; Waiting for completion A The report .Semaphore
ASet to1.ADecrement request for , common3individual .Request satisfied ;
1grant .Yes
ADecrement request for4.call WaitMany; WaitCompleted for A The report 1.
Semaphore
ASet to1.Yes
AThe decrement request for is3.Request satisfied ;
1grant .Yes
ADecrement request for4.Semaphore
ASet to5.Request satisfied ;
4grant .call
WaitMany;WaitCompleted for AThe report5; SemaphoreAvalue= 1.
Semaphore delete
Semaphores have no owner , And they are not referenced like object instances . Any process that can open a semaphore can delete it .
When a semaphore is deleted ,
- If there is a pending decrement for this semaphore in any waiting list , Call
WaitCompletedCallback , The decreasing value is zero . - It will be from the mapped system ( Local or remote ) Delete in .
- Any further attempts by other processes to access it will fail , And appear
<INVALID SEMAPHORE>error .
Work termination and waiting list
When the process terminates , Its waiting list is released . Remains in the waiting list but has not been by WaitMany Any satisfied decrement requests processed will be cleared . Their respective decrements are not added back to their decremented semaphores . Any unsatisfied requests in the waiting list will simply be deleted .
Semaphores and ECP
about ECP Semaphores on the system , Operations on semaphores are sorted according to the order in which requests reach the system holding the semaphore . Each operation is guaranteed to be completed before the next operation . The remote semaphore guarantees the following conditions :
- Semaphore increase and decrease will occur in
SETandKILLLater . - When a semaphore is
SET、 When increasing or decreasing ,ECPData caching and subsequent on the server SET、 Increasing or decreasing is consistent .
Because semaphores are not persistent , So in case of service interruption ,ECP Pending semaphore operations across servers on the system are unrecoverable . Due to server or network failure ECP After interruption , Semaphores on the application server will be deleted , Pending requests on the data server will also be deleted . It is the responsibility of the application to detect this and recreate the required semaphores in the correct state .
边栏推荐
- (note) Anaconda navigator flashback solution
- JS EventListener
- JS EventListener
- DataV轮播表组件dv-scroll-board宽度问题
- LVGL GUI GUIDER移植代码到STM32
- [batch dos-cmd command - summary and summary] - environment variables, path variables, search file location related instructions - set, path, where, what if there are spaces in the path parameters of
- PayPal账户遭大规模冻结!跨境卖家如何自救?
- 关于放大器失真的原因你了解多少呢?
- UE5神通--POI解决方案
- 招聘需求 视觉工程师
猜你喜欢

爬一个网页的所有导师信息

JVM常见的垃圾收集器

Matlab tips (18) matrix analysis -- entropy weight method

准备好迁移上云了?请收下这份迁移步骤清单

oracle用一条sql查出哪些数据不在某个表里

05 观察者(Observer)模式

(note) Anaconda navigator flashback solution

Redis的持久化机制

Blind survey shows that female code farmers are better than male code farmers

Reference | upgrade win11 mobile hotspot can not be opened or connected
随机推荐
Eight misunderstandings, broken one by one (final): the cloud is difficult to expand, the customization is poor, and the administrator will lose control?
[daily practice] realization of product card animation effect
Order by injection of SQL injection
Recognize the ordering of O (nlogn)
All tutor information on one page
【批处理DOS-CMD命令-汇总和小结】-cmd的内部命令和外部命令怎么区分,CMD命令和运行(win+r)命令的区别,
JS EventListener
"Short video" Linxia fire rescue detachment carries out fire safety training
2022 love analysis · panoramic report of it operation and maintenance manufacturers
[batch dos-cmd command - summary and summary] - how to distinguish the internal command and external command of CMD, and the difference between CMD command and run (win+r) command,
Helix QAC更新至2022.1版本,将持续提供高标准合规覆盖率
Read datasets iteratively with xgboost
针对直播痛点的关键技术解析——首帧秒开、清晰度、流畅度
100% understanding of 5 IO models
并发编程JUC的AQS底层源码
【c ++ primer 笔记】第4章 表达式
Closure problem
数字IC-1.9 吃透通信协议中状态机的代码编写套路
Redis的事务
No matter how good LCD and OLED display technologies are, they cannot replace this ancient display nixie tube