CN116225745B - Linux-based multi-process communication method and system - Google Patents

Linux-based multi-process communication method and system Download PDF

Info

Publication number
CN116225745B
CN116225745B CN202310486110.2A CN202310486110A CN116225745B CN 116225745 B CN116225745 B CN 116225745B CN 202310486110 A CN202310486110 A CN 202310486110A CN 116225745 B CN116225745 B CN 116225745B
Authority
CN
China
Prior art keywords
address information
current
ipc
current process
ipc address
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.)
Active
Application number
CN202310486110.2A
Other languages
Chinese (zh)
Other versions
CN116225745A (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 Wanweiyingchuang Technology Co ltd
Original Assignee
Beijing Wanweiyingchuang 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 Wanweiyingchuang Technology Co ltd filed Critical Beijing Wanweiyingchuang Technology Co ltd
Priority to CN202310486110.2A priority Critical patent/CN116225745B/en
Publication of CN116225745A publication Critical patent/CN116225745A/en
Application granted granted Critical
Publication of CN116225745B publication Critical patent/CN116225745B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Classifications

    • HELECTRICITY
    • H04ELECTRIC COMMUNICATION TECHNIQUE
    • H04LTRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
    • H04L69/00Network arrangements, protocols or services independent of the application payload and not provided for in the other groups of this subclass
    • H04L69/16Implementation or adaptation of Internet protocol [IP], of transmission control protocol [TCP] or of user datagram protocol [UDP]
    • H04L69/164Adaptation or special uses of UDP protocol
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/54Interprogram communication
    • G06F9/542Event management; Broadcasting; Multicasting; Notifications
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/54Interprogram communication
    • G06F9/544Buffers; Shared memory; Pipes
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/54Interprogram communication
    • G06F9/546Message passing systems or structures, e.g. queues
    • HELECTRICITY
    • H04ELECTRIC COMMUNICATION TECHNIQUE
    • H04LTRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
    • H04L69/00Network arrangements, protocols or services independent of the application payload and not provided for in the other groups of this subclass
    • H04L69/16Implementation or adaptation of Internet protocol [IP], of transmission control protocol [TCP] or of user datagram protocol [UDP]
    • H04L69/161Implementation details of TCP/IP or UDP/IP stack architecture; Specification of modified or new header fields
    • H04L69/162Implementation details of TCP/IP or UDP/IP stack architecture; Specification of modified or new header fields involving adaptations of sockets based mechanisms
    • YGENERAL TAGGING OF NEW TECHNOLOGICAL DEVELOPMENTS; GENERAL TAGGING OF CROSS-SECTIONAL TECHNOLOGIES SPANNING OVER SEVERAL SECTIONS OF THE IPC; TECHNICAL SUBJECTS COVERED BY FORMER USPC CROSS-REFERENCE ART COLLECTIONS [XRACs] AND DIGESTS
    • Y02TECHNOLOGIES OR APPLICATIONS FOR MITIGATION OR ADAPTATION AGAINST CLIMATE CHANGE
    • Y02DCLIMATE CHANGE MITIGATION TECHNOLOGIES IN INFORMATION AND COMMUNICATION TECHNOLOGIES [ICT], I.E. INFORMATION AND COMMUNICATION TECHNOLOGIES AIMING AT THE REDUCTION OF THEIR OWN ENERGY USE
    • Y02D10/00Energy efficient computing, e.g. low power processors, power management or thermal management

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Computer Security & Cryptography (AREA)
  • Computer Networks & Wireless Communication (AREA)
  • Signal Processing (AREA)
  • Multimedia (AREA)
  • Data Exchanges In Wide-Area Networks (AREA)
  • Small-Scale Networks (AREA)

Abstract

The invention discloses a Linux-based multi-process communication method and a Linux-based multi-process communication system, wherein unique process names and UDP Socket port numbers are allocated to each process, each process is grouped, and process group names corresponding to each process are obtained; constructing IPC address information corresponding to each process; establishing UDP Socket for each process; establishing an IPC address information table, and writing the IPC address information of each process into the IPC address information table; when the process information is interacted, the corresponding target process is searched by traversing the IPC address information table, and then the information data is sent to the target process.

Description

Linux-based multi-process communication method and system
Technical Field
The invention relates to the technical field of computers, in particular to a Linux-based multi-process communication method and system.
Background
Linux is a freely used and freely spread UNIX-like operating system, and is widely applied to the fields of servers, industrial computers, embedded computers, special computers and the like due to the characteristics of open source, stability, safety and the like;
in Linux operating systems, a process is the minimum unit of system resource allocation, for example: memory, files, semaphores, socket, etc., each process has its own independent virtual address space, and processes are isolated from each other; in order to realize different inter-process message transmission, linux provides various inter-process communication (IPC) mechanisms, which mainly comprise: pipes, shared memory, semaphores, message queues, sockets, etc.
For complex multi-process applications, inter-process message interactions can be very frequent, and communication models can exist in many forms, such as: unicast, multicast, broadcast, etc. The native IPC mechanism provided by the Linux system is directly used, so that the application program codes are very redundant and chaotic, the inter-process coupling degree is greatly increased, as shown in fig. 11, in the prior art, a third party middleware is generally adopted to forward a message, such as TCP, generally, the middleware essentially provides an intermediate service program to forward the message, when a message is transmitted from a process a to a process X, the transmitted message at least passes through the process a to the intermediate service program and the intermediate service program to the process X, and the two moving processes of the process X are carried out, so that for the scene of frequent multi-process interaction and larger data volume, the real-time performance and the system performance of the message transmission are greatly reduced, and the system throughput is limited.
Disclosure of Invention
The invention aims to provide a Linux-based multi-process communication method and a Linux-based multi-process communication system, which solve the technical problems pointed out in the prior art.
The invention provides a Linux-based multi-process communication method, which comprises the following operation steps:
Initializing a system: establishing UDP Socket for each process respectively; respectively and correspondingly acquiring IPC address information of all the processes; respectively constructing virtual address spaces corresponding to all the processes based on UDP Socket and IPC address information corresponding to all the processes; constructing Socket address information corresponding to all the processes; binding UDP sockets corresponding to all the processes to Socket addresses corresponding to the processes; adding the IPC address information of all the processes into an IPC address information table; acquiring a current message data source process and current message data;
acquiring a transmission object process of the message data source process, and determining the transmission object process as a target process; traversing the IPC address information table, and inquiring the IPC address information of the target process;
and sending the current message data to the target process.
Correspondingly, the invention also provides a Linux-based multi-process communication system, which comprises an initialization processing module, a traversal finding module and a sending processing module;
the initialization processing module comprises an initialization processing sub-module, a construction sub-module, a semaphore allocation processing sub-module and an IPC address processing sub-module:
The initialization processing module is used for respectively creating UDP sockets for each process; respectively and correspondingly acquiring IPC address information of all the processes; respectively constructing virtual address spaces corresponding to all the processes based on UDP Socket and IPC address information corresponding to all the processes; constructing Socket address information corresponding to all the processes; binding UDP sockets corresponding to all the processes to Socket addresses corresponding to the processes; adding the IPC address information of all the processes into an IPC address information table; acquiring a current message data source process and current message data;
the traversal finding module is used for obtaining a transmission object process of the message data source process and determining the transmission object process as a target process; traversing the IPC address information table, and inquiring the IPC address information of the target process;
and the sending processing module is used for sending the current message data to the target process.
The initialization processing sub-module is used for creating UDP Socket for the current process; assigning a unique process name and UDP Socket port number to the current process; acquiring attribute information of a current process; grouping the current process according to the attribute information of the current process to obtain a process group name of the current process; constructing IPC address information of the current process based on the process name of the current process, the process group name of the current process and the UDP Socket port number of the current process; and establishing an IPC address information table based on the IPC address information of the current process; acquiring a shared memory in which the IPC address information table is located; acquiring a virtual address space of the process based on UDP Socket and IPC address information of the current process;
The construction submodule is used for constructing Socket address information of the current process; binding the UDP Socket of the current process to a Socket address of the corresponding current process; connecting the shared memory where the IPC address information table is positioned to a virtual address space of the current process;
the semaphore allocation processing sub-module is used for creating and opening semaphores; the current process applies for the semaphore; judging whether the semaphore is applied by other processes (namely processes except the current process); if the semaphore has been applied by a process other than the current process, the current process waits for the semaphore to be released; if the semaphore is not applied by a process except the current process, controlling the current process to hold the semaphore, and adding the IPC address information of the current process into the IPC address information table; the semaphore is then released.
The IPC address processing sub-module; the IPC address processing sub-module is used for acquiring the IPC address information of the current process.
Compared with the prior art, the embodiment of the invention has at least the following technical advantages:
analyzing the Linux-based multi-process communication method and system provided by the invention can be known that when the Linux-based multi-process communication method and system are specifically applied, firstly, system initialization is carried out, and UDP sockets are respectively established for each process through the system initialization; respectively and correspondingly acquiring IPC address information of all the processes; respectively constructing virtual address spaces corresponding to all the processes based on UDP Socket and IPC address information corresponding to all the processes; constructing Socket address information corresponding to all the processes; binding UDP sockets corresponding to all the processes to Socket addresses corresponding to the processes; adding the IPC address information of all the processes into the IPC address information table; acquiring a current message data source process and current message data; subtracting the process that message interaction in the Linux system in the prior art needs to forward messages in a TPC mode, and directly carrying out message interaction between processes through UDP sockets, wherein each process only needs to create one UDP Socket, so that the method can be used for sending messages to any process on one hand and receiving messages from any process on the other hand;
Acquiring a transmission object process of the message data source process, and determining the transmission object process as a target process; traversing the IPC address information table, and inquiring the IPC address information of the target process; when sending the message, only the IPC address table is queried according to the name or the group of the target process, the IPC address (essentially the port number of the UDP Socket) of the target process is obtained, and then the message is directly sent to the target process by using the Socket API sendto (); an IPC address information table is adopted to store the IPC address information of each process, when a message data source process sends a message, the IPC address information of a single target process or a plurality of target processes is obtained by inquiring the IPC address information table, and then message data is directly sent to the corresponding target process; the IPC address table is a common resource of all processes in the process set, and each process must be accessible; in order to facilitate efficient and simple table lookup, the invention adopts a shared memory to store the IPC address table, and adopts a semaphore mechanism to ensure that multiple processes mutually exclusive access to the IPC address table; when each process is started, firstly, a UDP Socket is created and bound to a pre-allocated port number, and then the IPC address information of the user is registered in an IPC address table, so that all processes in the process set can find out the communication addresses of each other through the IPC address table.
When receiving the message, the process can adopt various modes such as synchronous, asynchronous, polling and the like to detect the arrival of UDP Socket data, then directly reads the message data from the UDP Socket by SocketAPI recvfrom (), and then sends the message data to an upper layer service for processing; and when the messages are interacted, a semaphore mechanism is adopted, so that only one process can access shared data at a time, and the problem that multiple processes access simultaneously to possibly cause data inconsistency is avoided.
Drawings
In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings that are needed in the description of the embodiments or the prior art will be briefly described, and it is obvious that the drawings in the description below are some embodiments of the present invention, and other drawings can be obtained according to the drawings without inventive effort for a person skilled in the art.
FIG. 1 is a schematic diagram of an operation flow of a Linux-based multi-process communication method according to an embodiment of the present invention;
FIG. 2 is a schematic diagram of an operation flow of system initialization in a Linux-based multi-process communication method according to an embodiment of the present invention;
Fig. 3 is a schematic operation flow diagram of a Linux-based multi-process communication method in IPC unicast according to a second embodiment of the present invention;
fig. 4 is a schematic diagram of a process-to-process communication model of a Linux-based multi-process communication method when IPC unicast is performed in accordance with a second embodiment of the present invention;
fig. 5 is a schematic diagram of an operation flow of a Linux-based multi-process communication method when IPC multicast is performed according to a third embodiment of the present invention;
fig. 6 is a schematic diagram of an inter-process communication model of a Linux-based multi-process communication method during IPC multicast according to a third embodiment of the present invention;
fig. 7 is a schematic diagram of an operation flow of a Linux-based multi-process communication method in IPC broadcasting according to a fourth embodiment of the present invention;
fig. 8 is a schematic diagram of a process-to-process communication model of a Linux-based multi-process communication method during IPC broadcasting according to a fourth embodiment of the present invention;
FIG. 9 is a schematic diagram of an overall architecture of a Linux-based multi-process communication system according to a fifth embodiment of the present invention;
FIG. 10 is a schematic diagram of a Linux-based multi-process communication system according to a fifth embodiment of the present invention;
fig. 11 is a schematic diagram of third party middleware message forwarding in the prior art.
Reference numerals: initializing a processing module 10; traversing the ascertaining module 20; a transmission processing module 30; an initialization processing sub-module 11; constructing a sub-module 12; a semaphore allocation processing sub-module 13; IPC address processing submodule 14.
Detailed Description
The following description of the embodiments of the present invention will be made apparent and fully in view of the accompanying drawings, in which some, but not all embodiments of the invention are shown. All other embodiments, which can be made by those skilled in the art based on the embodiments of the invention without making any inventive effort, are intended to be within the scope of the invention.
The invention will now be described in further detail with reference to specific examples thereof in connection with the accompanying drawings.
Example 1
As shown in fig. 1, correspondingly, the invention also provides a Linux-based multi-process communication method, which comprises the following operation steps:
step S10: initializing a system: establishing UDP Socket for each process respectively; respectively and correspondingly acquiring IPC address information of all the processes; respectively constructing virtual address spaces corresponding to all the processes based on UDP Socket and IPC address information corresponding to all the processes; constructing Socket address information corresponding to all the processes; binding UDP sockets corresponding to all the processes to Socket addresses corresponding to the processes; adding the IPC address information of all the processes into an IPC address information table; acquiring a current message data source process and current message data;
Explanation: the message data refers to message data to be prepared for unicast, multicast or broadcast by the current application; each process only needs to create one UDP Socket, which can be used for sending messages to any process and receiving messages from any process;
step S20: acquiring a transmission object process of the message data source process, and determining the transmission object process as a target process; traversing the IPC address information table, and inquiring the IPC address information of the target process;
step S30: and sending the current message data to the target process.
Explanation: when sending the message, the message data source process only needs to inquire the IPC address table according to the name or the group of the target process, acquires the IPC address (essentially the port number of the UDP Socket) of the target process, and then directly uses the Socket API sendto () to send the message to the target process; when the target process receives the message, the target process can adopt various modes such as synchronous, asynchronous, polling and the like to detect the arrival of UDP Socket data, then directly reads the message data from the UDP Socket by SocketAPI recvfrom (), and then sends the message data to an upper layer service for processing.
Specifically, as shown in fig. 2, in step S10, the system initialization includes the following operation steps:
Step S101: process initialization: establishing UDP Socket for the current process; assigning a unique process name and UDP Socket port number to the current process; acquiring attribute information of a current process; and grouping the current process according to the attribute information (explanation: for example, five processes are currently represented as a process 1, a process 2, a process 3, a process 4 and a process 5, wherein the process 1 is a primary process, the process 2 is a primary process, the process 3 is a secondary process, the process 4 is a tertiary process and the process 5 is a tertiary process), so as to obtain a primary process (group I), a process 1, a process 2, a secondary process (group II), a tertiary process (group III), a process 4 and a process 5, and then obtaining the process group names of the current process, wherein the process group name of the current process is a group I, the process group name of the process 2 is a group I, the process group name of the process 3 is a group II, the process group name of the process 4 is a group III, and the process group name of the process 5 is a group III), so as to obtain the process group name of the current process; constructing IPC address information of the current process based on the process name of the current process, the process group name of the current process and the UDP Socket port number of the current process; and establishing an IPC address information table based on the IPC address information of the current process; acquiring a shared memory in which the IPC address information table is located; acquiring a virtual address space of the process based on UDP Socket and IPC address information of the current process;
The IPC address information table comprises IPC address information of a plurality of processes; the IPC address information of the current process comprises a process name of the current process, a process group name of the current process and a UDP Socket port number of the current process; and the IPC address information table is empty when being initially established;
when the application program software architecture is designed, the aspects of process distribution, process interaction, process names, process grouping and the like are required to be designed and defined, each process is allocated with a unique process name and UDP Socket port number, and the processes are grouped according to architecture requirements; each process can only belong to one process group, and one process group can comprise one or more processes;
the UDP Socket is established according to Socket () functions in various inter-process communication (IPC) mechanisms provided by a Linux system;
when the current process is processed, recording a file descriptor of the current process as a global variable, wherein the global variable is a private resource of the process, and the processes cannot access each other; the file descriptor of the process is recorded as a global variable, so that the subsequent operation steps are more convenient;
The IPC address information table is used for storing the IPC address information of each process, and the message sender acquires the IPC addresses of a single target process or a plurality of target processes by inquiring the table and then directly sends the message to the corresponding target process; the IPC address information table is a common resource of all processes in the process set, and each process must be accessible; in order to facilitate efficient and simple table lookup, the embodiment of the invention adopts a shared memory to store the IPC address information table, and adopts a semaphore mechanism to ensure that multiple processes mutually exclusive access to the IPC address information table; when each process is started, firstly, a UDP Socket is created and bound to a pre-allocated port number, and then the IPC address information (comprising a process name, a process group name and an IPC address) of the user is registered in an IPC address information table, so that all processes in a process set can find out the communication addresses of each other through the IPC address information table;
the shared memory refers to an inter-process communication mode with the highest speed provided by a Linux system; the shared memory allows two or more processes to share the same block of memory, accesses the shared memory area as fast as accesses the memory area unique to the process, and does not need to be completed through a system call or other processes needing to cut into the kernel, so that the access speed is very fast. The shared memory area is not a private resource of a certain process, but is a memory area allocated by an operating system, and the process firstly needs to be connected to map the shared memory area to its own virtual address space, and then can access the shared memory.
Step S102: constructing Socket address information of the current process;
the construction of the Socket address information is explained by using Socket () functions in various inter-process communication (IPC) mechanisms provided by a Linux system, wherein the Socket address information of the current process is a structural body for recording information such as a packaging port number of the current process and IP of the current process; when the Socket address information of the current process is constructed, a sockaddr_in information structure body needs to be filled: the sin_addr address is set as the native loop IP127.0.0.1, the sin_port is set as the pre-assigned UDP Socket port number, and so on.
Step S103: binding the UDP Socket of the current process to a Socket address of the corresponding current process;
it should be noted that, binding the UDP Socket of the current process to the Socket address of the corresponding current process refers to binding the UDP Socket created by the current process to the UDP Socket port number initially allocated by the current process, and by binding the UDP Socket of the current process with the Socket address of the corresponding current process, the processes may directly use the UDP Socket to perform message interaction; binding UDP Socket to Socket address constructed in step S20 by bind () function; binding a local address with a socket; the bind () function is applicable to unconnected datagrams or stream class sockets and is used before connect () or listen () is called; when Socket () is used to create a Socket, the bind () function exists in a namespace (address family) but is not named. The bind () function establishes a local bundle (host address/port number) for a socket by assigning a local name to an unnamed socket.
Step S104: connecting the shared memory where the IPC address information table is positioned to a virtual address space of the current process;
explanation: the technical scheme adopted by the embodiment of the invention is that the shared memory in which the IPC address information table is positioned is connected to the virtual address space of the process by utilizing the shmat () function, so that the process can read and write the common IPC address information table through simple memory operation;
the virtual address space of the current process and the UDP Socket of the current process are stored in the memory of the current process; the shared memory is a communication mode between processes with the highest speed provided by the Linux system. It allows two or more processes to share the same block of memory, access to the shared memory region as fast as to access a memory region unique to the process, and does not need to be done by a system call or other procedure that requires a cut into the kernel, so the access speed is very fast. The shared memory area is not a private resource of a certain process, but is a memory area allocated by an operating system, and the process firstly needs to be connected to map the shared memory area to its own virtual address space, and then can access the shared memory.
Step S105: creating and turning on a semaphore;
The initial value of the semaphore is set to be 1, and the value after the semaphore is applied by a process is 0;
explanation: the semaphore is an inter-process synchronization mutual exclusion mechanism provided by the Linux system and is commonly used for accessing shared data by multiple processes, so that only one process accesses the shared data at a time, and the problem that the simultaneous access of the multiple processes possibly causes data inconsistency is avoided; the creation of the semaphore is created and opened by using a semopen () function in an API provided by a Linux system provided by the Linux system, and when the semaphore is created or opened, the initial value of the semaphore is set to be 1 for sharing data exclusive access;
the "semaphore" for the exclusive access of the IPC address information table is opened by using the semopen () function, so as to avoid the situation that the IPC address information table is possibly confused when the IPC address information table is operated by multiple processes simultaneously.
Step S106: the current process applies for the semaphore; judging whether the semaphore is applied by a process except the current process or not; if the semaphore has been applied by a process other than the current process, the current process waits for the semaphore to be released;
it should be noted that, the application semaphore applies for "semaphore" by using the sem_wait () function in the API provided by the Linux system, if the "semaphore" has been applied by a process other than the current process at this time, the process waits until the process holding the "semaphore" releases the "semaphore", and the process can not obtain the "semaphore" and further continues to execute;
Applying for 'signal quantity' by using sem_wait () function, applying for in a blocking mode, and continuing to operate the IPC address information table downwards after the application is successful;
step S107: if the semaphore is not applied by a process except the current process, controlling the current process to hold the semaphore, and adding the IPC address information of the current process into the IPC address information table; then releasing the semaphore;
it should be noted that, because the semaphore is held by at most one process at the same time; if the semaphore is not applied by a process other than the current process, determining that the current process applies for the semaphore and only the current process; the current process can apply for success and hold the semaphore; after the current process holds the semaphore, registering the IPC address information corresponding to the current process into an IPC address information table;
the technical scheme adopted by the embodiment of the invention is to release the 'semaphore' by utilizing the sempost () function in the API provided by the Linux system so that other processes can access the IPC address information table.
Example two
The embodiment of the invention shows an application scene of the method when IPC unicast is carried out;
as shown in fig. 3 or fig. 4, fig. 3 is a schematic operation flow diagram of a Linux-based multi-process communication method in IPC unicast according to a third embodiment of the present invention; fig. 4 is a schematic diagram of a process-to-process communication model of a Linux-based multi-process communication method when IPC unicast is performed in accordance with the third embodiment of the present invention;
The invention provides a Linux-based multi-process communication method, which comprises the following operation steps:
step S1001: initializing a system; acquiring unicast information data; determining the transmission object process of the current unicast information data as a target process;
it should be noted that, the system initialization refers to the IPC system initialization method provided in the second embodiment of the present invention; establishing UDP Socket for all the processes, acquiring IPC address information corresponding to each process, and adding the IPC address information corresponding to each process into an IPC address information table; when unicast processing operation is performed, the IPC address information of the corresponding target process can be found in the IPC address information table.
Step S1002: traversing the IPC address information table to obtain IPC address information of the target process;
explanation: the IPC address information table is traversed, the record of the target process is found out, and normally, only one matching record exists, and before and after the process is executed, the operations of applying for the semaphore and releasing the semaphore are also needed, so that the conflict with other processes in which the IPC address is added is avoided.
Step S1003: constructing Socket address information of the target process;
Explanation: the Socket address information is established by using Socket () functions in various inter-process communication (IPC) mechanisms provided by a Linux system, and the Socket address information of the current process is a structure body for recording information such as a package port number of the current process and IP of the current process; when the Socket address information of the current process is constructed, a sockaddr_in information structure body needs to be filled: the sin_addr address is set as the native loop IP127.0.0.1, the sin_port is set as the pre-assigned UDP Socket port number, and so on.
Step S1004: transmitting the unicast information data to the target process;
it should be noted that, the technical scheme adopted in the embodiment of the present invention is to send the message data of the upper layer service application to the target process by using a sendto (UDP Socket, message data, target process Socket address) function.
Example III
The embodiment of the invention shows an application scene of the method in IPC multicast;
as shown in fig. 5 or fig. 6, fig. 5 is a schematic operation flow diagram of a Linux-based multi-process communication method in IPC multicast according to a fourth embodiment of the present invention; fig. 6 is a schematic diagram of a process-to-process communication model of a Linux-based multi-process communication method during IPC multicast according to a fourth embodiment of the present invention;
The invention provides a Linux-based multi-process communication method, which comprises the following operation steps:
step S2001: initializing a system; acquiring multicast information data; acquiring a current multicast information data source process; determining a transmission object process group of the current multicast information data as a target process group;
it should be noted that, the system initialization refers to the IPC system initialization method provided in the second embodiment of the present invention; establishing UDP Socket for all the processes, acquiring IPC address information corresponding to each process, and adding the IPC address information corresponding to each process into an IPC address information table; when unicast processing operation is carried out, the IPC address information of the corresponding process in the target process group can be searched in the IPC address information table;
the current multicast information data source process refers to a source process of the current multicast information data, that is, the current multicast information data source process sends the multicast information data to be prepared for multicast to a certain process group;
step S2002: traversing an IPC address information table to obtain the target process group name; acquiring IPC address information corresponding to one or more target processes to be multicast respectively through the target process group names;
It should be noted that, traversing the IPC address information table, finding out a record of the name of the target process group from the IPC address information table, and obtaining the target process group through the name of the target process group; since a process group may include one or more processes, one or more records may be matched to the process group, and before and after the process is executed, operations of applying "semaphore" and releasing "semaphore" are also required to avoid conflicts with other processes in adding IPC address operations.
Step S2003: traversing all target processes to be multicast one by one, and screening out target processes to be multicast which are the same as the current multicast information data source process to obtain a first-stage target process group to be multicast;
explanation: all the queried records are traversed one by one, whether the process name is the same as the process or not is compared, and if so, the process is skipped, so that the message is prevented from being sent to the user.
Step S2004: randomly and randomly selecting one process in the first-level target process group to be multicast to determine the first-level target process group to be a multicast target process; constructing Socket address information of the multicast target process;
it should be noted that, socket address information of the target process is constructed, a Socket addr_in information structure is filled, a sin_addr address is set as a native loop IP127.0.0.1, and a sin_port is set as the field content of "IPC address" in the record.
Step S2005: transmitting the multicast information data to the multicast target process; then returning to the operation step S2003, screening out the current multicast target process to obtain a secondary target process group to be multicast; and transmitting the multicast information data to the next target process until the transmission of the multicast information data is completed;
explanation: and (3) transmitting the message data of the upper layer service application to the target process by using a sendto (UDP Socket, message data, target process Socket address) function, and returning to the step (2), and continuing to transmit the message to the next target process until all processes in the process group are completely transmitted.
Example IV
The embodiment of the invention shows an application scene of the method in IPC broadcasting;
fig. 7 is a schematic operation flow diagram of a Linux-based multi-process communication method according to a fifth embodiment of the present invention when IPC broadcasting is performed, as shown in fig. 7 or 8; fig. 8 is a schematic diagram of a process-to-process communication model of a Linux-based multi-process communication method during IPC broadcasting according to a fifth embodiment of the present invention;
the invention provides a Linux-based multi-process communication method, which comprises the following operation steps:
Step S3001: initializing a system; acquiring broadcast information data; acquiring a current broadcast information data source process; determining a plurality of transmission object processes of the current broadcast information data as a target process set to be broadcast;
it should be noted that, the system initialization refers to the IPC system initialization method provided in the second embodiment of the present invention; establishing UDP Socket for all the processes, acquiring IPC address information corresponding to each process, and adding the IPC address information corresponding to each process into an IPC address information table; when unicast processing operation is performed, the IPC address information of the corresponding process in the target process set to be broadcasted can be found in the IPC address information table.
The current broadcast information data source process refers to a source process of the current broadcast information data, that is, the current broadcast information data source process broadcasts the broadcast information data to be ready for broadcasting to all the current processes;
step S3002: traversing an IPC address information table to obtain IPC address information respectively corresponding to all processes in the target process set to be broadcasted;
it should be noted that, traversing the IPC address information table, reading all records, and reading the number of records that is the same as the number of processes contained in the application software under normal conditions, and before and after executing the process, also needing to apply for the operations of "semaphore" and releasing "semaphore" to avoid the conflict with other processes for adding IPC address operations;
Step S3003: traversing all processes in the target process set to be broadcasted one by one, and screening out the processes which are the same as the current broadcast information data source process to obtain a first target process set to be broadcasted;
it should be noted that, traversing all the queried records one by one, comparing whether the process name is the same as the process, if so, skipping, and avoiding the message from being sent to the user;
step S3004: randomly selecting any one process in all processes in the target process set to be broadcasted, and determining the process as a broadcasting target process; constructing Socket address information of the broadcasting target process;
it should be noted that, socket address information of the target process is constructed, a Socket addr_in information structure is filled, a sin_addr address is set as a native loop IP127.0.0.1, and a sin_port is set as the field content of "IPC address" in the record.
Step S3005: transmitting the broadcast information data to the target process to be broadcast; then returning to the operation step S3003, screening out the current broadcast target process, and obtaining a second target process set to be broadcast; and transmitting the broadcast information data to the next broadcast target process until the transmission of the broadcast information data is completed;
Explanation: and (3) transmitting the message data of the upper layer service application to the target process by using a sendto (UDP Socket, message data, target process Socket address) function, returning to the step (2), and continuing to transmit the message to the next target process until all processes in the application software are completely transmitted.
Example five
Fig. 9 is a schematic diagram of an overall architecture of a Linux-based multi-process communication system according to a first embodiment of the present invention, as shown in fig. 9 or fig. 10; FIG. 10 is a schematic diagram of a Linux-based multi-process communication system according to an embodiment of the present invention;
the invention provides a Linux-based multi-process communication system, which comprises an initialization processing module 10, a traversal finding module 20 and a sending processing module 30;
the initialization processing module 10 comprises an initialization processing sub-module 11, a construction sub-module 12, a signal quantity distribution processing sub-module 13 and an IPC address processing sub-module 14;
the initialization processing module 10 is configured to create a UDP Socket for each process; respectively and correspondingly acquiring IPC address information of all the processes; respectively constructing virtual address spaces corresponding to all the processes based on UDP Socket and IPC address information corresponding to all the processes; constructing Socket address information corresponding to all the processes; binding UDP sockets corresponding to all the processes to Socket addresses corresponding to the processes; adding the IPC address information of all the processes into an IPC address information table; acquiring a current message data source process and current message data;
The traversal finding module 20 is configured to obtain a transmission object process of the message data source process, and determine that the transmission object process is a target process (the target process is a transmission object of the message data source process); traversing the IPC address information table, and inquiring the IPC address information of the target process;
the sending processing module 30 is configured to send the current message data to the target process.
The initialization processing sub-module 11 is configured to create a UDP Socket for the current process; assigning a unique process name and UDP Socket port number to the current process; acquiring attribute information of a current process; grouping the current process according to the attribute information of the current process to obtain a process group name of the current process; constructing IPC address information of the current process based on the process name of the current process, the process group name of the current process and the UDP Socket port number of the current process; and establishing an IPC address information table based on the IPC address information of the current process; acquiring a shared memory in which the IPC address information table is located; acquiring a virtual address space of the process based on UDP Socket and IPC address information of the current process;
The construction submodule 12 is configured to construct Socket address information of the current process; binding the UDP Socket of the current process to a Socket address of the corresponding current process; connecting the shared memory where the IPC address information table is positioned to a virtual address space of the current process;
the semaphore allocation processing sub-module 13 is used for creating and turning on semaphores; the current process applies for the semaphore; judging whether the semaphore is applied by a process except the current process or not; if the semaphore has been applied by a process other than the current process, the current process waits for the semaphore to be released; if the semaphore is not applied by a process except the current process, controlling the current process to hold the semaphore, and adding the IPC address information of the current process into the IPC address information table; the semaphore is then released.
The IPC address processing sub-module 14 is configured to obtain IPC address information of a current process.
In summary, in the Linux-based multi-process communication system provided by the invention, firstly, a UDP Socket is respectively created for each process through an initialization module; respectively and correspondingly acquiring IPC address information of all the processes; respectively constructing virtual address spaces corresponding to all the processes based on UDP Socket and IPC address information corresponding to all the processes; constructing Socket address information corresponding to all the processes; binding UDP sockets corresponding to all the processes to Socket addresses corresponding to the processes; adding the IPC address information of all the processes into the IPC address information table; the method comprises the steps of acquiring a current message data source process and current message data, acquiring a transmission object process of the message data source process through a traversal finding module when a message is sent, and determining the transmission object process as a target process; traversing the IPC address information table, and inquiring the IPC address information of the target process; and then, the current message data is sent to the target process through a sending processing module.
In summary, according to the Linux-based multi-process communication method and system provided by the embodiment of the invention, firstly, system initialization is performed, and UDP sockets are respectively created for each process through the system initialization; respectively and correspondingly acquiring IPC address information of all the processes; respectively constructing virtual address spaces corresponding to all the processes based on UDP Socket and IPC address information corresponding to all the processes; constructing Socket address information corresponding to all the processes; binding UDP sockets corresponding to all the processes to Socket addresses corresponding to the processes; adding the IPC address information of all the processes into the IPC address information table; acquiring a current message data source process and current message data; subtracting the process that message interaction in the Linux system in the prior art needs to forward messages in a TPC mode, and directly carrying out message interaction between processes through UDP sockets, wherein each process only needs to create one UDP Socket, so that the method can be used for sending messages to any process on one hand and receiving messages from any process on the other hand;
acquiring a transmission object process of the message data source process, and determining the transmission object process as a target process; traversing the IPC address information table, and inquiring the IPC address information of the target process; when sending the message, only the IPC address table is queried according to the name or the group of the target process, the IPC address (essentially the port number of the UDP Socket) of the target process is obtained, and then the message is directly sent to the target process by using the Socket API sendto (); an IPC address information table is adopted to store the IPC address information of each process, when a message data source process sends a message, the IPC address information of a single target process or a plurality of target processes is obtained by inquiring the IPC address information table, and then message data is directly sent to the corresponding target process; the IPC address table is a common resource of all processes in the process set, and each process must be accessible; in order to facilitate efficient and simple table lookup, the invention adopts a shared memory to store the IPC address table, and adopts a semaphore mechanism to ensure that multiple processes mutually exclusive access to the IPC address table; when each process is started, firstly, a UDP Socket is created and bound to a pre-allocated port number, and then the IPC address information of the user is registered in an IPC address table, so that all processes in the process set can find out the communication addresses of each other through the IPC address table. When receiving the message, the process can adopt various modes such as synchronous, asynchronous, polling and the like to detect the arrival of UDP Socket data, then directly reads the message data from the UDP Socket by SocketAPI recvfrom (), and then sends the message data to an upper layer service for processing; and when the messages are interacted, a semaphore mechanism is adopted, so that only one process can access shared data at a time, and the problem that multiple processes access simultaneously to possibly cause data inconsistency is avoided.
Finally, it should be noted that: the above embodiments are only for illustrating the technical solution of the present invention, and not for limiting the same; modifications of the technical solutions described in the foregoing embodiments, or equivalent substitutions of some or all of the technical features thereof, may be made by those of ordinary skill in the art; such modifications and substitutions do not depart from the spirit of the invention.

Claims (8)

1. The Linux-based multi-process communication method is characterized by comprising the following operation steps:
initializing a system: establishing UDP Socket for each process respectively; respectively and correspondingly acquiring IPC address information of all the processes; respectively constructing virtual address spaces corresponding to all the processes based on UDP Socket and IPC address information corresponding to all the processes; constructing Socket address information corresponding to all the processes; binding UDP sockets corresponding to all the processes to Socket addresses corresponding to the processes; adding the IPC address information of all the processes into an IPC address information table; acquiring a current message data source process and current message data;
acquiring a transmission object process of the message data source process, and determining the transmission object process as a target process; traversing the IPC address information table, and inquiring the IPC address information of the target process;
Sending the current message data to the target process;
the system initialization comprises the following operation steps:
process initialization: establishing UDP Socket for the current process; assigning a unique process name and UDP Socket port number to the current process; acquiring attribute information of a current process; grouping the current process according to the attribute information of the current process to obtain a process group name of the current process; constructing IPC address information of the current process based on the process name of the current process, the process group name of the current process and the UDP Socket port number of the current process; and establishing an IPC address information table based on the IPC address information of the current process; acquiring a shared memory in which the IPC address information table is located; acquiring a virtual address space of the process based on UDP Socket and IPC address information of the current process;
constructing Socket address information of the current process;
binding the UDP Socket of the current process to a Socket address of the corresponding current process;
connecting the shared memory where the IPC address information table is positioned to a virtual address space of the current process;
creating and turning on a semaphore;
the current process applies for the semaphore; judging whether the semaphore is applied by a process except the current process or not; if the semaphore has been applied by a process other than the current process, the current process waits for the semaphore to be released;
If the semaphore is not applied by a process except the current process, controlling the current process to hold the semaphore, and adding the IPC address information of the current process into the IPC address information table; the semaphore is then released.
2. A Linux-based multi-process communication method according to claim 1, wherein the initial value of the semaphore is set to 1 and the post value applied by the process is set to 0.
3. The Linux-based multi-process communication method of claim 1, wherein the IPC address information table includes IPC address information of a plurality of processes.
4. The Linux-based multi-process communication method according to claim 1, wherein the IPC address information of the current process includes a process name of the current process, a process group name of the current process, and a UDP Socket port number of the current process; and the IPC address information table is initially empty when it is built.
5. A Linux-based multi-process communication system, wherein the system is applied to the Linux-based multi-process communication method according to any one of claims 1 to 4, and the system comprises an initialization processing module, a traversal finding module and a sending processing module;
The initialization processing module is used for respectively creating UDP sockets for each process; respectively and correspondingly acquiring IPC address information of all the processes; respectively constructing virtual address spaces corresponding to all the processes based on UDP Socket and IPC address information corresponding to all the processes; constructing Socket address information corresponding to all the processes; binding UDP sockets corresponding to all the processes to Socket addresses corresponding to the processes; adding the IPC address information of all the processes into an IPC address information table; acquiring a current message data source process and current message data;
the traversal finding module is used for obtaining a transmission object process of the message data source process and determining the transmission object process as a target process; traversing the IPC address information table, and inquiring the IPC address information of the target process;
the sending processing module is used for sending the current message data to the target process;
the initialization processing module comprises an initialization processing sub-module, a construction sub-module, a signal quantity distribution processing sub-module and an IPC address processing sub-module;
the initialization processing sub-module is used for creating UDP Socket for the current process; assigning a unique process name and UDP Socket port number to the current process; acquiring attribute information of a current process; grouping the current process according to the attribute information of the current process to obtain a process group name of the current process; constructing IPC address information of the current process based on the process name of the current process, the process group name of the current process and the UDP Socket port number of the current process; and establishing an IPC address information table based on the IPC address information of the current process; acquiring a shared memory in which the IPC address information table is located; and acquiring a virtual address space of the process based on the UDP Socket and IPC address information of the current process.
6. The Linux-based multi-process communication system according to claim 5, wherein the constructing submodule is configured to construct Socket address information of the current process; binding the UDP Socket of the current process to a Socket address of the corresponding current process; and connecting the shared memory where the IPC address information table is positioned to a virtual address space of the current process.
7. A Linux-based multiprocessing communication system according to claim 5, wherein the semaphore assignment processing sub-module is configured to create and turn on a semaphore; the current process applies for the semaphore; judging whether the semaphore is applied by a process except the current process or not; if the semaphore has been applied by a process other than the current process, the current process waits for the semaphore to be released; if the semaphore is not applied by a process except the current process, controlling the current process to hold the semaphore, and adding the IPC address information of the current process into the IPC address information table; the semaphore is then released.
8. The Linux-based multi-process communication system according to claim 5, wherein the IPC address processing sub-module is configured to obtain IPC address information of a current process.
CN202310486110.2A 2023-05-04 2023-05-04 Linux-based multi-process communication method and system Active CN116225745B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202310486110.2A CN116225745B (en) 2023-05-04 2023-05-04 Linux-based multi-process communication method and system

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202310486110.2A CN116225745B (en) 2023-05-04 2023-05-04 Linux-based multi-process communication method and system

Publications (2)

Publication Number Publication Date
CN116225745A CN116225745A (en) 2023-06-06
CN116225745B true CN116225745B (en) 2023-07-21

Family

ID=86585792

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202310486110.2A Active CN116225745B (en) 2023-05-04 2023-05-04 Linux-based multi-process communication method and system

Country Status (1)

Country Link
CN (1) CN116225745B (en)

Families Citing this family (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN117908849A (en) * 2024-03-15 2024-04-19 北京万维盈创科技发展有限公司 Universal embedded Linux software development system and development method

Family Cites Families (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US8566536B1 (en) * 2009-03-17 2013-10-22 Sandia Corporation Direct access inter-process shared memory
CN104572313B (en) * 2013-10-22 2018-03-13 华为技术有限公司 Communication means and device between a kind of process
CN106681842A (en) * 2017-01-18 2017-05-17 迈普通信技术股份有限公司 Management method and device for sharing memory in multi-process system
CN107368379B (en) * 2017-07-14 2020-07-10 中南大学 EVP-oriented cross Guest OS inter-process communication method and system
CN112131019A (en) * 2020-09-17 2020-12-25 国网宁夏电力有限公司营销服务中心(国网宁夏电力有限公司计量中心) Method for rapidly communicating processes of microkernel operating system
CN115934338A (en) * 2022-12-19 2023-04-07 北京天融信网络安全技术有限公司 Inter-process communication method and device

Also Published As

Publication number Publication date
CN116225745A (en) 2023-06-06

Similar Documents

Publication Publication Date Title
US5848234A (en) Object procedure messaging facility
US5396630A (en) Method and system for object management across process boundries in a data processing system
EP0343820B1 (en) Temporary state preservation for a distributed file service
US7444405B2 (en) Method and apparatus for implementing a MAC address pool for assignment to a virtual interface aggregate
CN116225745B (en) Linux-based multi-process communication method and system
RU2477930C2 (en) Method and system for transmitting flow multimedia data with zero copying
JP2612676B2 (en) Addressing automatic conversion method for porting a telecommunications application of a TCP / IP network to an OSI-CO network and apparatus for performing the method
JPS61289458A (en) Inter-process communication apparatus
US20030110166A1 (en) Queue management
JPH10301873A (en) System and method for controlling transmission of relatively large data object in communication system
CN106161537A (en) The processing method of remote procedure call, device, system and electronic equipment
TW201933853A (en) Binding method for user terminal and device, apparatus, and system
US9069592B2 (en) Generic transport layer mechanism for firmware communication
CN113326155A (en) Information processing method, device, system and storage medium
CN112463400A (en) Real-time data distribution method and device based on shared memory
EP1589424A2 (en) Vertical perimeter framework for providing application services in multi-CPU environments
CN113810230A (en) Method, device and system for carrying out network configuration on containers in container cluster
WO2000073902A1 (en) Single logical clipboard for multiple computers
WO2021238259A1 (en) Data transmission method, apparatus and device, and computer-readable storage medium
CN108737397B (en) Method for realizing data interaction between service and protocol stack in router
JP4518177B2 (en) Recording device, file descriptor generation method, program
US7382802B2 (en) Device for supporting NICs and TOEs under same protocol family of socket interface using IP checking mechanism
EP2765512B1 (en) Method and device for implementing communications between virtual machines based on scheduling layer
CN110895517A (en) Method, equipment and system for transmitting data based on FPGA
WO1994028486A1 (en) Method of selecting a server object to service a client object request within a network environment

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant