CN103761155A - Semaphore with quota - Google Patents

Semaphore with quota Download PDF

Info

Publication number
CN103761155A
CN103761155A CN201410043415.7A CN201410043415A CN103761155A CN 103761155 A CN103761155 A CN 103761155A CN 201410043415 A CN201410043415 A CN 201410043415A CN 103761155 A CN103761155 A CN 103761155A
Authority
CN
China
Prior art keywords
semaphore
value
quota
variable
sleep
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN201410043415.7A
Other languages
Chinese (zh)
Inventor
吴江
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Saifun Information Technology (xiamen) Co Ltd
Original Assignee
Saifun Information Technology (xiamen) Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Saifun Information Technology (xiamen) Co Ltd filed Critical Saifun Information Technology (xiamen) Co Ltd
Priority to CN201410043415.7A priority Critical patent/CN103761155A/en
Publication of CN103761155A publication Critical patent/CN103761155A/en
Pending legal-status Critical Current

Links

Landscapes

  • Power Sources (AREA)

Abstract

The invention discloses semaphore with a quota. The semaphore comprises a shaped variable, a sleep scheduling queue, a quota value and two operations (V and P). The operating process of the semaphore comprises the steps that 1, initialization is carried out, an integer value which is not a nonnegative number is given to the semaphore as the shaped variable X, and the quota value is given to the semaphore as a limiting value; 2, V is operated, the value of the variable X is increased, when the value of the variable X is increased into the quota value, a procedure for calling the operations of the semaphore is put into the sleep scheduling queue for sleep until other procedures call the operation P, and when the value of the variable X is reduced to be lower than the quota value, the procedure put into the sleep scheduling queue is reawakened; 3, P is operated, the value of the variable X is reduced, when the value of the variable X is reduced to be zero, a procedure for calling the operations of the semaphore is put into the sleep scheduling queue for sleep until other procedures call the operation V, and when the value of the variable X is larger than 1, the procedure put into the sleep scheduling queue is reawakened.

Description

A kind of semaphore with quota
Technical field
The present invention relates to semaphore, be specifically related to a kind of semaphore with quota of novelty.
Background technology
As everyone knows, in modern computer operating system, there is multiple synchronization primitive, for example semaphore, spin lock, mutual exclusion lock and conditional-variable etc.They have the scene of each self-application to be used for realizing the synchronous and mutual exclusion between multiple processes or multiple thread.Wherein, semaphore (Semaphore) is with an integer variable, and signal is provided, and to guarantee in parallel computation environment, different processes, when access shared resource, can not clash.It is a kind of method that one does not need to use busy waiting (busywaiting).The concept of semaphore, by Dutch computer scientist Ai Zigedijiesitela (EdsgerW.Dijkstra) invention, is widely used in different operating system.In system, give each process a semaphore, represent the state that each process is current, the process of controlled power can not be forced to stop in specific place, and the signal that wait can be proceeded arrives.Semaphore Mechanism has been widely used in uniprocessor and multiple processor system and computer network now.
In actual use, semaphore S possesses two kinds of operational motions, is called V(before and claims again signal ()) and P(wait ()).V operation can increase the numerical value of semaphore S, and P operation can reduce it.Its function mode is as follows:
Process 1: initialization, give the round values with its nonnegative number.
Process 2: operation P(wait ()), the value of semaphore S will be reduced.Attempt enters the process of critical section, need to first move P(wait ()).When semaphore S is kept to negative value, process can be blocked, and can not continue; When semaphore S is not negative value, process can get permission to enter critical section.
Process 3: operation V(claim again signal ()), the value of semaphore S can be increased.Exit the process of leaving critical section, will move V(and claim again signal ()).When semaphore S is not negative value, other processes that had previously been blocked, can get permission to enter critical section.
The applicable scene of current semaphore is the producer-consumer application.The producer is the process that produces message, and consumer is the process of obtaining message.Producer produces resource (message), consumer's consumption of natural resource (message), when the speed of consumer spending resource is faster than the producer, resource is reduced at 1 o'clock, and consumer's dormancy that will be scheduled, until producer produces goes out more available resource., in scene from above, can see that the scene of producer's ability much larger than consumer's ability do not considered in this synchronization primitives of semaphore meanwhile.
Summary of the invention
Therefore, for above-mentioned problem, the present invention proposes a kind of semaphore with quota, this semaphore with quota is a kind of mutation of semaphore, also be a kind of new synchronization primitives, for the process of consumption message, cannot consume fast the situation of abundant message and consider, and can solve some more challenges, thereby solve the deficiency of prior art.
In order to solve the problems of the technologies described above, the technical solution adopted in the present invention is, a kind of semaphore with quota, comprise the variable of an integer, a sleep scheduling queue and a quota value, this semaphore comprises two operations, V(signal ()) and P(wait ()), its function mode is as follows:
Process 1: initialization, give the round values of a nonnegative number of this semaphore as the variable X of integer; Meanwhile, give quota value of this semaphore as limits value;
Process 2: operation V(signal ()), the value of variable X is increased, when the value of this variable X is increased to quota value, the process of calling this semaphore operations is suspended to sleep scheduling queue and carries out dormancy, until there is other process transfer P operation, the value of this variable X is reduced and is less than quota value, reawakes the process that is suspended to sleep scheduling queue;
Process 3: operation P(wait ()), the value of variable X is reduced, when the value of this variable X reduces to 0, the process of calling this semaphore operations is suspended to sleep scheduling queue and carries out dormancy, until there is other process transfer V operation, the value of this variable X is increased and is greater than 1, reawakes the process that is suspended to sleep scheduling queue.
Preferably, described quota value is integer quota value, is the limits value of the productive capacity to the producer, if this quota value Non-precondition is defaulted as infinity.Like this, the in the situation that of acquiescence, this semaphore with quota just deteriorates to original semaphore, can be used as normal signal amount and uses.
What is called of the present invention is the one restriction to the producer with the semaphore of quota.This synchronization primitives is the same with semaphore, has the signal quantity of an integer, but in addition, the semaphore with quota of the present invention will be introduced an integer variable as quota value.Same similar to semaphore, new synchronization primitives (this semaphore with quota) also has P operation (consume operations), V operation (production operation).When V operation, signal quantity adds 1, when semaphore being done to P operation, signal quantity subtracts 1, when signal quantity is reduced to while equaling 0, the process of call signal amount P operation is suspended in sleep queue sleeps, until while having other process transfer V operation, signal quantity is greater than 1, and consumer's process of being slept is just waken up to continue and needs to process.Different from semaphore is, semaphore with quota is when producer's process transfer V operation, signal quantity is added to 1 until while equaling quota value, producer's process equally also will be suspended in sleep queue and sleep, until the P of other consumer's process operation is reduced to the value of semaphore while being less than quota value, producer's process just can be waken up and continue to carry out.
Said method is passed through in this invention, adopted a semaphore concept with quota, shared drive Inter-Process Communication not only can solve the problem that classical signal amount solves, and can also solve more challenge, as just can very simply solved with the semaphore with quota.Traditional semaphore is the present invention's's " with semaphore of quota " of proposing a special case in fact, and that is quota value infinitely-great " with the semaphore of quota ".By the process that cannot continue operation, enter dormant state, can reduce the consumption of system resource.The proposition of novelty of the present invention a kind of semaphore concept with quota, synchronization primitives to original signal amount improves, and produce a kind of instrument of more high efficiency interprocess communication, solved producer produces too many message and obtain taker and have little time the problem of processing, it has good practical value.
Embodiment
Now in conjunction with embodiment, the present invention is further described.
This synchronization primitives of semaphore of the prior art, does not consider the scene of producer's ability much larger than consumer's ability.Allow the explanation of giving one example.Carry out interprocess communication with shared drive, a slice shared drive is divided to multiple mailbox grooves position and be used for pass-along message.Each producer's process is distributed a mailbox from shared drive, and writing information, notifies the information extraction of consumer's process.At this time need a synchronization primitives to do synchronous between two processes.Semaphore is that the one of very naturally expecting is selected, if but the producer sends the speed of information much larger than consumer's process, after meaning that likely the producer divides all shared drive groove positions of luminous intensity distribution, before consumer does not also have enough time to process, the shared drive groove position that there is no processing will be reused, the loss of message will be caused like this.Tracing it to its cause, is that the producer is not limited.So the present invention proposes between a kind of new operating system process, the synchronization primitives " with the semaphore of quota " of cross-thread.
The semaphore with quota that the present invention proposes, a kind of novel semaphore in fact, it not only can solve the problem that classical signal amount solves, and can also solve more challenge, and shared drive Inter-Process Communication as above just can very simply solve with the semaphore with quota.Traditional semaphore is the present invention's's " with semaphore of quota " of proposing a special case in fact, and that is quota value infinitely-great " with the semaphore of quota ".
Concrete, a kind of semaphore with quota of the present invention, comprises the variable of an integer, a quota value (default value be infinitely great), a sleep scheduling queue, two operations (P operates and V operation).
Wherein, a value that integer variable X is exactly semaphore.
An integer quota value Quota is exactly the limits value of the productive capacity to the producer, defined by the user, if do not define default value, is exactly infinitely great (in the situation that of default value, this synchronization primitives has just been degenerated to original semaphore).
A sleep queue, this sleep queue is relevant to the scheduling queue of computer operating system, and any thread or task meet sleep condition, enter sleep queue, and temporary suspension task, abdicates the execution of CPU, enters sleep queue sleep; Until sleep condition is removed, again the task in sleep queue or process are waken up, be arranged to the state that can carry out, allow this process/task of CPU scheduled for executing.Above-mentioned sleep condition has two: a: producer thread has been added to signal quantity to equal quota value, and producer's process is suspended to sleep queue; B: consumer has been kept to 0 by semaphore, hangs consumer's thread into sleep queue.
Two operations: V operation, producer thread is done atom adding 1 to signal quantity; P operation, consumer's thread does atom to semaphore and subtracts 1.
The application that has had above element just can realize a semaphore with quota and can be applied to the various producer/consumer models has suffered.
What is called is the one restriction to the producer with the semaphore of quota.This synchronization primitives is the same with semaphore, has the signal quantity of an integer, but in addition, the semaphore with quota to introduce an integer variable as quota value.Same similar to semaphore, new synchronization primitives also has V operation (production operation), and P operates (consume operations).When V operation, signal quantity adds 1, when semaphore being done to P operation, signal quantity subtracts 1, when signal quantity is reduced to while equaling 0, the process of call signal amount P operation is suspended in sleep queue sleeps, until while having other process transfer V operation, signal quantity is greater than 1, and consumer's process of being slept is just waken up to continue and needs to process.Different from semaphore is, semaphore with quota is when producer's process transfer V operation, signal quantity is added to 1 until while equaling quota value, producer's process equally also will be suspended in sleep queue and sleep, until the P of other consumer's process operation is reduced to the value of semaphore while being less than quota value, producer's process just can be waken up and continue to carry out.
For an actual example such as: when using the business management system of bank, user need to get number.If there is no user's number of getting, the staff of bank just can not work, if there is user to get number, shows to have user to queue up, and staff needs work.It is " producer " that the user of the number of getting gets final product abstract, and staff can abstractly be " consumer ".The speed that staff processes is fast, does not have soon user to queue up, and staff quits work, until next user has got number queuing, staff starts working again.Producer consumer model in daily life that Here it is.From the above, have user's number of getting just to mean that the producer has done V operation, staff processes a user's affairs at every turn, just means consumer P operation.The number of number is thought to a variable X, and V operation causes X+1, and P operation causes X-1.When X is kept to zero, show that consumer stops consumption, once X is no longer zero, consumer starts consumption.If use traditional semaphore to solve the problems referred to above, because it has ignored the problem of producer produces ability surplus, if the speed that consumer processes is slow, the excessive velocities of producer produces, the business management system of bank just cannot continue to run well.Therefore, the present invention improves and forms a new Semaphore Mechanism existing semaphore, not only consumer's ability is limited, also the producer's ability is limited, the for example producer has a quota value 20, and when X equals 20, the producer also enters dormancy so, until the value of X is less than 20, just send out number next.As can be seen here, adopt the semaphore with quota of the present invention, can well address the above problem.
Although specifically show and introduced the present invention in conjunction with preferred embodiment; but those skilled in the art should be understood that; not departing from the spirit and scope of the present invention that appended claims limits; can make a variety of changes the present invention in the form and details, be protection scope of the present invention.

Claims (2)

1. the semaphore with quota, is characterized in that: comprise the variable of an integer, a sleep scheduling queue and a quota value, this semaphore comprises two operations, V and P, and its function mode is as follows:
Process 1: initialization, give the round values of a nonnegative number of this semaphore as the variable X of integer; Meanwhile, give quota value of this semaphore as limits value;
Process 2: operation V, the value of variable X is increased, when the value of this variable X is increased to quota value, the process of calling this semaphore operations is suspended to sleep scheduling queue and carries out dormancy, until there is other process transfer P operation, the value of this variable X is reduced and is less than quota value, reawakes the process that is suspended to sleep scheduling queue;
Process 3: operation P, the value of variable X is reduced, when the value of this variable X reduces to 0, the process of calling this semaphore operations is suspended to sleep scheduling queue and carries out dormancy, until there is other process transfer V operation, the value of this variable X is increased and is greater than 1, reawakes the process that is suspended to sleep scheduling queue.
2. the semaphore with quota according to claim 1, is characterized in that: described quota value is integer quota value.
CN201410043415.7A 2014-01-29 2014-01-29 Semaphore with quota Pending CN103761155A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201410043415.7A CN103761155A (en) 2014-01-29 2014-01-29 Semaphore with quota

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201410043415.7A CN103761155A (en) 2014-01-29 2014-01-29 Semaphore with quota

Publications (1)

Publication Number Publication Date
CN103761155A true CN103761155A (en) 2014-04-30

Family

ID=50528398

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201410043415.7A Pending CN103761155A (en) 2014-01-29 2014-01-29 Semaphore with quota

Country Status (1)

Country Link
CN (1) CN103761155A (en)

Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
EP0367701B1 (en) * 1988-10-31 1996-01-17 International Business Machines Corporation Enhanced semaphore architecture
CN102509040A (en) * 2011-10-12 2012-06-20 北京工业大学 Method for processing audit information in safe operation system
CN102609466A (en) * 2012-01-13 2012-07-25 广州从兴电子开发有限公司 Method and system for controlling shared memory

Patent Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
EP0367701B1 (en) * 1988-10-31 1996-01-17 International Business Machines Corporation Enhanced semaphore architecture
CN102509040A (en) * 2011-10-12 2012-06-20 北京工业大学 Method for processing audit information in safe operation system
CN102609466A (en) * 2012-01-13 2012-07-25 广州从兴电子开发有限公司 Method and system for controlling shared memory

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
无: "《信号量--win32 API函数》", 《HTTPS://BLOG.CSDN.NET/LUXIAOYU_SDC/ARTICLE/DETAILS/6150088》 *

Similar Documents

Publication Publication Date Title
KR101239082B1 (en) Resource management in a multicore architecture
JP6074351B2 (en) Method and apparatus for improving turbo performance for event processing
CN103761139B (en) General purpose computation virtualization implementation method based on dynamic library interception
US20160154678A1 (en) Reverting tightly coupled threads in an over-scheduled system
CN111127293A (en) Method, apparatus and computer program product for processing data
Kim et al. Utilization-aware load balancing for the energy efficient operation of the big. LITTLE processor
WO2006059543A1 (en) Scheduling method, scheduling device, and multiprocessor system
US8522254B2 (en) Programmable integrated processor blocks
CN102693162A (en) Method for process communication among multiple virtual machines on multi-core platform based on shared memory and intercore interruption
CN103346902B (en) The method and system of data acquisition scheduling
CN109597378B (en) Resource-limited hybrid task energy consumption sensing method
CN105492989A (en) Early wake-warn for clock gating control
CN106776023B (en) Task load balancing method for self-adaptive GPU unified dyeing array
CN103077039A (en) Software development platform for embedded industrial control instrument
CN106407231A (en) A data multi-thread export method and system
CN110278257A (en) A kind of method of mobilism configuration distributed type assemblies node label
CN104125165A (en) Job scheduling system and method based on heterogeneous cluster
CN109582436A (en) Fine granularity preemptive type resource scheduling system and method based on container cluster platform
CN109739332A (en) A kind of general energy consumption optimization method of multitask
CN104156316B (en) A kind of method and system of Hadoop clusters batch processing job
CN103761155A (en) Semaphore with quota
CN115904510B (en) Processing method of multi-operand instruction, graphic processor and storage medium
CN111367655B (en) Method, system and storage medium for GPU resource scheduling in cloud computing environment
CN107291559A (en) A kind of method and device of control CPU line journey and GPU thread synchronizations
CN108109104B (en) Three-level task scheduling circuit oriented to GPU (graphics processing Unit) with unified dyeing architecture

Legal Events

Date Code Title Description
C06 Publication
PB01 Publication
C10 Entry into substantive examination
SE01 Entry into force of request for substantive examination
RJ01 Rejection of invention patent application after publication
RJ01 Rejection of invention patent application after publication

Application publication date: 20140430