Author:Wang Shanshan, a lecturer at Huaqing vision embedded College.
The process communication methods in Linux are basically inherited from the process communication methods on the Unix platform. The Bell Labs and BSD (Berkeley Software release center at the University of California Berkeley), which have contributed significantly to the development of Unix, have different emphasis on inter-process communication. The former improves and expands the inter-process communication methods in the early days of Unix, and forms the "system
The communication process is limited to a single computer. The latter skips this restriction and forms a socket-based inter-process communication mechanism. In Linux, the two are inherited, as shown in:
Among them, the original Unix IPC includes: Pipeline, FIFO, signal; System v ipc includes: SySTem V message queue, System
V signal lamp and System V shared memory zone; POSIx IPC includes:
Posix message queue, Posix signal lamp, and Posix shared memory zone. There are two points to explain:
1) due to the diversity of Unix versions, the Institute of electronic and electrical engineering (IEEE) developed an independent Unix standard, this new ANSI Unix standard is called the portable operating system interface (PSOIX) in the computer environment ). Most of the existing Unix and popular versions follow the POSIX standard, while Linux has followed the POSIX standard from the very beginning;
2) BSD is not involved in inter-process communication within a single machine (socket itself can be used for inter-process communication within a single machine ). In fact, many Unix versions of Single-host IPC have traces of BSD, such as the anonymous memory ing supported by 4.4BSD and the realization of reliable signal semantics by 4.3 + BSD.
Introduction to several main methods for inter-process communication in linux:
1. MPS queue
The pipeline is the oldest method of inter-process communication. It includes two types: anonymous pipeline and famous pipeline. The former can be used for inter-process communication with kinship, it can be used for communication between the parent process and the child process. The latter overcomes the restriction that the MPs queue has no name. Therefore, in addition to the functions of the former, it also allows communication between unrelated processes and can be used for communication between any two processes running on the same machine.
The unknown pipeline is created by the pipe () function:
# Include <unistd. h>
Int pipe (int filedis [2]);
The filedis parameter returns two file descriptors: filedes [0] is read and filedes [1] is write. Output of filedes [1] is the input of filedes [0.
In Linux, famous pipelines can be created in two ways: Command Line mknod System Call and function mkfifo. The following two channels generate a famous pipe named myfifo under the current directory:
Method 1: mkfifo ("myfifo", "rw ");
Method 2: mknod myfifo p
After a famous pipeline is generated, you can use common file I/O functions such as open, close, read, and write to operate it.
2. Message Queue
Message Queue is a chain table of messages, including Posix Message Queue system V message queue. Message Queue is used for communication between processes running on the same machine. It is similar to the pipeline. A process with sufficient permissions can add messages to the queue, A process with read permission can read messages in the queue. The message queue overcomes the disadvantages of low signal carrying information, and the pipeline can only carry unformatted byte streams and limited buffer size. We can replace it with a Stream pipeline or a set of interfaces.
3. Shared Memory
Shared memory is the fastest way to communicate between processes running on the same machine, because data does not need to be copied between different processes. A shared memory area is usually created by a process, and other processes read and write this memory area. Shared Memory is often used in conjunction with other communication mechanisms, such as semaphores, to achieve synchronization and mutual exclusion between processes.
The first function to use is shmget, which obtains a shared storage identifier.
# Include <sys/types. h>
# Include <sys/ipc. h>
# Include <sys/shm. h>
Int shmget (key_t key, int size, int flag );
This function is similar to the familiar malloc function. The system allocates the size of memory as the shared memory according to the request. In the Linux kernel, each IPC structure has a non-negative integer identifier, So that you only need to reference the identifier when sending a message to a message queue. This identifier is obtained by the key word of the IPC structure in the kernel. This keyword is the key of the first function above. The data type key_t is defined in the header file sys/types. h. It is a long integer data. In our subsequent chapters, we will also encounter this keyword.
After the shared memory is created, other processes can call shmat () to connect it to their own address space.
Void * shmat (int shmid, void * addr, int flag );
Shmid is the shared storage identifier returned by the shmget function. The addr and flag parameters determine how to determine the connection address. the return value of the function is the actual address connected to the Data Segment of the process, A process can perform read/write operations on the process.
When using shared storage to implement inter-process communication, note the synchronization of data access. Make sure that the desired data has been written when a process reads data. Generally, semaphores are used to synchronize access to shared storage data. In addition, you can use the shmctl function to set some flags of shared storage memory, such as SHM_LOCK and SHM_UNLOCK.
4. Semaphores
Semaphores, also known as semaphores, are used to coordinate data objects between different processes. The most important application is the shared memory mode of inter-process communication in the previous section. Essentially, semaphores are a counter used to record access to a resource (such as shared memory. Generally, to obtain shared resources, the process must perform the following operations:
(1)
Test the semaphore that controls the resource.
(2) If the semaphore value is positive, the resource can be used. The process minus 1.
(3) If the semaphore is 0, the resource is currently unavailable, and the process enters the sleep state until the signal value is greater than 0. The process is awakened and transferred to step (1 ).
(4) When a process no longer uses a semaphore-controlled resource, the signal value is increased by 1. If a process is sleeping and waiting for this semaphore at this time, it will be awakened.
The Linux Kernel Operating system rather than the user process is used to maintain the semaphore state. We can see the definition of each structure used by the kernel to maintain the semaphore state from the/usr/src/linux/include/linux/sem. h file. Semaphores are a set of data. You can use each element of this set separately. The first function to be called is semget, which is used to obtain a semaphore ID.
# Include <sys/types. h>
# Include <sys/ipc. h>
# Include <sys/sem. h>
Int semget (key_t key, int nsems, int flag );
The key is the keyword of the IPC structure discussed earlier. It will decide whether to create a new semaphore set or reference an existing semaphore set in the future. Nsems is the number of semaphores in the set. If you create a new set (generally on the server), you must specify nsems. If you reference an existing set of semaphores (usually on the client), you can specify nsems as 0.
The semctl function is used to operate on semaphores.
Int semctl (int semid, int semnum, int cmd, uniON semun arg );
Different operations are implemented through the cmd parameter. Seven different operations are defined in the header file sem. h. You can refer to the actual programming.
The semop function automatically executes the operation array on the semaphore set.
Int semop (int semid, struct sembuf semoparray [], size_t nops );
Semoparray is a pointer that points to an array of semaphore operations. Nops specifies the number of operations in the array.
Next, let's look at a specific example. It creates a keyword for a specific IPC structure and a semaphore, creates an index for this semaphore, and modifies the semaphore value pointed to by the index, finally, we clear the semaphore.
5. Set of interfaces
Socket programming is one of the main ways to implement inter-process communication between Linux and most other operating systems. The well-known WWW Service, FTP service, and TELNET service are implemented based on a set of interface programming. In addition to remote computer processes, the set of interfaces also apply to inter-process communication within the same local computer.
"This article is written by Hua Qing vision http://www.embedu.org/index.htm"