To know how to develop software for your network, you must first understand interprocess communication (IPC). --richard Stevens
A large or large application that can be designed as a single large program, and a better way is to design it as a set of pieces of program that communicate with each other. It is typically implemented with multiple processes, each of which contains several threads. In this way, the threads within the process need to communicate, and the different processes need to communicate.
Interprocess communication can achieve the purpose of data transmission, sharing, event notification, process control, etc., including:
Data Transfer : One process sends the information to another process.
data sharing : Multiple processes read and write shared data.
Event Notification : A process sends a message to another process notifying it that an event has occurred.
Process Control : a process that wants to fully control the execution of another process, such as the debug process, to intercept all the falls and exceptions of a process, such as debug, and to be able to know its state changes in a timely manner. 1 Inter-process communication mode
Linux processes are all inherited from Unix, while the two main at&t of Bell Labs and BSD (UC Berkeley's Berkeley Software Publishing Center), which make a significant contribution to UNIX development, have different priorities for interprocess communication. The former has systematically improved and expanded the early inter-process communication mode of UNIX, formed "System V IPC", the communication process is confined to a single host, the latter has skipped the limit, and formed the interprocess communication mechanism based on socket. Linux inherits both. Reproduced
POSIX is a "Portable Operating System Interface" (Portableoperating system Interface) that is not a single standard, but a series of standards developed by the Institute of Electrical and Electronic Engineers and IEEE. The POSIX standard is also an international standard adopted by ISO and IEC (International Electrotechnical Commission).
XSI is the X/open system interface (X/opensystem Interface). Open Group is an organization incorporated in 1996 by X/open Inc. (established in 1984) and The Open Software Foundation (osf,1988). It is an international organization composed of manufacturers, end-users of the industry, government departments and academic institutions.
The IPC mechanism for process communications (such as Message Queuing, semaphores, and shared memory) on System V systems is often referred to as System V IPC. It was also called XSi IPC because it was later included in the XSI standard of UNIX. So system V IPC and XSI IPC actually refer to the same kind of thing.
2 ways to share information
Multiple processes are running on one system, each with its own address space. There are several ways to share information between UNIX processes.
The two processes on the left share some of the information that is persisted on a file in the file system. Some form of synchronization is necessary when a file is to be updated. such as read, write, Lseek, and so on, each process has to traverse the kernel to access this information.
The two processes in the middle share some of the information that resides in the kernel. such as piping, System V Message Queuing, System V semaphore. Each operation that accesses the shared information involves a system call to the kernel.
The two processes on the right have a shared memory area that both sides can access. Once each process is set up to change the shared memory area, it can access the data at all without involving the kernel. Processes that share this memory area require some form of synchronization.
3 IPC 3.1 IPC Objects
An IPC object can be any of three types of message queues, semaphores, shared storage, and so on. It is a tool for interprocess communication between activities at the kernel level.
The IPC object is referenced and accessed through its identifier , which is a non-negative integer that uniquely identifies an IPC object. When an IPC object is created or deleted, the corresponding identifier is continuously added to 1 until the maximum positive value of an integer is reached, and then to 0.
An IPC object's identifier resolves only internal access to an IPC object, and how multiple processes can access a particular IPC object requires a foreign key (key), and each IPC object is associated with a key. This solves the problem of multiple processes converging on an IPC object.
3.2 IPC Object view and delete
Because the three types of system V IPC are not identified by path names in the file system, they cannot be seen with standard LS and RM programs and cannot be deleted. However, any system that implements these types of IPC provides two special programs: IPCS and IPCRM.
IPCS outputs various information about the system VIPC feature, Ipcrm deletes a System V message queue, semaphore, or shared memory area.
lincoln@ubuntu:~$ ipcs
------Shared Memory segments--------
key shmid owner perms bytes nattch Status
------semaphore Arrays--------
key semid owner perms Nsems
------message queues--------
key msqid owner perms used-bytes messages
lincoln@ubuntu:~$ Mans Ipcrm
IPCRM (1) Linux Programmer ' s Manual IPCRM (1)
NAME
ipcrm-remove A message queue, semaphore set or Sha Red Memory ID
Synopsis
IPCRM [-M key |-M ID |-Q key |-Q ID |-S key |-S ID] ...
3.3 The persistence of IPC objects
The persistence of an IPC object is how long the object has been in existence. As shown in Figure four, three types of persistence are shown.
3.4 IPC Namespaces
When two or more unrelated processes use a type of IPC object to exchange information with one another, the IPC object must have a certain form of name (name) or identifier (identifier) so that one of the processes (often the server) can create the IPC object. The remaining processes can specify the same IPC object.
For a given IPC type, the collection of possible names is called its namespace (name space). Namespaces are important because, for all forms of IPC except ordinary pipes, names are the means by which customers and servers connect to each other in exchange for information.
IPC type |
namespaces |
Identity |
Pipeline FIFO |
No Name path name |
descriptor Descriptor td> |
POSIX Message Queuing POSIX well-known semaphore POSIX based on memory semaphore POSIX shared store |
POSIX IPC name POSIX IPC name No name Posix IPC name |
mqd_t value sem_t value sem_t pointer Descriptor |
System V Message Queuing System V Semaphore System V shared memory |
key_t key key_t key key_t key |
System V IPC identifier System V IPC identifier System V IPC identifier |
TCP sockets UDP sockets Unix domain sockets |
IP addresses and TCP ports IP addresses and UDP Port Path name |
Descriptor Descriptor Descriptor |