CN106681836A - Creating method and device of signal quantity - Google Patents
Creating method and device of signal quantity Download PDFInfo
- Publication number
- CN106681836A CN106681836A CN201611239233.2A CN201611239233A CN106681836A CN 106681836 A CN106681836 A CN 106681836A CN 201611239233 A CN201611239233 A CN 201611239233A CN 106681836 A CN106681836 A CN 106681836A
- Authority
- CN
- China
- Prior art keywords
- thread
- echo signal
- signal amount
- semaphore
- value
- 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.)
- Granted
Links
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
- G06F9/46—Multiprogramming arrangements
- G06F9/50—Allocation of resources, e.g. of the central processing unit [CPU]
- G06F9/5005—Allocation of resources, e.g. of the central processing unit [CPU] to service a request
- G06F9/5011—Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resources being hardware resources other than CPUs, Servers and Terminals
Abstract
The embodiment of the invention provides a creating method and device of a signal quantity. The method is executed by a processor in a computer, the computer further comprises a memory, and multiple signal quantities capable of being visited by a user mode thread or a kernel mode thread are stored in the memory. When the thread visits data, according to a target name of a signal quantity of data to be visited by the thread, whether the memory contains the signal quantity with the same name as the target name or not is queried, if the memory does not contains the signal quantity with the same name as the target name, it shows that the memory has no signal quantity of the data to be visited by the thread, so a target signal quantity with a name being void is chosen, and the name of the target signal quantity is defined as the target name so as to create the target signal quantity of data to be visited. The target signal quantity is the signal quantity which is visible to both the user mode thread and the kernel mode thread so as to avoid that the two kinds of threads conduct concurrent access on the data and cause conflicts.
Description
Technical field
The present embodiments relate to field of computer technology, more particularly to a kind of creation method and device of semaphore.
Background technology
The Semaphore Mechanism that multi-thread concurrent is accessed is divided into two classes, and a class is the semaphore synchronization mechanism for adopting User space,
The semaphore of multiple User space thread competition application User spaces, it is ensured that order when multiple User space threads concurrently access data is visited
Ask.Another kind of is using the semaphore synchronization mechanism of kernel state, the semaphore of multiple kernel state thread application kernel states, it is ensured that many
Individual kernel state thread concurrently accesses sequential access during data.
The semaphore of application User space needs across state operation, that is, the operate interface for needing call operation system to provide will be used
The running environment of family state thread is revised as after kernel state from User space, and the signal of User space is applied in the running environment of kernel state
Amount, realizes the access of data.If kernel state thread accesses the data, running environment of the kernel state thread in kernel state simultaneously
In, apply for the semaphore of kernel state, realize the access to the data.Due to kernel state semaphore and User space semaphore all
The data can be identified, operation of the above-mentioned two semaphore in kernel state running environment is independent, therefore no matter kernel state
Thread or User space thread for semaphore operation for other side be sightless.So, the thread pair of both types
The concurrently access of data will be clashed.
The content of the invention
The invention discloses the creation method and device of a kind of semaphore, so that create can be by User space thread and kernel state line
The semaphore that Cheng Gongtong is accessed, to avoid the User space thread and kernel state thread from clashing when concurrently accessing data.
First aspect provides a kind of creation method of semaphore, the method by computer computing device.The calculating
Machine also include memorizer, one or more semaphores are preserved in the memorizer, each semaphore have unique title or
Entitled sky, each semaphore preserved in the memorizer is can be by User space thread or the signal of kernel state thread accesses
Amount.When first thread data to be visited, the first thread can be User space thread, or kernel state thread.Institute
State comprising the concrete steps that for first thread described in computing device:The target designation of the semaphore of data to be visited is obtained, according to institute
Target designation is stated, whether the memorizer is inquired about comprising title and the target designation identical semaphore.When the memorizer
During not comprising title with the target designation identical semaphore, show that memorizer is not intended for User space thread and kernel state thread
The semaphore of the data is accessed jointly.So, processor selects an entitled empty echo signal amount, by target letter
Number amount name definition be the target designation, to create the echo signal amount of data to be visited.
In this application, User space thread or during kernel state thread accesses data, can be according to data to be visited
Semaphore target designation, whether comprising can jointly be accessed by the User space thread and kernel state thread in inquiry computer
Semaphore.It is the target designation by its name definition if it is not, selecting an entitled empty echo signal amount.
Thus a semaphore that can be jointly accessed by the User space thread and the kernel state thread is created, to avoid the user
State thread and kernel state thread are clashed when concurrently accessing data.
In the first implementation of first aspect, the target designation is pre-defined in the function for accessing data
, when the first thread needs to access data, the function of data is accessed described in computing device, according to the letter for accessing data
Whether the target designation defined in number, consults memory includes and the target designation identical semaphore.
With reference to the first implementation of first aspect or first aspect, in second implementation, create described
After echo signal amount, before User space thread or data described in kernel state thread accesses, the application control data are needed
Echo signal amount.Specifically, its application way is to inquire about the state parameter of the echo signal amount, the value of the state parameter
Represent the echo signal amount in idle condition or seizure condition.When the value of the state parameter represents the echo signal
When amount is in idle condition, show that the echo signal amount is not taken by other threads, so first thread can access this
The data of echo signal amount control.
With reference to second implementation of first aspect, in the third implementation, when according to the state parameter
Value inquires the echo signal amount in idle condition, changes the value of the state parameter, the amended state parameter
Value represent that the echo signal amount is in seizure condition, it is therefore an objective to proving to other threads of competition application echo signal amount should
Echo signal amount is currently at the state taken by the first thread, and other threads can not apply for the echo signal amount, to protect
The thread of card competition application echo signal amount applies in an orderly manner echo signal amount.
With reference to the third implementation of first aspect, in the 4th kind of implementation, the first thread creates this
After echo signal amount, if the second thread is also required to access the data, before competition application echo signal amount, need to look into
Look for echo signal amount.Specifically, mode is searched to obtain the target designation, according to the target designation, inquire about the storage
Whether device is comprising title and the target designation identical semaphore.If finding the memorizer comprising title and the mesh
Entitling claims identical semaphore, then the echo signal amount can be the second thread accesses for creating the echo signal amount.The
Two threads are the one of which of the thread of described two types, and different from the type of the first thread.When first thread is
During User space thread, the second thread is kernel state thread, and when first thread is kernel state thread, the second thread is User space line
Journey, realizes the application of kernel state thread and User space thread for echo signal amount etc. and operates for other side is visible.Such as
Fruit finds the memorizer and includes title and the target designation identical semaphore, and the title of the echo signal amount
For the target designation when, second thread can with the first thread competition apply the echo signal amount.Specifically, institute
The method for stating the second thread application echo signal amount is the inquiry state parameter.The value for determining the state parameter represents described
When echo signal amount is in seizure condition, show that the echo signal amount is currently at occupied state, the second thread is unable to Shen
Please the echo signal amount, therefore the second thread stops accessing the data.By inquiring about state parameter, the echo signal amount is determined
Current state, so echo signal amount is by first thread application success or not, for the second of competition application echo signal amount
Thread is visible, and then avoids User space thread and kernel state thread from clashing when concurrently accessing data.
With reference to any one implementation in second to the 4th kind implementation of first aspect, the 5th kind of realization side
The processes such as in formula, before inquiry state parameter, other threads or computer to prevent participation competition interrupt, exception are believed target
The value of state parameter of number amount is modified, and to echo signal amount locking protection is carried out.Locking mode is the inquiry target letter
Number amount lock Protection parameters;The value of the lock Protection parameters represents the echo signal amount in locking state or empty lock shape
State.When the value of the lock Protection parameters represents the echo signal amount in empty lock status, the lock Protection parameters are changed
Value;The value of the amended lock Protection parameters represents that the echo signal amount is in locking state.After locking protection, then look into
Ask the state parameter of echo signal amount, it is ensured that the thread of currently employed locking protection can inquire about the state ginseng of echo signal amount
Number.
With reference to first aspect or first aspect the first to any one implementation in the 5th kind of implementation,
In 6th kind of implementation, after creating echo signal amount, confirm to need to apply for target by the reference number of times of echo signal amount
The quantity of the thread of semaphore.If without thread application echo signal amount, closing echo signal amount.Specifically, creating
After echo signal amount, and before inquiry state parameter, default grain is carried out to the initial value of the reference number of times of echo signal amount
The plus coujnt of degree.After accessing data, the subtraction count of equal presetting granularity is carried out to quoting number of times, then inquire about the mesh
Whether the reference number of times of mark semaphore is initial value, if initial value, then initializes the echo signal amount.Initialization is described
The process of echo signal amount is that the title of echo signal amount is entered as into sky, changes the value of state parameter, amended state ginseng
Several values represents that semaphore is in idle condition.If adopting locking protection to echo signal amount, also need to change the target letter
The value of the lock Protection parameters of number amount, the value of amended lock Protection parameters represents semaphore in empty lock status.
Second aspect provides a kind of creating device of semaphore, and the creating device is located in computer, described to create dress
The function for realizing the middle computing device of the method for first aspect or any one implementation of first aspect is put, it is described
Function can be realized by hardware, it is also possible to performed corresponding software by hardware and realized.The hardware or software include one
Or multiple modules corresponding with above-mentioned functions.
The third aspect, the embodiment of the present invention provides a kind of computer, including memorizer and processor, and the memorizer is used
In one or more semaphores are preserved, each semaphore has unique title or entitled sky, and described each semaphore is energy
By the semaphore of two kinds of thread accesses, the thread of described two types includes User space thread and kernel state thread.Institute
The method that any one implementation that processor is stated for performing first aspect or first aspect is provided.
Description of the drawings
In order to be illustrated more clearly that the technical scheme of the embodiment of the present invention, below will be to use needed for embodiment description
Accompanying drawing be briefly described, it should be apparent that, drawings in the following description are only some embodiments of the present invention, for this
For the those of ordinary skill of field, on the premise of not paying creative work, can be with other according to these accompanying drawings acquisitions
Accompanying drawing.
Fig. 1 is the Organization Chart of computer provided in an embodiment of the present invention;
Fig. 2 is running environment schematic diagram when User space process provided in an embodiment of the present invention is run;
Fig. 3 accesses the method flow diagram of data 124 for User space thread L1 disclosed in the embodiment of the present invention;
Fig. 4 is the method flow diagram that kernel state thread L2 provided in an embodiment of the present invention accesses data 124;
Fig. 5 is a kind of structure chart of the creating device of semaphore provided in an embodiment of the present invention.
Specific embodiment
Fig. 1 is referred to, Fig. 1 is the Organization Chart of computer provided in an embodiment of the present invention.As shown in figure 1, computer includes
Processor 110, memorizer 120 and communication bus 130.Processor 110 is communicated with memorizer 120 by communication bus 130.
Memorizer 120 is used for storage program 121, and program 121 includes multiple application programs 122 and operating system (English:
Operating system, OS) 123, application program 122 is referred to complete a certain or multinomial specific computer program being made for,
It operates in user's space, can interact with user, with visual user interface.Operating system 123 is that management is calculated
The computer program of machine hardware and software resource, operates in kernel spacing, is that run directly on " bare machine " most basic is
System software, any other software all must could run under the support of operating system.For different programs, the operation of program
Environment divides into User space and kernel state, and when application program 122 operates in low privilege level, residing running environment is user
State, it is impossible to direct access kernel space and computer hardware, when application program 122 operates in high privilege level, residing fortune
Row environment is kernel state, can be with access kernel space and computer hardware.The running environment of operating system 123 is kernel state.Should
Brought into operation in the running environment of User space with program 122, it is User space process that it is run to the process for terminating.Each use
When family state process is run, including several independent threads.The thread run in the running environment of User space is User space line
Journey, the thread run in the running environment of kernel state is kernel state thread.
Memorizer 120 also stored for computer and allow the data 124 that User space thread and kernel state thread are accessed jointly.Behaviour
Make function that system 123 operated including several to semaphore for example, create the function of semaphore, the letter of application semaphore
Number, the function of release semaphore, the wake-up function of the thread in the function of shutdown signal amount and the waiting list of wake-up signal amount.
Semaphore is a kind of protection mechanism used when multiple threads concurrently access data 124, before thread accesses data 124,
The thread must obtain an echo signal amount, and echo signal amount is directed to the semaphore of the data 124, there is no the mesh
The thread of mark semaphore does not allow to access the data 124, and then ensures data 124 not by concurrent invocation by echo signal amount.
Computer starting load driver program carry out it is initialized during, apply memorizer 120 in memory space, store one
Or multiple semaphores, then each semaphore is initialized, the title of each semaphore is entered as into sky.Initialization
After end, it is allowed to which kernel state thread and User space thread access the memory space that semaphore is stored in memorizer 120 jointly, to create
Build, apply or using echo signal amount.
The function for creating semaphore is used for according to target designation, and whether consults memory 120 is comprising title and target
Title identical semaphore, when title is not included with the target designation identical semaphore in the memorizer 120, creates
Echo signal amount, the entitled described target designation of the echo signal amount.The function of application semaphore is used to inquire about the mesh
The state parameter of mark semaphore, according to different state parameters, confirms echo signal amount in idle condition or seizure condition.
The function of application semaphore is additionally operable to the state parameter when the echo signal amount is inquired in idle condition, by the mesh
The value of the state parameter of mark semaphore is revised as the value of the state parameter for representing seizure condition.The function of release semaphore is used for will
The value of the state parameter of the echo signal amount is revised as the value of the state parameter for representing idle condition.The function of shutdown signal amount
For carrying out to the reference number of times of the echo signal amount from the computing that subtracts 1.Because the thread for having multiple access data 124 needs Shen
Please the echo signal amount, by quote number of times can confirm that how many thread will apply for the echo signal amount.Each target
The initial value of the reference number of times of semaphore can be 0, show to prepare to apply for the echo signal amount, each thread Shen without thread
The reference number of times of echo signal amount please be carried out from add-one operation before the echo signal amount.Reference to the echo signal amount
Number of times is carried out from after the computing that subtracts 1, and the function of the shutdown signal amount is additionally operable to inquire about the reference number of times of the echo signal amount
Whether it is initial value, if initial value, then initializes the echo signal amount, the title of echo signal amount is entered as into sky,
The value of state parameter is revised as to represent the value of the state parameter of idle condition, number of times is quoted and is entered as the initial value.
Computer starting load driver program carry out it is initialized during, can be with the title of each semaphore of assignment
For sky, the state parameter for initializing each semaphore is idle condition.If necessary to the thread of the echo signal amount to applying
Counted, in initialization procedure, the reference number of times of each semaphore can also be set to initial value.
Operating system 123 additionally provides system call layer 125.The system call layer 125 includes chained list, the chained list bag
The behaviour of each function used in the running environment of kernel state in the operating system 123 provided for User space thread is provided
Make interface, each operate interface identifies and point to a function, and by the operate interface operating system can be found
Corresponding function in 123.During initialized, can load in operating system 123 and preserve and connect comprising several operations
The chained list of mouth.
During User space thread accesses data 124, by system call layer 125, search what the system call layer 125 was provided
The chained list is operated so as to obtain the function operated to echo signal amount to echo signal amount, is applied to target letter
Number amount after, visit again data 124.
Memorizer 120 includes volatile memory, nonvolatile memory or its combination.Volatile memory be, for example, with
Machine accesses memorizer (English:Random-access memory, RAM).Nonvolatile memory such as floppy disk, hard disk, solid-state
Hard disk (solid state disk, SSD), CD etc. are various can be with the machine readable media of store program codes.
Processor 110 is one of capital equipment of computer, and function is mainly interpretive machine instruction and processes calculating
Data in machine software.Processor 110 is probably a central processing unit (English:Central processing unit,
CPU), it is also possible to CPLD (English:Complex Programmable Logic Device, CPLD)
Or field programmable gate array (Field-Programmable Gate Array, FPGA).
Fig. 2 is referred to, Fig. 2 is running environment schematic diagram when User space process provided in an embodiment of the present invention is run.Such as
Shown in Fig. 2, destroyed to prevent data 124 to be shared use, the User space thread L1 and kernel state line in User space process L
When journey L2 concurrently accesses data 124, echo signal amount 210 is applied.The thread applied to the echo signal amount 210 just may be used
To access data 124.Do not apply for the thread to the echo signal amount 210, be then mounted to the wait team of the echo signal amount 210
In row, treat other thread accesses data 124 and discharge after the echo signal amount 210, then be waken up.User space thread L1 and interior
Core state thread L2 can also be belonging respectively to different User space processes.
When Fig. 2 illustrates User space thread L1 access data 124, the process of application to echo signal amount 210.Such as Fig. 2 institutes
Show, User space thread L1 needs to first pass through system call layer 125 can just apply for echo signal amount 210, application to echo signal
After amount 210, data 124 can be just accessed.Kernel state thread L2 apply when accessing data 124 process of echo signal amount 210 with
The process of User space thread L1 application echo signals amount 210 is different.Difference is that kernel state thread L2 need not be by being
System calling layer 125, can directly operate echo signal amount 210.
With reference to the application scenarios of User space process L shown in Fig. 2, User space line disclosed in the description embodiment of the present invention
Journey and kernel state thread are created when concurrently accessing data, applied and using the method for echo signal amount.
Fig. 3 is referred to, Fig. 3 is the method flow diagram of User space thread accesses data 124 disclosed in the embodiment of the present invention.Such as
Shown in Fig. 3, when User space thread L1 accesses data 124, processor 110 performs following steps:
S310, according to target designation, whether consults memory 120 includes and the target designation identical semaphore.Mesh
Entitling claims to be set in advance, and the title of the semaphore accessed jointly by User space thread and kernel state thread.Memorizer 120
In preserve one or more semaphores, each semaphore can by User space thread and kernel state thread accesses, and each
Semaphore has unique title or entitled sky.Chained list in the meeting inquiry system of processor 110 calling layer 125, obtains and points to
The operate interface of the function of semaphore is created, according to the operate interface of the function for pointing to and creating semaphore memorizer is found
Establishment function in 120, performs the establishment function.The target designation be access data 124 function in it is predefined,
When thread accesses data 124 in advance, the function of data 124 is accessed described in computing device, according in the function for accessing data 124
Whether the target designation of definition, consults memory 120 includes and the target designation identical semaphore.
S320, when the memorizer 120 not comprising title with the target designation identical semaphore when, create target letter
Number amount 210, the entitled described target designation of the echo signal amount 210.The title of the echo signal amount 210, for only
The one mark echo signal amount 210.
In present embodiment, when User space thread L1 accesses data, can be according to the semaphore of data 124 to be visited
Whether target designation, inquire about in computer comprising the letter that can be jointly accessed by the User space thread L1 or kernel state thread L2
Number amount.It is the target designation by its name definition if it is not, selecting an entitled empty echo signal amount 210.By
This creates an echo signal amount 210 that can be jointly accessed by the User space thread L1 or kernel state thread L2, to avoid
The User space thread L1 or kernel state thread L2 are clashed when concurrently accessing data 124.
After creating echo signal amount 210, the User space thread L1 and kernel state thread L2 for concurrently accessing data 124 can be competing
Strive application echo signal amount 210.The application process of User space thread L1 application echo signals amount 210 is described below, it is concrete to process
Device 110 performs following steps:
S330, the state parameter for inquiring about the echo signal amount 210.Each semaphore in memorizer 120 has one
Individual state parameter, the state parameter is used to represent the use state of semaphore.For example represent letter when the value of state parameter is 0
Number amount be in idle condition, when state parameter value be 1 when represent semaphore be in seizure condition.Processor 110 can inquire about institute
Chained list is stated, the operate interface of the function for pointing to application semaphore is obtained, according to the operation of the function for pointing to and applying for semaphore
Interface performs the function of the application semaphore.
S340, when inquire the echo signal amount 210 in idle condition when, by the shape of the echo signal amount 210
The value of state parameter is revised as representing the state parameter " 1 " of seizure condition.When the value of the state parameter of the echo signal amount 210 is repaiied
It is changed to represent the state parameter " 1 " of seizure condition, then shows that echo signal amount 210 has been arrived in User space thread L1 applications.So, its
His thread, such as kernel state thread L2 then can not apply to echo signal amount 210.The purpose of modification state parameter is to competition Shen
Please other threads of echo signal amount 210 prove that the echo signal amount 210 is currently at what is taken by the User space thread L1
State, other threads can not apply for the echo signal amount 210, to ensure the concurrent thread for competing application echo signal amount in an orderly manner
Application echo signal amount.
S350, the data 124 for accessing the target designation sensing.When the echo signal amount 210 is in idle condition,
Illustrate even to conduct interviews it without data 124 described in other thread accesses, therefore processor 110.Step S340 with
Step S350 can be performed simultaneously, and execution sequence is in no particular order.
After User space thread L1 is accessed data 124 and finished, processor 110 is additionally operable to the echo signal amount
The value of 210 state parameter is revised as representing the state parameter " 0 " of idle condition.The meeting inquiry linked list of processor 110, obtains and points to
The operate interface of the function of release semaphore, according to the operate interface of the function of the sensing release semaphore, finds storage
The function of the release semaphore in device 120, performs the function of the release semaphore, discharges the echo signal amount 210.
User space thread L1 is created after echo signal amount 210, and kernel state thread L2 inquires echo signal amount 210, meeting
With User space thread L1 competition application echo signals amount 210.Step S330 to step S330 describe with kernel state thread
Win during L2 competition application echo signal amounts 210, the process of application to echo signal amount 210.Correspondingly, kernel state thread L2 exists
Process during application echo signal amount 210 may refer to Fig. 4.Fig. 4 is kernel state thread L2 provided in an embodiment of the present invention access
The method flow diagram of data 124.As shown in figure 4, when kernel state thread L2 accesses data 124, processor 110 performs following step
Suddenly:
S410, according to target designation, whether consults memory 120 includes and the target designation identical semaphore.Mesh
Entitling claims to be set in advance, and the title of the semaphore accessed jointly by User space thread and kernel state thread.Memorizer 120
In preserve one or more semaphores, each semaphore can by User space thread and kernel state thread accesses, and each
Semaphore has unique title or entitled sky.The step is with the difference of step S310 shown in Fig. 3, processor
110 functions for directly performing the establishment semaphore, without the chained list in inquiry system calling layer 125, point to without obtaining
Create the operate interface of the function of semaphore.Kernel state thread L2 is by searching the echo signal amount 210, it is possible to achieve the mesh
Mark semaphore 210 is not created the kernel state thread L2 of echo signal amount 210 and is accessed, and realizes kernel state thread L2 and user
State thread L1 is for the operation such as application of echo signal amount 210 is for the visible probability of other side, it is to avoid concurrent User space line
Journey L1 and kernel state thread L2 are clashed when accessing data 124.
S420, when inquiring the memorizer 120 comprising title and the target designation identical echo signal amount, inquiry
The state parameter of the echo signal amount 210, the state parameter is used to represent the use state of semaphore.For example when state is joined
Several values represents that semaphore, in idle condition, represents that semaphore is in seizure condition when the value of state parameter is 1 when being 0.
The step is that processor 110 directly performs the letter of the application semaphore with the difference of step S330 shown in Fig. 3
Number, without the chained list in inquiry system calling layer 125, connects without the operation for obtaining the function for pointing to the application semaphore
Mouthful.By inquiring about state parameter, the current state of the echo signal amount 210 is determined, so echo signal amount 210 is by User space line
Journey L1 applies for success or not, for the kernel state thread L2 of competition application echo signal amount 210 is visible, and then avoids user
State thread L1 and kernel state thread L2 are clashed when concurrently accessing data 124.
S430, when inquire the echo signal amount 210 in seizure condition when, stop execution task.And by kernel state
Thread L2 is mounted in the waiting list of the echo signal amount 210, waits User space thread L1 to discharge the echo signal amount 210
Afterwards, by the User space thread wakening application echo signal amount 210.
If in the embodiment shown in Fig. 3, User space thread L1 releases target letter after data 124 have been accessed
Number amount 210, then the echo signal amount 210 be in idle condition.Then the inquiry linked list of processor 110, obtains and points to wake-up letter
Several operate interfaces, finds the wake-up function from memorizer 120, performs and wakes up function, and to wake up the target is mounted to
Kernel state thread L2 in the waiting list of semaphore 210.After kernel state thread L2 is waken up, processor 110 is inquired about again described
The state parameter of echo signal amount 210, if inquiring the echo signal amount 210 in idle condition, can refer to Fig. 3 institutes
The step of showing S340, application echo signal amount 210 goes to access data 124, then discharges echo signal amount 210.
User space thread L1 shown in Fig. 3 is accessed in the embodiment of data 124, has been performed and has been created the echo signal amount
After 210 the step of S320, before applying for the echo signal amount 210, the reference number of times of echo signal amount 210 is carried out to transport from Jia 1
Calculate.It is assumed that carrying out before add-one operation, the reference number of times of echo signal amount 210 is initial value 0, then after carrying out add-one operation
The reference number of times of the echo signal amount 210 is 1.In other cases, the reference number of times of echo signal amount 210 carried out every time
Plus the granularity of computing can be other numerical value more than zero.Kernel state thread L2 shown in Fig. 4 is inquired in the memorizer 120
Title comprising semaphore and target designation identical echo signal amount 210, are applying for the access data of echo signal amount 210
Before 124, also the reference number of times of echo signal amount 210 is carried out from add-one operation, now the reference of the echo signal amount 210
Number of times is 2.Each thread application to the echo signal amount 210 has been accessed after data 124, can discharge the echo signal amount 210.
After release echo signal amount 210, the reference number of times of the echo signal amount 210 of processor 110 pairs is carried out from subtract 1 computing, table
It is bright no longer to apply for the echo signal amount 210.Processor the reference number of times of the echo signal amount 210 is carried out from the computing that subtracts 1 it
Afterwards, whether the reference number of times for also inquiring about the echo signal amount 210 is initial value, if initial value, then initializes the mesh
Mark semaphore 210, by the title of echo signal amount 210 sky is entered as, and the value of state parameter is initialized as representing idle condition
The value of state parameter, quotes number of times and is entered as the initial value.User space thread L1 is accessed in the embodiment of data, processor
110 meeting inquiry linked lists, obtain the operate interface of the function for pointing to shutdown signal amount, according to the function of the sensing shutdown signal amount
Operate interface find the function of the shutdown signal amount in memorizer 120, perform the function of the shutdown signal amount, close mesh
Mark semaphore 210.
In addition, in the above-described embodiments, to prevent other threads or the process such as computer interruption, abnormal to echo signal amount
The value of 210 state parameter is modified, and can also carry out locking protection to echo signal amount 210.Each letter in memorizer 120
Number amount also have lock Protection parameters, the lock Protection parameters be used for represent whether semaphore carries out lock protection.For example, lock protection ginseng
Several values is that the 1 expression echo signal amount 210 is in locking state, and the value for locking Protection parameters represents the echo signal for 0
Amount 210 is in empty lock status.
Specifically, before application echo signal amount 210, the lock Protection parameters of the echo signal amount 210 are inquired about, when looking into
Ask the expression echo signal amount 210 in empty lock status when, the value for locking Protection parameters is revised as locking state by processor
1.Then the state parameter of echo signal amount 210 is inquired about, when the echo signal amount 210 is inquired in idle condition, will
The value of state parameter is revised as representing " 1 " of seizure condition, then is revised as representing empty lock status by the value of lock Protection parameters
“0”.After locking protection, then the state parameter for inquiring about echo signal amount 210, it is ensured that the thread of currently employed locking protection can be with
The state parameter of inquiry echo signal amount 210.
In addition, the interruption of thread or computer, the exception to prevent other to be not belonging in the waiting list of echo signal amount 210
The value of the state parameter of echo signal amount 210 is modified etc. process, before release echo signal amount 210, processor 110
The lock Protection parameters of inquiry echo signal amount 210, when the expression echo signal amount 210 is inquired in empty lock status, will
The value of the lock Protection parameters of echo signal amount 210 is revised as representing " 1 " of locking state.In making for the echo signal amount 210
It is turned into idle condition from seizure condition with state and discharges after the echo signal amount 210, then by echo signal amount 210
The value of lock Protection parameters is revised as representing " 0 " of empty lock status.
Fig. 5 is referred to, Fig. 5 is a kind of structure chart of the creating device of semaphore provided in an embodiment of the present invention.Such as Fig. 5 institutes
Show, the creating device 510 of semaphore provided in an embodiment of the present invention, in computer 500, the creating device 510 includes
Acquisition module 511 and processing module 512.One or more semaphores are preserved in the computer 500, each semaphore has
Unique title or entitled sky, described each semaphore is can be described two by the semaphore of two kinds of thread accesses
The thread of type includes User space thread and kernel state thread, and the first thread is wherein the one of the thread of described two types
Kind.The function of each module of creating device 510 is described below:
Acquisition module 511, for when first thread accesses data, obtaining the first thread data to be visited
Semaphore target designation.The processor 110 that acquisition module 511 can pass through in computer in present embodiment is realized, obtained
Delivery block 511 obtains the function of target designation, and may be referred to the step S310 description shown in Fig. 3 obtains the concrete of target designation
Details, repeats no more at this.
Processing module 512, for according to the target designation inquire about in the computer 500 whether comprising title with it is described
Target designation identical semaphore, the processing module 512 is additionally operable to when the computer 500 is not comprising title and the mesh
When entitling claims identical semaphore, an entitled empty echo signal amount 210 is selected, by the name of the echo signal amount 210
Title is defined as the target designation.Processing module 512 can be realized by the processor 110 in computer, processing module 512
The function of echo signal amount 210 is created, the detail of the establishment echo signal amount 210 that the S320 shown in Fig. 3 is described is referred to,
Repeat no more at this.
As a kind of optional embodiment, the processing module 512, it is additionally operable to inquiry and represents the echo signal amount
210 state parameter, the value of the state parameter represents the echo signal amount 210 in idle condition or seizure condition.
The processing module 512, is additionally operable to when the value of the state parameter represents that the echo signal amount 210 is in idle condition,
Access the data.In present embodiment, the function of the application echo signal of processing module 512 amount 210 can refer to the step shown in Fig. 3
Rapid S330 and S340 application echo signals amount 210 implements details, repeats no more at this.
As a kind of optional embodiment, the processing module 512, it is additionally operable to change the value of the state parameter, institute
The value for stating amended state parameter represents that the echo signal amount 210 is in seizure condition.
As a kind of optional embodiment, the acquisition module 511, it is additionally operable to when data described in other thread accesses
When, obtain the target designation.In present embodiment, the processor 110 that acquisition module 511 can pass through in computer is realized,
Acquisition module 511 inquires about the function of echo signal amount 210 according to target designation, may be referred to the step S310 description shown in Fig. 3
Acquisition echo signal amount detail, repeat no more at this.
The processing module 512, is additionally operable to be obtained during the data according to the acquisition module 511 is in other thread accesses
The target designation, whether inquire about the memorizer comprising title and the target designation identical semaphore.And process
Module 512 can realize that processing module 512 inquires about the function of echo signal amount 210 by the processor 110 in computer,
The detail of the inquiry echo signal amount 210 that the S410 shown in Fig. 4 is described is referred to, is repeated no more at this.
The processing module 512, is additionally operable to it is determined that comprising the echo signal amount 210 in the computer 500, and
During the entitled target designation of the echo signal amount 210, the state parameter is inquired about, second thread is described two
The one of which of the thread of type, and the type of second thread is different from the type of the first thread.The place
Reason module 512, when being additionally operable to determine that the value of the state parameter represents that the echo signal amount 210 is in seizure condition, stops
Access the data.And the function of the inquiry state parameter of processing module 512 is referred to the S420 and S430 description shown in Fig. 4
Inquiry state parameter detail, repeat no more here.
As a kind of optional embodiment, the processing module 512, it is additionally operable to inquire about institute in the processing module 512
Before stating state parameter, the lock Protection parameters of the echo signal amount 210 are inquired about;The value of the lock Protection parameters represents the mesh
Mark semaphore 210 is in locking state or empty lock status;The processing module 512, is additionally operable to when the lock Protection parameters
When value represents that the echo signal amount 210 is in empty lock status, the value of the lock Protection parameters is changed;The amended lock is protected
The value of shield parameter represents that the echo signal amount 210 is in locking state.In present embodiment, processing module 512 is believed target
The locking protection function of number amount 210 can refer to description in said method embodiment, repeat no more at this.
In embodiments of the present invention, User space thread or during kernel state thread accesses data, can be according to be visited
Data 124 semaphore target designation, whether comprising can be by the User space thread and kernel state line in inquiry computer
The semaphore that Cheng Gongtong is accessed.If it is not, selecting an entitled empty echo signal amount 210, it is by its name definition
The target designation.Thus a semaphore that can be jointly accessed by the User space thread and the kernel state thread is created,
To avoid the User space thread and kernel state thread from clashing when concurrently accessing data.
In several embodiments provided herein, it should be understood that disclosed system, apparatus and method can be with
Realize by another way.For example, device embodiment described above is only schematic, for example, the module
Divide, only a kind of division of logic function there can be an other dividing mode when realizing, such as multiple module or components can be with
With reference to or be desirably integrated into another system, or some features can be ignored, or not perform.It is another, it is shown or discussed
Coupling each other or direct-coupling or communication connection can be INDIRECT COUPLING by some interfaces, device or module or
Communication connection, can be electrical, mechanical or other forms.
The module as separating component explanation can be or may not be it is physically separate, it is aobvious as module
The part for showing can be or may not be physical module, you can with positioned at a place, or can also be distributed to multiple
On mixed-media network modules mixed-media.Some or all of module therein can according to the actual needs be selected to realize the mesh of this embodiment scheme
's.
In addition, each functional module in each embodiment of the invention can be integrated in a processing module, it is also possible to
It is that modules are individually physically present, it is also possible to which two or more modules are integrated in a module.Above-mentioned integrated mould
Block both can be realized in the form of hardware, it would however also be possible to employ hardware adds the form of software function module to realize.
The above-mentioned integrated module realized in the form of software function module, can be stored in an embodied on computer readable and deposit
In storage media.Above-mentioned software function module is stored in a storage medium, including some instructions are used so that a computer
Equipment (can be personal computer, server, or network equipment etc.) performs the portion of each embodiment methods described of the invention
Step by step.And aforesaid storage medium includes:Portable hard drive, read only memory, random access memory, magnetic disc or CD etc.
It is various can be with the medium of storage function code.
Finally it should be noted that:Above example only to illustrate technical scheme, rather than a limitation;Although
The present invention has been described in detail with reference to the foregoing embodiments, it will be understood by those within the art that:It still may be used
To modify to the technical scheme described in foregoing embodiments, or equivalent is carried out to which part technical characteristic;
And these modifications or replacement, do not make the protection model of the essence disengaging various embodiments of the present invention technical scheme of appropriate technical solution
Enclose.
Claims (10)
1. a kind of creation method of semaphore, it is characterised in that methods described is applied to computer, the computer includes processing
Device and memorizer, when first thread accesses data, include described in the computing device during first thread:
Obtain the target designation of the semaphore of the first thread data to be visited;
Whether the memorizer is inquired about comprising title and the target designation identical semaphore according to the target designation, it is described
One or more semaphores are preserved in memorizer, each semaphore has unique title or entitled sky, described each letter
For can be by the semaphore of two kinds of thread accesses, the thread of described two types includes User space thread and kernel state for number amount
Thread, the first thread is the one of which of the thread of described two types;
When the memorizer does not include title with the target designation identical semaphore, an entitled empty target is selected
Semaphore, is the target designation by the name definition of the echo signal amount.
2. the method for claim 1, it is characterised in that also include:
The state parameter of the echo signal amount is inquired about, the value of the state parameter represents the echo signal amount in idle shape
State or seizure condition;
When the value of the state parameter represents the echo signal amount in idle condition, the data are accessed.
3. method as claimed in claim 2, it is characterised in that also include:
The value of the state parameter is changed, the value of the amended state parameter represents the echo signal amount in occupancy shape
State.
4. method as claimed in claim 3, it is characterised in that in the value of the state parameter of the modification echo signal amount
Afterwards, when data described in the second thread accesses, methods described also also performs second thread including the processor, described
Include during the second thread described in computing device:
Obtain the target designation;
Whether the memorizer is inquired about comprising title and the target designation identical semaphore according to the target designation;
Determines the echo signal amount that include in the memorizer, and the entitled described target designation of the echo signal amount
When, inquire about the state parameter, second thread is the one of which of the thread of described two types, and second line
The type of journey is different from the type of the first thread;
When determining that the value of the state parameter represents that the echo signal amount is in seizure condition, stop accessing the data.
5. the method as described in arbitrary in claim 2-4, it is characterised in that before the inquiry state parameter, also
Including:
Inquire about the lock Protection parameters of the echo signal amount;The value of the lock Protection parameters represents that the echo signal amount is in and adds
Lock status or empty lock status;
When the value of the lock Protection parameters represents the echo signal amount in empty lock status, the lock Protection parameters are changed
Value;The value of the amended lock Protection parameters represents that the echo signal amount is in locking state.
6. a kind of creating device of semaphore, it is characterised in that described device is located in computer, and described device includes:
Acquisition module, for when first thread accesses data, obtaining the signal of the first thread data to be visited
The target designation of amount;
Processing module, for whether being inquired about in the computer comprising title and the target designation phase according to the target designation
Same semaphore, preserves one or more semaphores in the computer, each semaphore has unique title or title
For sky, described each semaphore is to include using by the semaphore of two kinds of thread accesses, the thread of described two types
Family state thread and kernel state thread, the first thread is the one of which of the thread of described two types;
The processing module, is additionally operable to when the computer does not include title with the target designation identical semaphore, choosing
An entitled empty echo signal amount is selected, is the target designation by the name definition of the echo signal amount.
7. device as claimed in claim 6, it is characterised in that
The processing module, is additionally operable to inquire about the state parameter of the echo signal amount, and the value of the state parameter represents described
Echo signal amount is in idle condition or seizure condition;
The processing module, is additionally operable to, when the value of the state parameter represents that the echo signal amount is in idle condition, visit
Ask the data.
8. device as claimed in claim 7, it is characterised in that
The processing module, is additionally operable to change the value of the state parameter, and the value of the amended state parameter represents described
Echo signal amount is in seizure condition.
9. device as claimed in claim 8, it is characterised in that
The acquisition module, is additionally operable to, when data described in the second thread accesses, obtain the target designation;
The processing module, be additionally operable to be obtained during the data according to the acquisition module is in second thread accesses described in
Whether target designation, inquire about the memorizer comprising title and the target designation identical semaphore;
The processing module, is additionally operable to it is determined that comprising the echo signal amount in the computer, and the echo signal
During the entitled target designation of amount, the state parameter is inquired about, second thread is the thread of described two types
One of which, and the type of second thread is different from the type of the first thread;
The processing module, when being additionally operable to determine that the value of the state parameter represents that the echo signal amount is in seizure condition,
Stop accessing the data.
10. the device as described in arbitrary in claim 7-9, it is characterised in that
The processing module, was additionally operable to before the processing module inquires about the state parameter, inquired about the echo signal amount
Lock Protection parameters;The value of the lock Protection parameters represents the echo signal amount in locking state or empty lock status;
The processing module, is additionally operable to when the value of the lock Protection parameters represents the echo signal amount in empty lock status,
The value of the modification lock Protection parameters;The value of the amended lock Protection parameters represents the echo signal amount in locking shape
State.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201611239233.2A CN106681836B (en) | 2016-12-28 | 2016-12-28 | Semaphore creation method and semaphore creation device |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201611239233.2A CN106681836B (en) | 2016-12-28 | 2016-12-28 | Semaphore creation method and semaphore creation device |
Publications (2)
Publication Number | Publication Date |
---|---|
CN106681836A true CN106681836A (en) | 2017-05-17 |
CN106681836B CN106681836B (en) | 2021-03-05 |
Family
ID=58872274
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN201611239233.2A Active CN106681836B (en) | 2016-12-28 | 2016-12-28 | Semaphore creation method and semaphore creation device |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN106681836B (en) |
Cited By (6)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN107590015A (en) * | 2017-09-11 | 2018-01-16 | 北京京东尚科信息技术有限公司 | The method and apparatus for monitoring the obstruction of main thread |
CN110309024A (en) * | 2019-04-23 | 2019-10-08 | 网宿科技股份有限公司 | Data processing system and its method for executing data processing task |
CN110569131A (en) * | 2019-08-22 | 2019-12-13 | 北京华捷艾米科技有限公司 | semaphore management system and semaphore management method |
CN111475264A (en) * | 2020-02-28 | 2020-07-31 | 新华三技术有限公司合肥分公司 | Method and device for realizing lock-free forwarding of user mode |
CN112612582A (en) * | 2020-12-14 | 2021-04-06 | 北京和利时系统工程有限公司 | Semaphore function implementation method and semaphore function implementation device |
CN117076139A (en) * | 2023-10-17 | 2023-11-17 | 北京融为科技有限公司 | Data processing method and related equipment |
Citations (7)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN102200930A (en) * | 2011-05-26 | 2011-09-28 | 华为数字技术有限公司 | Synchronous variable mapping method and device, synchronous variable freeing method and synchronous variable deleting method |
CN102262559A (en) * | 2010-05-24 | 2011-11-30 | 腾讯科技(深圳)有限公司 | Resource sharing method and system |
CN102999378A (en) * | 2012-12-03 | 2013-03-27 | 中国科学院软件研究所 | Read-write lock implement method |
CN103092784A (en) * | 2011-10-27 | 2013-05-08 | 飞思卡尔半导体公司 | Systems and methods for semaphore-based protection of shared system resources |
CN103731328A (en) * | 2014-01-02 | 2014-04-16 | 烽火通信科技股份有限公司 | System and method for achieving home gateway data communication based on Linux shared memory |
CN104980454A (en) * | 2014-04-02 | 2015-10-14 | 腾讯科技(深圳)有限公司 | Method, server and system for sharing resource data |
US20160344820A1 (en) * | 2015-05-21 | 2016-11-24 | Freescale Semiconductor, Inc. | Method and apparatus for controlling wake events in a data processing system |
-
2016
- 2016-12-28 CN CN201611239233.2A patent/CN106681836B/en active Active
Patent Citations (7)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN102262559A (en) * | 2010-05-24 | 2011-11-30 | 腾讯科技(深圳)有限公司 | Resource sharing method and system |
CN102200930A (en) * | 2011-05-26 | 2011-09-28 | 华为数字技术有限公司 | Synchronous variable mapping method and device, synchronous variable freeing method and synchronous variable deleting method |
CN103092784A (en) * | 2011-10-27 | 2013-05-08 | 飞思卡尔半导体公司 | Systems and methods for semaphore-based protection of shared system resources |
CN102999378A (en) * | 2012-12-03 | 2013-03-27 | 中国科学院软件研究所 | Read-write lock implement method |
CN103731328A (en) * | 2014-01-02 | 2014-04-16 | 烽火通信科技股份有限公司 | System and method for achieving home gateway data communication based on Linux shared memory |
CN104980454A (en) * | 2014-04-02 | 2015-10-14 | 腾讯科技(深圳)有限公司 | Method, server and system for sharing resource data |
US20160344820A1 (en) * | 2015-05-21 | 2016-11-24 | Freescale Semiconductor, Inc. | Method and apparatus for controlling wake events in a data processing system |
Cited By (8)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN107590015A (en) * | 2017-09-11 | 2018-01-16 | 北京京东尚科信息技术有限公司 | The method and apparatus for monitoring the obstruction of main thread |
CN110309024A (en) * | 2019-04-23 | 2019-10-08 | 网宿科技股份有限公司 | Data processing system and its method for executing data processing task |
CN110569131A (en) * | 2019-08-22 | 2019-12-13 | 北京华捷艾米科技有限公司 | semaphore management system and semaphore management method |
CN111475264A (en) * | 2020-02-28 | 2020-07-31 | 新华三技术有限公司合肥分公司 | Method and device for realizing lock-free forwarding of user mode |
CN111475264B (en) * | 2020-02-28 | 2023-05-12 | 新华三技术有限公司合肥分公司 | Method and device for realizing user mode lock-free forwarding |
CN112612582A (en) * | 2020-12-14 | 2021-04-06 | 北京和利时系统工程有限公司 | Semaphore function implementation method and semaphore function implementation device |
CN117076139A (en) * | 2023-10-17 | 2023-11-17 | 北京融为科技有限公司 | Data processing method and related equipment |
CN117076139B (en) * | 2023-10-17 | 2024-04-02 | 北京融为科技有限公司 | Data processing method and related equipment |
Also Published As
Publication number | Publication date |
---|---|
CN106681836B (en) | 2021-03-05 |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
CN106681836A (en) | Creating method and device of signal quantity | |
CN101221514B (en) | Method, processor and system for selecting the architecture level to which a processor appears to conform | |
CN104636202A (en) | Computer system and scheduling method thereof | |
US20100325633A1 (en) | Searching Regular Expressions With Virtualized Massively Parallel Programmable Hardware | |
CN103699437A (en) | Resource scheduling method and device | |
US9086911B2 (en) | Multiprocessing transaction recovery manager | |
CN108334408B (en) | Code execution method and device, terminal equipment and computer readable storage medium | |
CN110187835A (en) | For managing the method, apparatus, equipment and storage medium of access request | |
CN109818810A (en) | A kind of access server connection optimization method, access server and communication system | |
US20200272512A1 (en) | Hardware for supporting os driven observation and anticipation based on more granular, variable sized observation units | |
CN109359060B (en) | Data extraction method, device, computing equipment and computer storage medium | |
CN101221515A (en) | Method and system for scheduling execution units | |
WO2008003536A2 (en) | Method, system and computer program for determining the processing order of a plurality of events | |
CN108021405A (en) | The driving method and device of storage medium in a kind of SOC system starting process | |
CN104102549A (en) | Method, device and chip for realizing mutual exclusion operation of multiple threads | |
CN109634714B (en) | Intelligent scheduling method and device | |
CN109992526A (en) | A kind of read-write management method and relevant apparatus | |
EP3702911A2 (en) | Hardware for supporting os driven load anticipation based on variable sized load units | |
CN111258950B (en) | Atomic access and storage method, storage medium, computer equipment, device and system | |
CN106970998A (en) | The update method and device of news data | |
CN116260876A (en) | AI application scheduling method and device based on K8s and electronic equipment | |
CN110851245A (en) | Distributed asynchronous task scheduling method and electronic equipment | |
CN109583198B (en) | Page monitoring method and device, storage medium and electronic equipment | |
CN111353766A (en) | Service process processing system and method of distributed service system | |
CN110010167A (en) | It is a kind of to improve method, apparatus, computer equipment and the storage medium for obtaining descriptor state |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination | ||
GR01 | Patent grant | ||
GR01 | Patent grant |