Patent Description:
Interprocess communication (Interprocess Communication, IPC) is a communication transmission technology that is mainly used to transmit a message between different processes in a computer system. Currently, main mechanisms include pipeline communication, Socket, and shared memory.

A message communication mechanism in an existing terminal operating system such as an Android (Android™) system includes a binder communication mechanism, and the binder communication mechanism is an IPC communication mechanism. In principle, the binder communication mechanism is also a communication mechanism based on a C/S architecture, except that an internal proxy is used for communication in binder communication, so that a binder client seems to perform an operation locally during communication. In addition, during communication, a binder only needs to know a name of a peer service without a need to know a port of the service.

The binder communication mechanism in Android is indeed very secure and fast, but also has the following disadvantages: (<NUM>) Communication performance of the binder is related to a length of data carried in the binder. In addition, a binder driver needs to copy, to second process space, data carried by a first process of the binder. This is also time-consuming, and a time required increases as a data volume increases. (<NUM>) Because a kernel driver needs to copy the data of the first process, specific memory needs to be managed, for storing the data. Due to a limited communication information length, the kernel driver needs to allocate additional memory to assist in communication. <CIT> describes a shared memory region <NUM> partitioned or divided into multiple zones. Each zone is owned by a single process. The multiple processes are able to perform both read access and write access of a zone owned by a particular process. Each process can write data to a shared zone using a direct memory write access, and another process can read the data written to the shared zone using a direct memory read access. <CIT> describes a process-to-process communication method comprising building a first shared memory and a second shared memory according to identification of a first process and identification of a second process, wherein the identification is used for uniquely identifying one process; and conducting communication between the first process and the second process through the first shared memory and the second shared memory. <CIT> describes a shared memory region <NUM> partitioned or divided into multiple zones. Each zone is owned by a single process. The multiple processes are able to perform both read access and write access of a zone owned by a particular process. Each process can write data to a shared zone using a direct memory write access, and another process can read the data written to the shared zone using a direct memory read access. <CIT> describes a process-to-process communication method comprising building a first shared memory and a second shared memory according to identification of a first process and identification of a second process, wherein the identification is used for uniquely identifying one process; and conducting communication between the first process and the second process through the first shared memory and the second shared memory.

The present invention provides an interprocess communication method and an interprocess communications system, to resolve a delay issue caused by memory copy, allocation, and release and bypass a restriction imposed by a binder communication mechanism on a volume of carried data.

According to a first aspect, an interprocess communication method is provided, and is used to transmit communication data from a first process to a second process. The method includes: determining whether a data length of the communication data of the first process is less than a size of first memory space, where the communication data is used for data exchange between the first process and the second process, and the first memory space is memory space allocated to the first process for access; when the length of the communication data is less than the size of the first memory space, storing the communication data in the first memory space; allocating virtual memory space to the second process, where the virtual memory space is used by the second process to access the communication data; and mapping a physical memory page in the first memory space to the virtual memory space. This resolves a delay issue caused by memory copy, allocation, and release and bypasses a restriction imposed by a binder communication mechanism in Android on a volume of carried data.

In a possible implementation, the method includes: unmapping the physical memory page in the first memory space from the virtual address space, to release the virtual address space. In this implementation, a virtual address can be reclaimed.

The method includes: when the length of the communication data is greater than the size of the first memory space, storing the communication data as a data file, where the second process accesses the communication data by using metadata of the data file.

In a possible implementation, the storing the communication data as a data file, where the second process accesses the communication data by using metadata of the data file specifically includes: storing the communication data in second memory space outside the first memory space; mapping the second memory space to the data file; and storing the metadata of the data file in the first memory space.

In a possible implementation, the method includes: adjusting an address of the communication data; and sending a message to the second process, where the message includes the metadata of the data file.

In a possible implementation, the metadata of the data file includes a file descriptor, and before the sending a message to the second process, the method further includes: determining the corresponding second process based on control information corresponding to the file descriptor, and converting the file descriptor into a file descriptor that can be identified by the second process.

In a possible implementation, after interprocess data exchange is performed in a selected communication manner, the method further includes: determining a communication status of the data file based on a communication status identifier of the data file; and in response to a fact that communication has been completed for the data file, storing file information of the data file in a first cache list, where the file information is used as to-be-used file information for a next time. In this implementation, a file used at the end of reading by the second process is reserved and is used as a backup for a next process.

In a possible implementation, the storing the communication data as a data file, where the second process accesses the communication data by using metadata of the data file specifically includes: storing the communication data in second memory space outside the first memory space; sending the metadata of the data file to a kernel; allocating virtual memory space to the second process, where the virtual memory space is used by the second process to access the communication data; and mapping physical memory address space of the data file to the virtual memory space based on the metadata.

According to another aspect, an interprocess communication method is provided, and the method includes: storing communication data of a first process in first memory space, where the communication data is used for data exchange between the first process and a second process, and the first memory space is memory space allocated to the first process for access; allocating virtual memory space to the second process, where the virtual memory space is used by the second process to access the communication data; and mapping a physical memory page in the first memory space to the virtual memory space.

Before the storing communication data of a first process in first memory space, the method further includes: determining that a data length of the communication data is less than a size of the first memory space.

According to another aspect, an embodiment of the present invention provides an interprocess communications system, configured to transmit communication data from a first process to a second process. The system includes: a selection module, configured to determine whether a data length of the communication data of the first process is less than a size of first memory space, where the communication data is used for data exchange between the first process and the second process, and the first memory space is memory space allocated to the first process for access; a processing module, configured to: when the length of the communication data is less than the size of the first memory space, store the communication data in the first memory space; and a configuration module, configured to allocate virtual memory space to the second process, where the virtual memory space is used by the second process to access the communication data, where the processing module is further configured to map a physical memory page in the first memory space to the virtual memory space.

According to another aspect, an interprocess communication method is provided, and is used to transmit communication data from a first process to a second process. The method includes: performing initialization configuration on the first process and the second process, including creating first memory space in shared memory space; selecting a communication manner based on a length of the communication data and a value of a threshold, where the communication data includes data and a message, and the threshold is a size of the first memory space; and performing interprocess data exchange in the selected communication manner. This resolves a delay issue caused by memory copy, allocation, and release and bypasses a restriction imposed by a binder communication mechanism in Android on a volume of carried data.

In a possible implementation, the method includes: selecting, in response to a fact that the length of the communication data is less than the threshold, a memory sharing manner for communication, where in the memory sharing manner, the communication data is transmitted by using the first memory space.

In a possible implementation, the performing interprocess data exchange in the selected communication manner includes: allocating virtual address space to the second process; and mapping a physical memory page in the first memory space to the virtual address space of the second process. In this implementation, a read/write function is enabled by using shared memory of the first process, so that memory is fully shared between the first process and the second process, thereby resolving a delay issue caused by memory copy, allocation, and release.

In a possible implementation, the performing interprocess data exchange in the selected communication manner further includes: sending the message to the second process, to trigger the second process to run.

In a possible implementation, after the performing interprocess data exchange in the selected communication manner, the method further includes: unmapping the physical memory page in the first memory space from the virtual address space, to release the virtual address space. In a possible implementation, the selecting a communication manner based on a length of the communication data and a value of a threshold includes: selecting, in response to a fact that the length of the communication data is greater than or equal to the threshold, a data file manner for communication, where in the data file manner, the communication data is stored as a data file, and metadata of the data file is transmitted by using the first memory space. In this implementation, a restriction imposed by a communication mechanism on a volume of carried data is bypassed.

In a possible implementation, the selecting, in response to a fact that the length of the communication data is greater than or equal to the threshold, a data file manner for communication includes: storing the communication data in second memory space outside the shared memory space; mapping the second memory space to the data file; and storing the metadata of the data file in the first memory space. In this implementation, a restriction imposed by a communication mechanism on a volume of carried data is bypassed.

In a possible implementation, before the storing the communication data in second memory space outside the shared memory space, the method further includes: determining a to-be-used data file based on a first cache list that is used to store to-be-used file information; determining, based on the to-be-used data file, to-be-used shared memory corresponding to the to-be-used data file; determining whether the to-be-used shared memory includes to-be-used shared memory that meets the length of the communication data; and in response to a fact that the to-be-used shared memory includes to-be-used shared memory that meets the length of the communication data, using, as the second memory space, the to-be-used shared memory that meets the length of the communication data; or in response to a fact that the to-be-used shared memory does not include to-be-used shared memory that meets the length of the communication data, creating, outside the shared memory space, shared memory that is used as the second memory space and that meets the length of the communication data. In this implementation, the second memory space is created outside the shared memory space, and therefore, is not restricted by a size of the original shared memory space, thereby bypassing a restriction imposed by a communication mechanism on a volume of carried data.

In a possible implementation, the data file includes a communication status identifier, the communication status identifier is used to indicate a communication status of the data file, and before the determining a to-be-used data file based on a first cache list that is used to store to-be-used file information, the method further includes: traversing a second cache list, where the second cache list is used to store file information of a data file in communication; determining, based on the communication status identifier, whether the second cache list includes file information corresponding to a data file for which communication has been completed; and in response to a fact that the second cache list includes the file information corresponding to the data file for which communication has been completed, storing, in the first cache list, the file information that is in the second cache list and that is corresponding to the data file for which communication has been completed, where the file information is used as the to-be-used file information. In this implementation, it can be ensured that the data file, for which communication has been completed, corresponding to the file information that is in the second cache list and that has not been stored in the first cache list in time is also included in a selection range of a second cache, thereby improving cache resource utilization.

In a possible implementation, file information in the first cache list is sorted based on a size of corresponding to-be-used shared memory, and the determining whether the to-be-used shared memory includes to-be-used shared memory that meets the length of the communication data includes: successively performing determining starting from smallest to-be-used shared memory in the first cache list. In this implementation, smallest shared memory in shared memory that meets the length of the communication data and that is corresponding to a file descriptor in the first cache list is used with priority, to save space.

In a possible implementation, the storing the metadata of the data file in the first memory space includes: determining whether the first memory space is sufficient to store the metadata of the data file; and in response to a fact that the first memory space is insufficient to store the metadata of the data file, expanding the first memory space to be sufficient to store the metadata of the data file; or in response to a fact that the first memory space is sufficient to store the metadata of the data file, replacing original data in the first memory space with the metadata of the data file. In this implementation, the metadata of the data file is stored by using the existing first memory space, thereby saving space.

In a possible implementation, the performing interprocess data exchange in the selected communication manner includes: exchanging data by transmitting the metadata of the data file. In this implementation, data is shared by transmitting the metadata.

In a possible implementation, the exchanging data by transmitting the metadata of the data file includes: adjusting an address of the communication data; and sending a message to the second process, where the message is used to trigger the second process, and the message includes the metadata of the data file.

In a possible implementation, triggering the second process includes: triggering the second process to restore data in the data file based on the file descriptor that can be identified by the second process and the corresponding control information.

In a possible implementation, after the performing interprocess data exchange in the selected communication manner, the method further includes: determining a communication status of the data file based on a communication status identifier of the data file; and in response to a fact that communication has been completed for the data file, storing file information of the data file in a first cache list, where the file information is used as to-be-used file information for a next time. In this implementation, a file used at the end of reading by the second process is reserved and is used as a backup for a next process.

In a possible implementation, the storing file information of the data file in the second cache list, where the file information is used as to-be-used file information for a next time further includes: determining whether a volume of to-be-used file information stored in the first cache list is greater than a threshold of the first cache list, where the threshold of the first cache list is a maximum value of a volume of to-be-used file information that can be accommodated in the first cache list; and in response to a fact that the volume of to-be-used file information stored in the first cache list is greater than the threshold of the first cache list, determining, based on the to-be-used file information in the first cache list, a to-be-used data file corresponding to the to-be-used file information; determining, based on the to-be-used data file, to-be-used shared memory corresponding to the to-be-used data file; releasing smallest to-be-used shared memory in the to-be-used shared memory; and deleting, from the first cache list, to-be-used file information corresponding to the smallest to-be-used shared memory. In this implementation, an upper limit of a stored volume of to-be-used file information is set in the first cache list. When the stored volume reaches the upper limit, the smallest memory is released. This ensures that memory is not occupied because there is excessive shared cache memory, and can also improve cache utilization.

According to another aspect, an embodiment of the present invention provides an interprocess communications system, configured to transmit communication data from a first process to a second process, and the system includes a configuration module, a selection module, and a processing module. The configuration module is configured to perform initialization configuration on the first process and the second process, where the initialization configuration includes: creating first memory space in shared memory space. The selection module is configured to select a communication manner based on a length of the communication data and a value of a threshold, where the communication data includes data and a message, and the threshold is a size of the first memory space. The processing module is configured to perform interprocess data exchange in the selected communication manner. The system can implement a function performed in the method design in the first aspect. The function may be implemented by hardware, or may be implemented by hardware executing corresponding software. The hardware or the software includes one or more modules corresponding to the foregoing function.

According to another aspect, an embodiment of the present invention provides a computer storage medium, and the computer storage medium stores an instruction. When running on a computer, the instruction enables the computer to perform the method according to any one of the first aspect or the possible designs of the first aspect.

According to another aspect, an embodiment of the present invention provides a computer program product including instructions. When the instructions are executed by a computer, the instructions enable the computer to perform the method according to any one of the first aspect or the possible designs of the first aspect.

According to an interprocess communication method provided in an embodiment of the present invention, there is no need to reallocate a memory address in a second process to store data of a first process; therefore, there is no need to release memory after interprocess communication is completed, thereby resolving a delay issue caused by memory copy, allocation, and release.

An embodiment of the present invention further provides a terminal, and the terminal may include at least a memory, a transceiver, and a processor. The memory may be configured to store a software program, the processor performs various functions of the terminal by running the software program stored in the memory, and the transceiver may be configured to receive and transmit a notification message.

The following further describes a structure of the terminal by using <FIG> as an example. <FIG> is a schematic structural diagram of a mobile phone according to an embodiment of the present invention. Referring to <FIG>, the mobile phone <NUM> includes components such as a transceiver <NUM>, a memory <NUM>, other input devices <NUM>, a touchscreen <NUM>, a sensor <NUM>, an audio circuit <NUM>, an I/O subsystem <NUM>, a processor <NUM>, and a power supply <NUM>. A person skilled in the art may understand that a structure of the mobile phone shown in <FIG> imposes no limitation on the mobile phone, and the mobile phone may include more or fewer components than those shown in the figure or may have a different component arrangement, or some components may be combined, or some components may be split.

The following describes each constituent component of the mobile phone <NUM> in detail with reference to <FIG>.

The transceiver <NUM> may be configured to: receive and send a signal in an information receiving/transmitting process or a call process, and in particular, after receiving downlink information from a base station, send the downlink information to the processor <NUM> for processing; and in addition, send related uplink data to the base station. Usually, the transceiver <NUM> includes but is not limited to an antenna, at least one amplifier, a transceiver, a coupler, an LNA (Low Noise Amplifier, low noise amplifier), a duplexer, and the like. In addition, the transceiver <NUM> may further communicate with a network and another device through wireless communication. The wireless communication may use any communications standard or protocol, including but not limited to GSM (Global System for Mobile Communications, Global System for Mobile Communications), GPRS (General Packet Radio Service, general packet radio service), CDMA (Code Division Multiple Access, Code Division Multiple Access), WCDMA (Wideband Code Division Multiple Access, Wideband Code Division Multiple Access), LTE (Long Term Evolution, Long Term Evolution), an email, an SMS (Short Message Service, short message service), and the like.

The memory <NUM> may be configured to store a software program, and the processor <NUM> performs various functions of the mobile phone <NUM> by executing the software program stored in the memory <NUM>. The memory <NUM> may mainly include a program storage area and a data storage area. The program storage area may store an operating system, an application program required by at least one function (such as a sound playing function or an image playing function), and the like; and the data storage area may store data (such as audio data or an address book) maintained based on use of the mobile phone <NUM>, and the like. In addition, the memory <NUM> may include a high-speed random access memory, and may further include a nonvolatile memory, such as at least one magnetic disk storage device, a flash device, or another volatile solid-state storage device.

The other input devices <NUM> may be configured to: receive input number or character information, and generate key signal input related to a user setting and function control of the mobile phone <NUM>. Specifically, the other input devices <NUM> may include but are not limited to one or more of a physical keyboard, a function key (such as a volume control key or an on/off key), a trackball, a mouse, a joystick, an optical mouse (where the optical mouse is a touch-sensitive surface that does not display visual output, or an extension of a touch-sensitive surface formed by a touchscreen), and the like. The other input devices <NUM> are connected to another input device controller <NUM> of the I/O subsystem <NUM>, and exchange a signal with the processor <NUM> under control of the another input device controller <NUM>.

The touchscreen <NUM> may be configured to display information entered by a user or information provided to a user, and various menus of the mobile phone <NUM>, and may further receive user input. Specifically, the touchscreen <NUM> may include a display panel <NUM> and a touch panel <NUM>. The display panel <NUM> may be configured in a form of an LCD (Liquid Crystal Display, liquid crystal display), an OLED (Organic Light-Emitting Diode, organic light-emitting diode), or the like. The touch panel <NUM>, also referred to as a display, a touch-sensitive screen, or the like, can collect a contact or non-contact operation (for example, an operation performed by the user on the touch panel <NUM> or near the touch panel <NUM> by using any proper object or accessory such as a finger or a stylus, or a motion sensing operation, where the operation includes an operation type such as a single-point control operation or a multipoint control operation) performed by the user on or near the touch panel <NUM>, and drive a corresponding connection apparatus based on a preset program. Optionally, the touch panel <NUM> may include two parts: a touch detection apparatus and a touch controller. The touch detection apparatus detects a gesture of the user, namely, a touch orientation or gesture, detects a signal brought by the touch operation, and transmits the signal to the touch controller. The touch controller receives touch information from the touch detection apparatus, converts the touch information into information that can be processed by the processor, and then sends the information to the processor <NUM>. The touch controller can receive and execute a command sent by the processor <NUM>. In addition, the touch panel <NUM> may be implemented in a plurality of form, such as a resistive form, a capacitive form, an infrared ray, and a surface acoustic wave, or the touch panel <NUM> may be implemented by using any technology developed in the future. Further, the touch panel <NUM> may cover the display panel <NUM>, and the user may perform, based on content displayed on the display panel <NUM> (the displayed content includes but is not limited to a soft keyboard, a virtual mouse, a virtual key, an icon, or the like), an operation on or near the touch panel <NUM> that covers the display panel <NUM>. After detecting the operation on or near the touch panel <NUM>, the touch panel <NUM> transmits the operation to the processor <NUM> by using the I/O subsystem <NUM> to determine the user input. Then, the processor <NUM> provides corresponding visual output on the display panel <NUM> by using the I/O subsystem <NUM> based on the user input. Although in <FIG>, the touch panel <NUM> and the display panel <NUM> implement input and output functions of the mobile phone <NUM> as two independent components, in some embodiments, the touch panel <NUM> and the display panel <NUM> may be integrated to implement the input and output functions of the mobile phone <NUM>.

The mobile phone <NUM> may further include at least one sensor <NUM>, such as a light sensor, a motion sensor, and another sensor. Specifically, the light sensor may include an ambient light sensor and a proximity sensor. The ambient light sensor may adjust luminance of the display panel <NUM> based on brightness of ambient light, and the proximity sensor may disable the display panel <NUM> and/or backlight when the mobile phone <NUM> approaches an ear. As a motion sensor, an accelerometer sensor can detect a value of acceleration in each direction (usually, three axes), can detect a value and a direction of gravity in a static state, and can be used in an application for identifying a mobile phone posture (such as switching between a landscape orientation and a portrait orientation, a related game, and magnetometer posture calibration), a function related to vibration identification (such as a pedometer and a strike), and the like. Other sensors such as a gyroscope, a barometer, a hygrometer, a thermometer, and an infrared sensor may also be disposed in the mobile phone <NUM>.

The audio circuit <NUM>, a speaker <NUM>, and a microphone <NUM> may provide an audio interface between the user and the mobile phone <NUM>. The audio circuit <NUM> may convert received audio data into a signal and transmit the signal to the speaker <NUM>, and the speaker <NUM> converts the signal into a sound signal for output. In addition, the microphone <NUM> converts a collected sound signal into a signal, and the audio circuit <NUM> receives the signal and converts the signal into audio data, and outputs the audio data to the transceiver <NUM> to send the audio data to, for example, another mobile phone, or outputs the audio data to the memory <NUM> for further processing.

The I/O subsystem <NUM> is configured to control an external input/output device, and may include the another input device controller <NUM>, a sensor controller <NUM>, and a display controller <NUM>. Optionally, one or more other input device controllers <NUM> receive a signal from the other input devices <NUM> and/or send a signal to the other input devices <NUM>. The other input devices <NUM> may include a physical button (a press button, a rocker button, or the like), a dial pad, a slider switch, a joystick, a click scroll wheel, and an optical mouse (the optical mouse is a touch-sensitive surface that does not display visual output, or an extension of a touch-sensitive surface formed by a touchscreen). It should be noted that the another input device controller <NUM> may be connected to any one or more of the foregoing devices. The display controller <NUM> in the I/O subsystem <NUM> receives a signal from the touchscreen <NUM> and/or sends a signal to the touchscreen <NUM>. After the touchscreen <NUM> detects the user input, the display controller <NUM> converts the detected user input into interaction with a user interface object displayed on the touchscreen <NUM>, to implement man-machine interaction. The sensor controller <NUM> may receive a signal from the one or more sensors <NUM> and/or send a signal to the one or more sensors <NUM>.

The processor <NUM> is a control center of the mobile phone <NUM>. The processor <NUM> connects parts of the whole mobile phone by using various interfaces and lines, and performs various functions of the mobile phone <NUM> and processes data by running or executing the software program and/or a module that are/is stored in the memory <NUM> and by invoking data stored in the memory <NUM>, thereby performing overall monitoring on the mobile phone. Optionally, the processor <NUM> may include one or more processing units. The processor <NUM> may integrate an application processor and a modem processor. The application processor mainly processes an operating system, a user interface, an application program, and the like. The modem processor mainly processes wireless communication. It can be understood that, alternatively, the modem processor may not be integrated into the processor <NUM>.

The mobile phone <NUM> further includes the power supply <NUM> (such as a battery) that supplies power to each component. Preferably, the power supply may be logically connected to the processor <NUM> by using a power management system, to manage functions such as charging, discharging, and power consumption by using the power management system.

Although not shown, the mobile phone <NUM> may further include a camera, a Bluetooth module, and the like.

Operation processes of the components are as follows:.

The processor <NUM> performs initialization configuration on a first process and a second process, where the initialization configuration includes: creating first memory space in shared memory space; selects a communication manner based on a length of communication data of the first process and a value of a threshold, where the communication data includes data and a message, and the threshold is a size of the first memory space; and performs interprocess data exchange in the selected communication manner.

Optionally, the processor <NUM> selects, in response to a fact that the length of the communication data is less than the threshold, a memory sharing manner for communication, where in the memory sharing manner, the communication data is transmitted by using the first memory space.

Optionally, the processor <NUM> allocates virtual address space to the second process; and maps a physical memory page in the first memory space to the virtual address space of the second process.

Optionally, the transceiver <NUM> sends the message to the second process, to trigger the second process to run.

Optionally, the processor <NUM> unmaps the physical memory page in the first memory space from the virtual address space, to release the virtual address space.

Optionally, the processor <NUM> selects, in response to a fact that the length of the communication data is greater than or equal to the threshold, a data file manner for communication, where in the data file manner, the communication data is stored as a data file, and metadata of the data file is transmitted by using the first memory space.

Optionally, the processor <NUM> stores the communication data in second memory space outside the shared memory space; maps the second memory space to the data file; and stores the metadata of the data file in the first memory space.

Optionally, the processor <NUM> determines a to-be-used data file based on a first cache list that is used to store to-be-used file information; determines, based on the to-be-used data file, to-be-used shared memory corresponding to the to-be-used data file; determines whether the to-be-used shared memory includes to-be-used shared memory that meets the length of the communication data; and in response to a fact that the to-be-used shared memory includes to-be-used shared memory that meets the length of the communication data, uses, as the second memory space, the to-be-used shared memory that meets the length of the communication data; or in response to a fact that the to-be-used shared memory does not include to-be-used shared memory that meets the length of the communication data, creates, outside the shared memory space, shared memory that is used as the second memory space and that meets the length of the communication data.

Optionally, the processor <NUM> traverses a second cache list, where the second cache list is used to store file information of a data file in communication; determines, based on the communication status identifier, whether the second cache list includes file information corresponding to a data file for which communication has been completed; and in response to a fact that the second cache list includes the file information corresponding to the data file for which communication has been completed, stores, in the first cache list, the file information that is in the second cache list and that is corresponding to the data file for which communication has been completed, where the file information is used as to-be-used file information.

Optionally, the processor <NUM> successively performs determining starting from smallest to-be-used shared memory in the first cache list.

Optionally, the processor <NUM> determines whether the first memory space is sufficient to store the metadata of the data file; and in response to a fact that the first memory space is insufficient to store the metadata of the data file, expands the first memory space to be sufficient to store the metadata of the data file; or in response to a fact that the first memory space is sufficient to store the metadata of the data file, replaces original data in the first memory space with the metadata of the data file.

Optionally, the processor <NUM> exchanges data by transmitting the metadata of the data file.

Optionally, the processor <NUM> adjusts an address of the communication data.

Optionally, the transceiver <NUM> sends the message to the second process, where the message is used to trigger the second process, and the message includes the metadata of the data file.

Optionally, the processor <NUM> determines the corresponding second process based on control information corresponding to the file descriptor, and converts the file descriptor into a file descriptor that can be identified by the second process.

Optionally, the processor <NUM> triggers the second process to restore data in the data file based on the file descriptor that can be identified by the second process and the corresponding control information.

Optionally, the processor <NUM> determines a communication status of the data file based on the communication status identifier of the data file; and in response to a fact that communication has been completed for the data file, stores file information of the data file in the first cache list, where the file information is used as to-be-used file information for a next time.

Optionally, the processor <NUM> determines whether a volume of to-be-used file information stored in the first cache list is greater than a threshold of the first cache list, where the threshold of the first cache list is a maximum value of a volume of to-be-used file information that can be accommodated in the first cache list; and in response to a fact that the volume of to-be-used file information stored in the first cache list is greater than the threshold of the first cache list, determines, based on the to-be-used file information in the first cache list, a to-be-used data file corresponding to the to-be-used file information; determines, based on the to-be-used data file, to-be-used shared memory corresponding to the to-be-used data file; releases smallest to-be-used shared memory in the to-be-used shared memory; and deletes, from the first cache list, to-be-used file information corresponding to the smallest to-be-used shared memory.

<FIG> is a flowchart of an interprocess communication method according to an embodiment of the present invention. The method includes the following steps:.

In a specific embodiment, in this method, in a memory sharing manner, a binder driver performs interprocess data exchange by remapping data; and in a data file manner, the binder driver performs interprocess data exchange by transmitting metadata of a data file, to implement interprocess communication.

In an embodiment, when the length of the communication data is less than a threshold, a solution using a memory sharing mechanism is selected. In this solution, a read/write function is enabled by using shared memory of the first process, so that memory is fully shared between the first process and the second process. The following describes the solution in this embodiment with reference to accompanying drawings.

<FIG> is a schematic flowchart of a method for performing initialization configuration on a first process and a second process according to an embodiment of the present invention.

In an example, with reference to <FIG> and <FIG>, correspondingly, in step <NUM>, initialization configuration on the first process and the second process includes process initialization in user space and process initialization by a binder driver. Details are as follows:.

In an example, correspondingly, in step <NUM>, the length of the communication data and a value of the threshold are compared. A communication manner is selected based on a result of the comparison, and the threshold is the size of the first memory space allocated in step <NUM>. When the length of the communication data is less than the threshold, the memory sharing manner is selected for communication. In the memory sharing manner, the communication data is transmitted by using the first memory space.

In an example, correspondingly, in step <NUM>, that interprocess data exchange is performed in the selected communication manner includes:
allocating virtual address space to the second process, and mapping the physical memory page in the first memory space to the virtual address space of the second process, thereby implementing zero data copy by remapping data in shared memory to the second process.

In an example, correspondingly, in step <NUM>, a message is sent to the second process, to trigger the second process to run. In this way, the second process starts running, and directly reads data from the first memory space based on a remapped address, to exchange data with the first process.

After the interprocess data exchange is performed in the memory sharing manner, to reclaim a virtual address, the physical memory page in the first memory space is unmapped from the virtual address space, to release the virtual address space.

<FIG> is a schematic diagram of a memory sharing mechanism according to an embodiment of the present invention. It can be learned from <FIG> that, in a memory sharing solution, the first process writes data into the shared memory, and remaps the data in the shared memory to the second process by remapping the data, so that the second process directly reads the data from the shared memory, thereby implementing zero data copy. Because there is no need to reallocate a memory address in the second process to store data of the first process, there is no need to release memory after interprocess communication is completed, thereby resolving a delay issue caused by memory copy, allocation, and release.

In another embodiment, when the length of the communication data is greater than or equal to the threshold, a solution using a data file mechanism is selected. In this solution, to-be-transmitted data of the first process is stored by using a data file, to bypass a restriction on the length of the communication data. The following describes the solution in this embodiment with reference to accompanying drawings.

In an example, first, in step <NUM>, initialization configuration on the first process and the second process includes: creating the first memory space in the shared memory space.

In an example, correspondingly, in step <NUM>, that a communication manner is selected based on the length of the communication data and the value of the threshold includes:
selecting, in response to a fact that the length of the communication data reaches or exceeds the threshold, the data file manner for communication, where in the data file manner, the communication data is stored as a data file, and metadata of the data file is transmitted by using the first memory space.

<FIG> is a schematic flowchart of a method for selecting, in response to a fact that a length of communication data reaches or exceeds a threshold, a data file manner for communication according to an embodiment of the present invention. The schematic diagram shows substeps of step <NUM>. With reference to <FIG> and <FIG>, in step <NUM>, the selecting, in response to a fact that the length of the communication data reaches or exceeds the threshold, the data file manner for communication further includes the following steps:.

In an example, correspondingly, in step <NUM>, before the storing the communication data in second memory space outside shared memory space, the method further includes:.

The to-be-used file information includes a file descriptor, a file size, and a file start address.

Further, in an example, the data file includes a communication status identifier, the communication status identifier is used to indicate a communication status of the data file, and before the determining a to-be-used data file based on a first cache list that is used to store to-be-used file information, the method further includes: traversing a second cache list, where the second cache list is used to store file information of a data file in communication; determining, based on the communication status identifier, whether the second cache list includes file information corresponding to a data file for which communication has been completed; and in response to a fact that the second cache list includes the file information corresponding to the data file for which communication has been completed, storing, in the first cache list, the file information that is in the second cache list and that is corresponding to the data file for which communication has been completed, where the file information is used as the to-be-used file information. In this way, it can be ensured that the data file, for which communication has been completed, corresponding to the file information that is in the second cache list and that has not been stored in the first cache list in time is also included in a selection range of a second cache, thereby improving cache resource utilization.

Further, in an example, file information in the first cache list is sorted based on a size of corresponding to-be-used shared memory, and the determining whether the to-be-used shared memory includes to-be-used shared memory that meets the length of the communication data includes: successively performing determining starting from smallest to-be-used shared memory in the first cache list; and when determining that the to-be-used shared memory includes the to-be-used shared memory that meets the length of the communication data, stopping determining and using the determined shared memory as the second memory space. In this way, smallest shared memory in shared memory that meets the length of the communication data and that is corresponding to a file descriptor in the first cache list is used with priority, to save space.

In an example, correspondingly, in step <NUM>, the storing metadata of the data file in the first memory space includes: determining whether the first memory space is sufficient to store the metadata of the data file; and.

Based on step <NUM>, in an example, correspondingly, in step <NUM>, that interprocess data exchange is performed in the selected communication manner includes: exchanging data by transmitting the metadata of the data file.

<FIG> is a schematic flowchart of a method for selecting a data file manner for interprocess data exchange according to an embodiment of the present invention. The schematic diagram shows substeps of step <NUM>. With reference to <FIG> and <FIG>, correspondingly, in step <NUM>, the exchanging data by transmitting the metadata of the data file includes the following steps:.

In an example, correspondingly, in step <NUM>, the metadata of the data file includes a file descriptor, and before the sending a message to the second process, the method further includes:
determining the corresponding second process based on control information corresponding to the file descriptor, and converting the file descriptor into a file descriptor that can be identified by the second process.

In an example, correspondingly, in step <NUM>, triggering the second process includes:
triggering the second process to restore data in the data file based on the file descriptor that can be identified by the second process and the corresponding control information.

Based on step <NUM>, in an example, after data exchange is completed in the data file manner, the method includes: determining a communication status of the data file based on a communication status identifier of the data file; and in response to a fact that communication has been completed for the data file, storing, in a first cache list, file information of the data file for which communication is completed, where the file information is used as to-be-used file information for a next time. In this way, a file used at the end of reading by the second process is reserved and is used as a backup for a next process.

Further, in an example, the storing, in a first cache list, file information of the data file for which communication is completed, where the file information is used as to-be-used file information for a next time further includes:.

<FIG> is a schematic diagram of a data file mechanism according to an embodiment of the present invention. It can be learned from <FIG> that in a data file solution, to-be-transmitted data of the first process is stored by using a data file. In this process, a binder driver only needs to transmit metadata of the data file without a need to transmit the entire file, and data sharing can be implemented. In this manner, a restriction imposed by a binder communication mechanism on a volume of carried data can be bypassed.

<FIG> is a schematic structural diagram of an interprocess communications system according to an embodiment of the present invention.

The interprocess communications system provided in this embodiment of the present invention includes a configuration module <NUM>, a selection module <NUM>, and a processing module <NUM>. The configuration module <NUM> is configured to perform initialization configuration on a first process and a second process, where the initialization configuration includes: creating first memory space in shared memory space. The selection module <NUM> selects a communication manner based on a length of communication data and a value of a threshold, where the threshold is a size of the first memory space. The processing module <NUM> is configured to perform interprocess data exchange in the selected communication manner. In a specific example, the shared memory space is usually set to <NUM> B (<NUM> KB), and the size of the first memory space may be set to <NUM> KB.

Corresponding to the embodiment in which the solution using the memory sharing mechanism is selected when the length of the communication data is less than the threshold, that the configuration module <NUM> performs initialization configuration on a first process and a second process includes:.

In an example, the selection module <NUM> selects the communication manner based on the length of the communication data and the value of the threshold, where the threshold is the size of the first memory space allocated in the shared memory space; and selects, in response to a fact that the length of the communication data is less than the threshold, a memory sharing manner for communication.

In an example, that the processing module <NUM> performs interprocess data exchange in the selected communication manner includes:
allocating virtual address space to the second process, and mapping a physical memory page in the first memory space to the virtual address space of the second process, thereby implementing zero data copy by remapping data in shared memory to the second process.

In an example, the processing module <NUM> sends a message to the second process, to trigger the second process to run. In this way, the second process starts running, and directly reads data from the first memory space based on a remapped address, to exchange data with the first process.

After performing the interprocess data exchange in the memory sharing manner, in order to reclaim a virtual address, the processing module <NUM> unmaps the physical memory page in the first memory space from the virtual address space, to release the virtual address space.

In a memory sharing solution, the first process writes data into the shared memory, and remaps the data in the shared memory to the second process by remapping the data, so that the second process directly reads the data from the shared memory, thereby implementing zero data copy. Because there is no need to reallocate a memory address in the second process to store data of the first process, there is no need to release memory after interprocess communication is completed, thereby resolving a delay issue caused by memory copy, allocation, and release.

Corresponding to the embodiment in which the data file mechanism is selected when the length of the communication data is greater than the threshold, to-be-transmitted data of the first process is stored by using a data file, to bypass a restriction on the length of the communication data.

In an example, that the configuration module <NUM> performs initialization configuration on a first process and a second process further includes: creating the first memory space in the shared memory space.

In an example, that the selection module selects a communication manner based on a length of communication data and a value of a threshold includes:
selecting, in response to a fact that the length of the communication data reaches or exceeds the threshold, a data file manner for communication.

In an example, that the selection module selects, in response to a fact that the length of the communication data reaches or exceeds the threshold, a data file manner for communication further includes:.

In an example, before storing the communication data in the second memory space outside the shared memory space, the selection module further includes:.

Further, in an example, the data file includes a communication status identifier, the communication status identifier is used to indicate a communication status of the data file, and before determining the to-be-used data file based on the first cache list that is used to store the to-be-used file information, the selection module further includes: traversing a second cache list, where the second cache list is used to store file information of a data file in communication; determining, based on the communication status identifier, whether the second cache list includes file information corresponding to a data file for which communication has been completed; and in response to a fact that the second cache list includes the file information corresponding to the data file for which communication has been completed, storing, in the first cache list, the file information that is in the second cache list and that is corresponding to the data file for which communication has been completed, where the file information is used as the to-be-used file information. In this way, it can be ensured that the data file, for which communication has been completed, corresponding to the file information that is in the second cache list and that has not been stored in the first cache list in time is also included in a selection range of a second cache, thereby improving cache resource utilization.

Further, in an example, file information in the first cache list is sorted based on a size of corresponding to-be-used shared memory, and that the selection module determines whether the to-be-used shared memory includes to-be-used shared memory that meets the length of the communication data further includes: successively performing determining starting from smallest to-be-used shared memory in the first cache list; and when determining that the to-be-used shared memory includes the to-be-used shared memory that meets the length of the communication data, stopping determining and using the determined shared memory as the second memory space. In this way, smallest shared memory in shared memory that meets the length of the communication data and that is corresponding to a file descriptor in the first cache list is used with priority, to save space.

In an example, that the selection module stores metadata of the data file in the first memory space includes:.

On a basis that the data file manner is selected for communication, in an example, that the processing module performs interprocess data exchange in the selected communication manner includes: exchanging data by transmitting the metadata of the data file.

The exchanging data by transmitting the metadata of the data file further includes:.

Further, in an example, the metadata of the data file includes a file descriptor, and before sending the message to the second process, the processing module <NUM> is further configured to perform:
determining the corresponding second process based on control information corresponding to the file descriptor, and converting the file descriptor into a file descriptor that can be identified by the second process.

Further, in an example, that the processing module triggers the second process includes:
triggering the second process to restore data in the data file based on the file descriptor that can be identified by the second process and the corresponding control information.

In an example, after completing data exchange in the data file manner, the processing module <NUM> determines a communication status of the data file based on a communication status identifier of the data file; and in response to a fact that communication has been completed for the data file, stores, in a first cache list, file information of the data file for which communication is completed, where the file information is used as to-be-used file information for a next time. In this way, a file used at the end of reading by the second process is reserved and is used as a backup for a next process.

Further, in an example, that the processing module stores, in the first cache list, file information of the data file for which communication is completed, where the file information is used as to-be-used file information for a next time further includes:.

In an example, alternatively, the first process may send the metadata of the data file to the second process by using a kernel. The first process sends the metadata of the data file to the kernel; and the kernel determines, by using the metadata, a physical address of memory space in which the data file is located. After allocating a virtual memory address to the second process, the kernel maps a physical memory address of the data file to the virtual memory address of the second process, so that the second process accesses the data file by using the virtual memory address, to access the communication data.

In the data file solution, the to-be-transmitted data of the first process is stored by using the data file. In this process, a binder driver only needs to transmit metadata of the data file without a need to transmit the entire file, and data sharing can be implemented. In this manner, a restriction imposed by a binder communication mechanism on a volume of carried data can be bypassed.

The following describes a specific implementation embodiment to facilitate understanding of this solution.

In an embodiment, an overall method procedure of binder communication is optimized. Specific steps are as follows:.

During the communication in the "memory sharing manner", the binder driver remaps data in shared memory to the target process, to implement zero copy. Because there is no need to reallocate a memory address in the target process to store data of the client process, there is no need to release memory after interprocess communication is completed, thereby resolving a delay caused by memory copy, allocation, and release.

During communication in the "memory sharing manner", to-be-transmitted data of the client process is stored by using an additional shared file. In this way, the binder driver only needs to transmit metadata of the shared file to implement data sharing. In the file sharing manner, a restriction imposed by a binder communication mechanism in Android on a volume of carried data is bypassed.

It should be noted that the module division in this embodiment of the present invention is an example, and is merely logical function division. There may be another division manner during actual implementation.

It may be understood that to implement the foregoing functions, the interprocess communications system includes a corresponding hardware structure and/or software module for performing the functions. A person of ordinary skill in the art should easily be aware that the units and algorithm steps in the examples described in combination with the embodiments disclosed in this specification may be implemented by hardware or a combination of hardware and computer software in the present invention. Whether a function is performed by hardware or hardware driven by computer software depends on particular applications and design constraints of the technical solutions.

A person skilled in the art should be aware that in the foregoing one or more examples, functions described in the present invention may be implemented by hardware, software, firmware, or any combination thereof. When the present invention is implemented by software, the foregoing functions may be stored in a computer-readable medium or transmitted as one or more instructions or code in the computer-readable medium. The computer-readable medium includes a computer storage medium and a communications medium, where the communications medium includes any medium that enables a computer program to be transmitted from one place to another. The storage medium may be any available medium accessible to a general-purpose or dedicated computer.

Claim 1:
A computer implemented interprocess communication method, wherein the method comprises:
determining (<NUM>) that a data length of the communication data is less than a size of a first memory space;
when the data length of the communication data is less than the size of the first memory space, storing (<NUM>) communication data of a first process in the first memory space, wherein the communication data is used for data exchange between the first process and a second process, and the first memory space is memory space allocated to the first process for access;
allocating (<NUM>) virtual memory space to the second process, wherein the virtual memory space is used by the second process to access the communication data; and
mapping (<NUM>) a physical memory page in the first memory space to the virtual memory space; and
when the data length of the communication data is greater than the size of the first memory space, storing the communication data as a data file, wherein the second process accesses the communication data by using metadata of the data file.