Summary of the invention
In view of this, the invention provides a kind of inter-process communication methods and device, in order to solve the technical matters of inter-process communication methods performance history complexity in prior art.For realizing described object, technical scheme provided by the invention is as follows:
A kind of inter-process communication methods, comprising:
When the first process generates data, obtain the Boost message queue be pre-created;
Described data are write described Boost message queue;
When receiving the described Boost message queue with data, the Data Concurrent extracted wherein delivers to the second process.
Alternatively, describedly described data are write described Boost message queue and comprise:
Determine the length of described data and default single message-length of described Boost message queue;
When the length of described data is greater than described default single message-length, in units of described default single message-length, described data is cut into slices, obtains multiple slice of data;
Described multiple slice of data is write described Boost message queue.
Alternatively, describedly described multiple slice of data write described Boost message queue comprise:
According to section order, described multiple slice of data is write described Boost message queue.
Alternatively, described data are being cut into slices, after obtaining multiple slice of data, are also comprising:
Add up the number of described slice of data, and the described number of statistics is write described Boost message queue.
Alternatively, described when receiving the described Boost message queue with data, the Data Concurrent extracted wherein is delivered to the second process and is comprised:
When receiving the described Boost message queue with data, extract slice of data wherein, until the number of the slice of data extracted equals the described number of adding up;
Splice the slice of data extracted, and the data that splicing obtains are sent to the second process.
This application provides a kind of interprocess communication device, comprising:
Message queue acquisition module, for when the first process generates data, obtains the Boost message queue be pre-created;
Message queue writing module, for writing described Boost message queue by described data;
Process data extraction module, for when receiving the described Boost message queue with data, the Data Concurrent extracted wherein delivers to the second process.
Alternatively, described message queue writing module comprises:
Length determination submodule, for default single message-length of the length and described Boost message queue of determining described data;
Data slicer submodule, for when the length of described data is greater than described default single message-length, in units of described default single message-length, cuts into slices to described data, obtains multiple slice of data;
Data write submodule, for writing described Boost message queue by described multiple slice of data.
Alternatively, described data write submodule comprises:
Be sequentially written in unit, for according to section order, described multiple slice of data write described Boost message queue.
Alternatively, described message queue writing module also comprises:
Number write submodule, for cutting into slices to described data, after obtaining multiple slice of data, is adding up the number of described slice of data, and the described number of statistics is being write described Boost message queue.
Alternatively, described process data extraction module comprises:
Data extract submodule, for when receiving the described Boost message queue with data, extract slice of data wherein, until the number of the slice of data extracted equals the described number of adding up;
The data that splicing obtains for splicing the slice of data extracted, and are sent to the second process by data splicing submodule.
From above scheme, the beneficial effect of the application is:
The invention provides a kind of inter-process communication methods and device, the method comprises: when the first process generates data, obtain the Boost message queue be pre-created, write direct data Boost message queue, when the receiver module that the second process end is arranged receives the Boost message queue with data, extract data wherein, and the data of extraction are sent to the second process.Compared with prior art, the data of the first process are not directly put into shared drive by the embodiment of the present invention, but data are write in Boost message queue, utilize Boost message queue to realize the first process by data communication to the second process.Thus do not need on stream to control read-write stationary problem, performance history is simpler.
Embodiment
Below in conjunction with the accompanying drawing in the embodiment of the present invention, be clearly and completely described the technical scheme in the embodiment of the present invention, obviously, described embodiment is only the present invention's part embodiment, instead of whole embodiments.Based on the embodiment in the present invention, those of ordinary skill in the art, not making the every other embodiment obtained under creative work prerequisite, belong to the scope of protection of the invention.
With reference to Fig. 1, it has disclosed the flow process of the inter-process communication methods that the embodiment of the present invention provides, and specifically comprises following flow process:
Step S101: when the first process generates data, obtain the Boost message queue be pre-created.
Wherein, when the first process generates data and needs these data to be sent to the second process, then the Boost message queue be pre-created is obtained.Boost message queue is used for sending data to the second process.
Step S102: described data are write described Boost message queue.
In the present embodiment, write direct data Boost message queue, and self can the realizing of Boost message queue sends data to the second process end, and wherein, the second process end is previously provided with receiver module, for performing next step.
Step S103: when receiving the described Boost message queue with data, the Data Concurrent extracted wherein delivers to the second process.
Wherein, when the receiver module of the second process end receives the Boost message queue with data, receiver module extracts data wherein, and the data extracted are sent to the second process, thus first process that completes sends the process of data to the second process.As shown in Figure 2, the first process end is provided with sending module, Boost message queue calls the transmission that sending module realizes data, and the second process end is provided with receiver module, and receiver module realizes the reception of data, thus completes the communication of different process.
In the present embodiment, whether receiver module can have message queue to be sent to self by Real-Time Monitoring, or receiver module is not monitored, but message queue triggers receiver module after arriving, then the receiver module be triggered extracts data wherein.
From above technical scheme, the inter-process communication methods that the present embodiment provides comprises: when the first process generates data, obtain the Boost message queue be pre-created, directly data are write in Boost message queue, the mechanism of Boost message queue self is utilized to realize data to mail to the second process end, when the receiver module that the second process end is arranged receives the Boost message queue with data, extract data wherein, and the data of extraction are sent to the second process.
Briefly, the embodiment of the present invention creates Boost message queue at the first process end with WriteMode, opens this message queue in a read mode at the second process end.First process end writes data in Boost message queue, and the second process end reads data wherein.Compared with prior art, the data of the first process are not directly put into shared drive by the embodiment of the present invention, but utilize Boost message queue to send data to the second process.Like this, the embodiment of the present invention does not need read-write synchronous operation when accessing shared drive between control process on stream, and performance history is simpler.
It should be noted that, two processes are in same equipment, and do not limit the Platform Type of equipment use, can be Linux platform, also can be windows platforms.In addition, the first process can be calling data to the process data that the second process sends, and this calling data triggers the second process and sends process data to the first process, thus realizes the first process calling the second process.Certainly, the mode that the second process sends process data to the first process can be the inter-process communication methods that the embodiment of the present invention provides.
When creating message queue, need to arrange the length of single message in message queue.When memory amount is less and performance requirement is lower, can arrange less message-length value, need to illustrate, the concrete numerical value of message-length can be determined according to the process interaction size of data of reality, and in the present invention, each embodiment does not limit.When the message-length arranged is less, and when data volume to be sent is larger, need data to cut into slices.Also namely, as shown in Figure 3, in above-described embodiment, specific implementation data being write Boost message queue can be:
Step S201: determine the length of described data and default single message-length of described Boost message queue.
Wherein, when creating Boost message queue, the length value of single message is pre-set, in order to the size of message each in restriction of message queue.
Step S202: when the length of described data is greater than described default single message-length, in units of described default single message-length, cuts into slices to described data, obtains multiple slice of data.
That is, when the length of data is larger, in units of default single message-length, be multiple data by data cutting.Such as, single message-length is 10K, and data are 200K size, be then the slice of data of 20 10K sizes by data cutting.
It should be noted that, even if when the last part data of cutting are less than single message-length, also using this partial data as a slice of data.Such as, single message-length is 10K, and process message is 205K size, be then 21 slice of datas by data cutting, comprising the slice of data of a 5K.
Step S203: described multiple slice of data is write described Boost message queue.
Wherein, corresponding sequence identification can be set for each slice of data, by multiple slice of data write Boost message queue being provided with sequence identification.Or direct basis section order, by multiple slice of data write Boost message queue.Multiple section processes in same message queue have identical process identification (PID), and this process identification (PID) is mark i.e. first process identification (PID) of transmission process.
In this kind of implementation, in message queue, comprise multiple slice of data.After second process end receiver module receives message queue, need to extract multiple slice of data successively, and be spliced into complete data.In order to ensure that receiver module accurately extracts whole slice of datas, the number of slice of data can be write in Boost message queue.That is:
Described data are being cut into slices, after obtaining multiple slice of data, is adding up the number of described slice of data, and the described number of statistics is being write described Boost message queue.
Therefore, the number of slice of data is included in the Boost message queue of encapsulation.When the second process termination receives Boost message queue, according to the number of the slice of data extracted, the extraction of data can be carried out.Particularly:
When receiving the described Boost message queue with data, extract slice of data wherein, until the number of the slice of data extracted equals the described number of adding up; Splice the slice of data extracted, and the data that splicing obtains are sent to the second process.
If slice of data has sequence identification, then the slice of data extracted is identified in order and splice; If slice of data there is no mark, but when being sequentially written in Boost message queue according to section, can extract successively and splice successively, the data that splicing obtains be complete data.
Below the interprocess communication device that the embodiment of the present invention provides is described, it should be noted that, below can be cross-referenced with inter-process communication methods above to the introduction of the interprocess communication device that the embodiment of the present invention provides.
With reference to Fig. 4, it illustrates the structure of the interprocess communication device that the embodiment of the present invention provides, specifically comprise: message queue acquisition module 100, message queue writing module 200 and process data extraction module 300; Wherein:
Message queue acquisition module 100, for when the first process generates data, obtains the Boost message queue be pre-created;
Message queue writing module 200, for writing described Boost message queue by described data;
Process data extraction module 300, for when receiving the described Boost message queue with data, the Data Concurrent extracted wherein delivers to the second process.
From above technical scheme, in the interprocess communication device that the present embodiment provides, when the first process generates data, message queue acquisition module 100 obtains the Boost message queue be pre-created, data directly write in Boost message queue by message queue writing module 200, when receiving the Boost message queue with data, process data extraction module 300 extracts data wherein, and the data of extraction are sent to the second process.Compared with prior art, the data of the first process are not put into shared drive by the embodiment of the present invention, but utilize Boost message queue directly to send data to the second process.Like this, the embodiment of the present invention does not need read-write synchronous operation when accessing shared drive between process, and development scheme is simpler.
Need explanation, message queue acquisition module 100 in the present embodiment, message queue writing module 200 can think the sending module in said method embodiment, be integrated in the first process end, for sending the data that the first process generates, process data extraction module 300 can think the receiver module in said method embodiment, be integrated in the second process end, for receiving data.Certainly, the first process end and the second process end can all arrange sending module and receiver module, then each process both can send data, also can receive data.
As shown in Figure 5, wherein, the message queue writing module 200 in above-mentioned interprocess communication device can comprise: length determination submodule 201, data slicer submodule 202 and data write submodule 203.Wherein:
Length determination submodule 201, for default single message-length of the length and described Boost message queue of determining described data;
Data slicer submodule 202, for when the length of described data is greater than described default single message-length, in units of described default single message-length, cuts into slices to described data, obtains multiple slice of data;
Data write submodule 203, for writing described Boost message queue by described multiple slice of data.
Wherein, the data write submodule 203 in above-mentioned interprocess communication device can comprise:
Be sequentially written in unit, for according to section order, described multiple slice of data write described Boost message queue.
As shown in Figure 5, alternatively, the message queue writing module 200 in above-mentioned interprocess communication device can also comprise:
Number write submodule 204, for cutting into slices to described data, after obtaining multiple slice of data, is adding up the number of described slice of data, and the described number of statistics is being write described Boost message queue.
Alternatively, the process data extraction module 300 in above-mentioned interprocess communication device can comprise:
Data extract submodule, for when receiving the described Boost message queue with data, extract slice of data wherein, until the number of the slice of data extracted equals the described number of adding up;
The data that splicing obtains for splicing the slice of data extracted, and are sent to the second process by data splicing submodule.
It should be noted that, each embodiment in this instructions all adopts the mode of going forward one by one to describe, and what each embodiment stressed is the difference with other embodiments, between each embodiment identical similar part mutually see.
Also it should be noted that, in this article, the such as relational terms of first and second grades and so on is only used for an entity or operation to separate with another entity or operational zone, and not necessarily requires or imply the relation that there is any this reality between these entities or operation or sequentially.And, term " comprises ", " comprising " or its any other variant are intended to contain comprising of nonexcludability, thus make to comprise the process of a series of key element, method, article or equipment and not only comprise those key elements, but also comprise other key elements clearly do not listed, or also comprise by the intrinsic key element of this process, method, article or equipment.When not more restrictions, the key element limited by statement " comprising ... ", and be not precluded within process, method, article or the equipment comprising above-mentioned key element and also there is other identical element.
To the above-mentioned explanation of the disclosed embodiments, professional and technical personnel in the field are realized or uses the present invention.To be apparent for those skilled in the art to the multiple amendment of these embodiments, General Principle as defined herein can without departing from the spirit or scope of the present invention, realize in other embodiments.Therefore, the present invention can not be restricted to these embodiments shown in this article, but will meet the widest scope consistent with principle disclosed herein and features of novelty.