CN102662632A - Serial number generation method based on semaphore and generator. - Google Patents

Serial number generation method based on semaphore and generator. Download PDF

Info

Publication number
CN102662632A
CN102662632A CN2012100665843A CN201210066584A CN102662632A CN 102662632 A CN102662632 A CN 102662632A CN 2012100665843 A CN2012100665843 A CN 2012100665843A CN 201210066584 A CN201210066584 A CN 201210066584A CN 102662632 A CN102662632 A CN 102662632A
Authority
CN
China
Prior art keywords
semaphore
sequence number
link
sequence
serial number
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
Application number
CN2012100665843A
Other languages
Chinese (zh)
Other versions
CN102662632B (en
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.)
BEIJING DIGITAL CHINA SI-TECH INFORMATION TECHNOLOGY Co Ltd
Original Assignee
BEIJING DIGITAL CHINA SI-TECH INFORMATION TECHNOLOGY 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 BEIJING DIGITAL CHINA SI-TECH INFORMATION TECHNOLOGY Co Ltd filed Critical BEIJING DIGITAL CHINA SI-TECH INFORMATION TECHNOLOGY Co Ltd
Priority to CN201210066584.3A priority Critical patent/CN102662632B/en
Publication of CN102662632A publication Critical patent/CN102662632A/en
Application granted granted Critical
Publication of CN102662632B publication Critical patent/CN102662632B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Abstract

The invention discloses a serial number generation method based on a semaphore and a serial number generator. The steps of generating a serial number includes the following steps: an input unit inputting a link number; a receiving unit receiving the semaphore ID after opening the semaphore; after the receiving unit successfully obtains the semaphore ID, a locking unit carrying out a lock operation on the semaphore; a serial number generator unit calculating the serial number according to the link number when determining whether or not the link number is within a reasonable range; andan unlocking element carries out an unlocking operation on the semaphore when a judgment unit determines that the serial number has not reached the maximum, releasesthe occupied resource and returns to the serial number. The method guarantees the uniqueness of the serial number in an environment with multiprocessing and multithreading.

Description

A kind of sequence number generation method and maker that utilizes semaphore to realize
Technical field
The present invention relates to obtain under a kind of multi-process and the multi-thread environment method and the sequence number maker of unique sequence number.
Background technology
In the internet data transmission, often to use unique sequence number.Traditional approach generally adopts counter to add thread lock and controls, and under the multi-process environment, there are problems such as sequence number repeats in this method.Also have employing to read sequence number from database, though that this method can guarantee is unique, inefficiency can't be supported high concurrent.Therefore, problem to be solved by this invention is the uniqueness that how in multi-process, multithreading, high concurrent environment, guarantees sequence number.
Summary of the invention
One of the object of the invention provides a kind of sequence number generation method, guarantees the uniqueness of sequence number under multi-process and multithreading situation occurred frequently, and this method comprises following flow process:
Step 1, the input link numbering;
Step 2, the opening signal amount is obtained semaphore ID;
Step 3 if semaphore ID obtains success, adds latching operation to semaphore;
Whether in the reasonable scope step 4 judges link number, if, then according to the link number sequence of calculation number;
Wherein, number be specially, find the corresponding counter of link number, add up, formation sequence number according to the link number sequence of calculation.
Step 5 judges whether sequence number reaches maximal value, if not, and direct execution in step 6;
Step 6 is carried out unlocking operation to semaphore, discharges the resource that it takies, and returns sequence number then.
Above-mentioned steps 3 further comprises, and is unsuccessful if semaphore I D obtains, and then creates semaphore, if semaphore is created successfully, then semaphore locked, unsuccessful if semaphore is created, and then obtains Signal Fail, process ends.
Above-mentioned steps 5 further comprises, if sequence number has reached maximal value, then this sequence number is changed to initial value again, and execution in step 6 then.
Two of the object of the invention provides a kind of sequence number maker, guarantees the uniqueness of sequence number under multi-process and multithreading situation occurred frequently, and this sequence number maker comprises:
Input block is numbered in order to input link;
Acquiring unit in order to the opening signal amount, obtains semaphore ID;
Add lock unit, after obtaining successfully, semaphore is added latching operation at semaphore ID;
Whether in the reasonable scope the sequence number generation unit judges link number, if, then according to the link number sequence of calculation number;
Judging unit judges whether sequence number reaches maximal value;
Separate lock unit,, semaphore is carried out unlocking operation, discharge the resource that it takies, return sequence number then if sequence number does not reach maximal value.
Wherein, the sequence number generation unit number is specially according to the link number sequence of calculation, finds the corresponding counter of link number, adds up formation sequence number.
This sequence number maker comprises that further semaphore creates the unit, is used for obtaining when unsuccessful the establishment semaphore as semaphore ID; If semaphore is created successfully, then semaphore is locked by adding lock unit, unsuccessful if semaphore is created; Then obtain Signal Fail, process ends.
This sequence number maker further comprises the sequence number reset cell, in order to when the judgment unit judges sequence number has reached maximal value, then this sequence number is changed to initial value again, and then carries out unlocking operation by separating lock unit.
The invention has the beneficial effects as follows:
When 1) adopting the present invention to obtain unique sequence number, can directly transplanting, need not change.Can set the initial value of sequence number, maximal value after sequence number reaches maximal value, begins circulation from initial value automatically.
2) the present invention can guarantee that under the concurrent situation of height sequence number remains unique in certain period.Time is by the initial value of setting in advance, maximal value, and concurrency decision.
Description of drawings
Fig. 1 is the process flow diagram of sequence number generation method of the present invention.
Embodiment
Below in conjunction with accompanying drawing principle of the present invention and characteristic are described, institute gives an actual example and only is used to explain the present invention, is not to be used to limit scope of the present invention.
The present invention provides a kind of sequence number generation method, and flow process is following:
Step 1, the input link numbering.
Step 2, the opening signal amount is obtained semaphore ID.
Be specially, according to/etc/hosts, through ftok, the opening signal amount is obtained signal ID.
The ftok function definition is following:
key_t?ftok(char*fname,int?id);
Fname is exactly the filename (filename that has existed) of appointment, generally uses current directory.
Id is sub-sequence number.
If guarantee that the key_t value is constant, guarantee that the file of ftok is not deleted.So the general filename that specifies is in the system/the etc/hosts file.
Step 3 judges whether semaphore ID obtains successful, if semaphore is added latching operation.
If it is unsuccessful that semaphore ID obtains, then create semaphore, if semaphore is created successfully, then semaphore is added latching operation, if semaphore obtains failure, process ends then.
Adopt function semop that semaphore is locked, the semop function definition is following:
int?semop(int?semid,struct?sembuf?*sops,unsigned?nsops)
Semid: the identification code of set of signals, can obtain through semget.
Sops: point to the array pointer of storage signal operating structure, the prototype of signal operation structure is following.
Figure BDA0000143276240000041
Sem_num: the numbering of operation signal in set of signals, the numbering of first signal is 0.
Sem_op: if its value is positive number, this value can be added in existing signal value of including.Be generally used for discharging the right to use of the resource of controlling; If the value of sem_op is a negative, and its absolute value is greater than the present worth of signal, and operation will be blocked, up to the absolute value of signal value more than or equal to sem_op.Be generally used for obtaining the right to use of resource; If the value of sem_op is 0, then operation will temporarily be blocked, and become 0 up to the value of signal.
Sem_flg: signal operation sign.
In adding latching operation, it is-1 that sem_op is set, and operation is blocked.After entering added latching operation, these piece shared drive data can not be by other process visits.
Whether in the reasonable scope step 4 judges link No., if, according to the link No. sequence of calculation number, if not, process ends then.
The sequence of calculation number is specially: in shared drive, each process has a counter.After getting into locking state, find corresponding counter according to route_id (each process all is provided with a unique value in advance), add up, formation sequence number, promptly sequence number increases progressively.
Step 5 judges whether sequence number reaches maximal value, if not, and direct execution in step 6.
If sequence number reaches maximal value, then this sequence number is changed to initial value again, execution in step 6 then.
Step 6 is carried out unlocking operation to semaphore, discharges the resource that it takies, and returns sequence number then.
The present invention also provides a kind of sequence number maker, guarantees the uniqueness of sequence number under multi-process and multithreading situation occurred frequently, and this sequence number maker comprises:
Input block is numbered in order to input link;
Acquiring unit in order to the opening signal amount, obtains semaphore ID;
Add lock unit, after obtaining successfully, semaphore is added latching operation at semaphore ID;
Whether in the reasonable scope the sequence number generation unit judges link number, if, then according to the link number sequence of calculation number;
Judging unit judges whether sequence number reaches maximal value;
Separate lock unit,, semaphore is carried out unlocking operation, discharge the resource that it takies, return sequence number then if sequence number does not reach maximal value.
Wherein, the sequence number generation unit number is specially according to the link number sequence of calculation, finds the corresponding counter of link number, adds up formation sequence number.
This sequence number maker comprises that further semaphore creates the unit, is used for obtaining when unsuccessful the establishment semaphore as semaphore ID; If semaphore is created successfully, then semaphore is locked by adding lock unit, unsuccessful if semaphore is created; Then obtain Signal Fail, process ends.
This sequence number maker further comprises the sequence number reset cell, in order to when the judgment unit judges sequence number has reached maximal value, then this sequence number is changed to initial value again, and then carries out unlocking operation by separating lock unit.
For example: during Shanxi telecommunication intelligent switching plane was implemented, every CCR message all will be obtained a unique HH, before system's operation, was that the link of 31 provinces is all mixed link number route_id, the initial value of setting sequence number then, maximal value.The initial value of each province and maximal value are all inequality, so that the HH of each province is in different scopes.
Suppose link number=1 o'clock to establish maximal value=100000000, initial value is 1, after this sequence number reaches maximal value, and can be from initial value from newly beginning circulation.
Input link numbering route_id, the opening signal amount is obtained semaphore ID, if obtain semaphore ID failure, then creates semaphore, if obtain semaphore ID success or create the semaphore success, then carries out semaphore and adds latching operation.When link number less than 0 or during greater than maximal value, return miscue, if link number is positioned at zone of reasonableness, then find corresponding counter according to route_id (each process all is provided with a unique value in advance), add up formation sequence number.Then, whether judge sequence number greater than maximal value, if not, then the unlocking signal amount discharges resource, if greater than maximal value, then sequence number is reset to initial value, and the unlocking signal amount discharges resource then.
The above is merely preferred embodiment of the present invention, and is in order to restriction the present invention, not all within spirit of the present invention and principle, any modification of being done, is equal to replacement, improvement etc., all should be included within protection scope of the present invention.

Claims (8)

1. a sequence number generation method of utilizing semaphore to realize comprises the steps:
Step 1, the input link numbering;
Step 2, the opening signal amount is obtained semaphore ID;
Step 3 if semaphore ID obtains success, adds latching operation to semaphore;
Whether in the reasonable scope step 4 judges link number, if, then according to the link number sequence of calculation number;
Step 5 judges whether sequence number reaches maximal value, if not, and direct execution in step 6;
Step 6 is carried out unlocking operation to semaphore, discharges the resource that it takies, and returns sequence number then.
2. sequence number generation method according to claim 1 is characterized in that: in the step 4, number be specially according to the link number sequence of calculation, find the corresponding counter of link number, add up, formation sequence number.
3. sequence number generation method according to claim 1 and 2 is characterized in that: this step 3 further comprises, and is unsuccessful if semaphore ID obtains; Then create semaphore; If semaphore is created successfully, then semaphore is locked, unsuccessful if semaphore is created; Then obtain Signal Fail, process ends.
4. sequence number generation method according to claim 1 and 2 is characterized in that: in the step 5, if sequence number has reached maximal value, then this sequence number is changed to initial value again, execution in step 6 then.
5. sequence number maker that utilizes semaphore to realize, this sequence number maker comprises:
Input block is numbered in order to input link;
Acquiring unit in order to the opening signal amount, obtains semaphore ID;
Add lock unit, after obtaining successfully, semaphore is added latching operation at semaphore ID;
Whether in the reasonable scope the sequence number generation unit judges link number, if, then according to the link number sequence of calculation number;
Judging unit judges whether sequence number reaches maximal value;
Separate lock unit,, semaphore is carried out unlocking operation, discharge the resource that it takies, return sequence number then if sequence number does not reach maximal value.
6. sequence number maker according to claim 5 is characterized in that: the sequence number generation unit number is specially according to the link number sequence of calculation, finds the corresponding counter of link number, adds up formation sequence number.
7. according to claim 5 or 6 described sequence number makers, it is characterized in that: this sequence number maker comprises that further semaphore creates the unit, is used for obtaining when getting nowhere as semaphore ID; Create semaphore; If semaphore is created successfully, then semaphore is locked by adding lock unit, unsuccessful if semaphore is created; Then obtain Signal Fail, end operation.
8. according to claim 5 or 6 described sequence number makers; It is characterized in that: this sequence number maker further comprises the sequence number reset cell; In order to when the judgment unit judges sequence number has reached maximal value; Then this sequence number is changed to initial value again, and then carries out unlocking operation by separating lock unit.
CN201210066584.3A 2012-03-14 2012-03-14 Serial number generation method based on semaphore and generator. Active CN102662632B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201210066584.3A CN102662632B (en) 2012-03-14 2012-03-14 Serial number generation method based on semaphore and generator.

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201210066584.3A CN102662632B (en) 2012-03-14 2012-03-14 Serial number generation method based on semaphore and generator.

Publications (2)

Publication Number Publication Date
CN102662632A true CN102662632A (en) 2012-09-12
CN102662632B CN102662632B (en) 2015-04-08

Family

ID=46772133

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201210066584.3A Active CN102662632B (en) 2012-03-14 2012-03-14 Serial number generation method based on semaphore and generator.

Country Status (1)

Country Link
CN (1) CN102662632B (en)

Cited By (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN105389226A (en) * 2015-10-23 2016-03-09 上海斐讯数据通信技术有限公司 System and method for accessing to abnormality of shared memory
CN107247770A (en) * 2017-06-05 2017-10-13 广东亿迅科技有限公司 Global sequence's generation method and device based on zookeeper
CN108614740A (en) * 2016-12-12 2018-10-02 上海美桔网络科技有限公司 The method and system of unique ID are generated based on shared drive mode between process
CN109656920A (en) * 2018-10-19 2019-04-19 中国建设银行股份有限公司 Sequence number processing method and system, device and storage medium
CN109948012A (en) * 2019-03-14 2019-06-28 北京达佳互联信息技术有限公司 Generation method, device and the storage medium of sequence number

Citations (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
JP2006114036A (en) * 2004-10-14 2006-04-27 Internatl Business Mach Corp <Ibm> Instruction group formation and mechanism for smt dispatch
CN1831779A (en) * 2005-03-11 2006-09-13 华为技术有限公司 Detection method of signal quantization deadlock

Patent Citations (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
JP2006114036A (en) * 2004-10-14 2006-04-27 Internatl Business Mach Corp <Ibm> Instruction group formation and mechanism for smt dispatch
CN1831779A (en) * 2005-03-11 2006-09-13 华为技术有限公司 Detection method of signal quantization deadlock

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
CXSUAL: "PHP下通过系统信号量加锁方式获取递序列ID", 《百度文库》, 11 October 2011 (2011-10-11), pages 1 - 4 *

Cited By (8)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN105389226A (en) * 2015-10-23 2016-03-09 上海斐讯数据通信技术有限公司 System and method for accessing to abnormality of shared memory
CN105389226B (en) * 2015-10-23 2018-07-13 上海斐讯数据通信技术有限公司 A kind of system and method accessing shared drive exception
CN108614740A (en) * 2016-12-12 2018-10-02 上海美桔网络科技有限公司 The method and system of unique ID are generated based on shared drive mode between process
CN107247770A (en) * 2017-06-05 2017-10-13 广东亿迅科技有限公司 Global sequence's generation method and device based on zookeeper
CN109656920A (en) * 2018-10-19 2019-04-19 中国建设银行股份有限公司 Sequence number processing method and system, device and storage medium
CN109656920B (en) * 2018-10-19 2021-08-27 中国建设银行股份有限公司 Serial number processing method, system, device and storage medium
CN109948012A (en) * 2019-03-14 2019-06-28 北京达佳互联信息技术有限公司 Generation method, device and the storage medium of sequence number
CN109948012B (en) * 2019-03-14 2021-11-16 北京达佳互联信息技术有限公司 Serial number generation method and device and storage medium

Also Published As

Publication number Publication date
CN102662632B (en) 2015-04-08

Similar Documents

Publication Publication Date Title
CN102662632A (en) Serial number generation method based on semaphore and generator.
CN108153784B (en) Synchronous data processing method and device
CN104020724A (en) Alarm monitoring method and device
CN103761148A (en) Control method for cluster timing scheduling tasks
CN105825122A (en) Weak password verification and cracking method and device
CN105740080B (en) A kind of stuck detection method of program and processor
CN103186333A (en) System and method for unlocking electronic equipment
US7523113B2 (en) Distributed system, computer and state transition control method for distributed system
CN103761260A (en) Method and device for processing mutually-exclusive lock of database, and distributed system
CN106250487B (en) Database concurrency control method and device
CN103294350A (en) Unlocking system and method of electronic equipment
CN106557371A (en) A kind of Distributed System Deadlock detection method followed the trail of based on side
CN103366139A (en) Electronic device, hardware maintenance system and hardware maintenance method
CN104978313A (en) Data synchronization method and apparatus for database system, and server
CN105426544B (en) Method and device for monitoring database state
CN105389335A (en) Data mining analysis method and device based on automatic exercise
CN104898973A (en) Mobile terminal and unlocking method for same
CN102880117B (en) A kind of screen catch system being applied to numerically-controlled machine
CN115237426A (en) Method, device and equipment for determining database difference and storage medium
CN104484376B (en) The method and device of displaying data in real-time
CN104424406A (en) Linear cutting unlocking method and system
CN104123193A (en) Computer resource monitor method
CN107766442B (en) A kind of mass data association rule mining method and system
CN104142865A (en) Data collecting and processing method based on thread synchronization
CN102054080A (en) Method and device for detecting unreasonable component placement constraint

Legal Events

Date Code Title Description
C06 Publication
PB01 Publication
C10 Entry into substantive examination
SE01 Entry into force of request for substantive examination
C53 Correction of patent of invention or patent application
CB02 Change of applicant information

Address after: 100085 Haidian District, Zhongguancun, South Street, No. 6,, building information, floor, No. 16

Applicant after: SI-TECH Information Technology Ltd.

Address before: 100085 Haidian District, Zhongguancun, South Street, No. 6,, building information, floor, No. 16

Applicant before: Beijing Digital China SI-TECH Information Technology Co., Ltd.

COR Change of bibliographic data

Free format text: CORRECT: APPLICANT; FROM: BEIJING DIGITAL CHINA SI-TECH INFORMATION TECHNOLOGY LTD. TO: BEIJING SI-TECH INFORMATION TECHNOLOGY LTD.

C14 Grant of patent or utility model
GR01 Patent grant