How to "Go" based on the Linux process communication design scheme

Source: Internet
Author: User
Tags int size message queue posix semaphore

Objective

The process communication methods under Linux are basically inherited from the process communication methods on the UNIX platform. The two main players at T-Lab and BSD (UC Berkeley's Berkeley Software Release Center), which have made significant contributions to the development of UNIX, have a different focus on interprocess communication. The former makes a systematic improvement and expansion of the inter-process communication means of UNIX, which forms the "system V IPC", the communication process is confined to a single computer, the latter skips the limit and forms the inter-process communication mechanism based on socket. Linux has inherited both:

Among them, the original UNIX IPC includes: pipeline, FIFO, signal; System V IPC includes: System V Message Queue, System V Semaphore, System v shared memory area; POSIX IPC includes: POSIX Message Queuing, POSIX semaphore, POSIX shared memory area. There are two points to explain briefly:

1) due to the diversity of UNIX versions, the Institute of Electrical and Electronic Engineering (IEEE) has developed an independent UNIX standard, the new ANSI UNIX standard known as computational fatty for the bindings GUI ≒soix). Most of the existing UNIX and popular versions follow the POSIX standard, and Linux follows the POSIX standard from the start;

2) BSD is not a non-interprocess communication within a single machine (the socket itself can be used for interprocess communication within a single machine). In fact, many UNIX versions of the stand-alone IPC are left with BSD traces, such as the 4.4BSD supported anonymous memory mapping, the implementation of 4.3+BSD for reliable signal semantics, and so on.

Inter-process communication under Linux

Introduction to several major instruments:

1. Piping

Pipeline is the oldest way of interprocess communication, it includes nameless pipes and two kinds of famous pipes, the former can be used for communication between kinship processes, and can be used for communication between parent process and child process, which overcomes the limitation of pipe without name, so, in addition to the function that the former has, It also allows non-affinity interprocess communication to be used for any two interprocess communication running on the same machine.

Nameless pipes are created by the pipe () function:

#include

int pipe (int filedis);

Parameter Filedis returns two file descriptor: filedes[0] Open for read, filedes for write. The output of the filedes is the input of filedes[0].

Under the Linux system, a well-known pipeline can be created in two ways: command line Mknod system calls and function Mkfifo. The following two paths are generated in the current directory with a named Myfifo pipeline:

Mode one: Mkfifo ("Myfifo", "RW");

Mode two: Mknod Myfifo p

Once a named pipeline is generated, it can be manipulated using generic file I/O functions such as open, close, read, write, and so on.

2. Message Queuing

Message Queuing is a linked table of messages, including POSIX Message Queuing system V Message Queuing. Message Queuing is used to run interprocess communication on the same machine, which is similar to a pipeline where a process with sufficient permissions can add messages to the queue, and a process that is given Read permission can read the messages in the queue. Message queue overcomes the disadvantage that the signal carrying information is low, the pipeline can only carry the unformatted byte stream and the buffer size is limited.

We can replace it with a flow pipe or a socket interface.

3. Shared memory

Shared memory is the fastest way to communicate between processes running on the same machine because the data does not need to replicate between different processes. A shared memory area is typically created by a process, and the remaining processes read and write to the 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 be used is shmget, which obtains a shared storage identifier.

#include

#include

#include

int Shmget (key_t key, int size, int flag);

This function is somewhat similar to the familiar malloc function, which is used as a shared memory by the request to allocate size memory. A non-negative integer identifier for each IPC structure in the kernel of the Linux system so that a message queue is sent with a reference to the identifier. This identifier is the kernel of the IPC structure of the keyword, this keyword is the first function above the key. The data type key_t is defined in the header file Sys/types.h, which is a long-shaped data. This keyword is also encountered in the chapters that follow us.

When shared memory is created, the rest of the process can call Shmat () to connect it to its own address space.

void *shmat (int shmid, void *addr, int flag);

Shmid the shared storage identifier returned for the Shmget function, the addr and flag parameters determine how the address of the connection is determined, the return value of the function is the actual address to which the process data segment is connected, and the process can read and write to the process.

The point of note for interprocess communication using shared storage is to synchronize data access, and you must ensure that when a process reads the data, the data it wants is already written. Typically, semaphores are used to synchronize the access to shared storage data, and in addition, some flags such as Shm_lock, Shm_unlock, and so on, can be set by using the SHMCTL function for shared storage memory.

4. Signal Volume

Semaphores are also called semaphores, which are used to coordinate data objects between different processes, and the most important application is interprocess communication in the previous section of shared memory mode. Essentially, a semaphore is a counter that is used to record access to a resource, such as shared memory. In general, in order to get a shared resource, the process needs to do the following:

(1) test the semaphore that controls the resource.

(2) If the value of this semaphore is positive, the resource is allowed to be used. The process reduced the number of incoming numbers by 1.

(3) If the semaphore is 0, the resource is currently unavailable, the process goes to sleep until the semaphore value is greater than 0, the process is awakened, and the step is transferred (1).

(4) When a process is no longer using a semaphore-controlled resource, the semaphore value is added to 1. If there is a process waiting for this semaphore at this time, the process is awakened.

The status of the semaphore is maintained by the Linux kernel operating system and not by the user process. We can see the definition of each structure that the kernel uses to maintain semaphore status from the/usr/src/linux/include/linux/sem.h file. A semaphore is a collection of data that a user can use individually for each element of the collection. The first function to invoke is Semget, which is used to obtain a semaphore ID.

#include

#include

#include

int Semget (key_t key, int nsems, int flag);

Key is the keyword for the IPC structure that was previously spoken, and it will decide in the future whether to create a new semaphore collection or to reference an existing semaphore collection. Nsems is the number of semaphores in the collection. If you are creating a new collection (typically in the server), you must specify Nsems, or if you are referencing an existing semaphore collection (typically in the client), specify Nsems as 0.

The SEMCTL function is used to manipulate the semaphore.

int semctl (int semid, int semnum, int cmd, Union semun Arg);

The different operations are implemented by the CMD parameter, which defines 7 different operations in the header file sem.h, which can be used as reference in the actual programming.

The SEMOP function automatically executes an array of operations on the semaphore set.

int semop (int semid, struct sembuf semoparray[], size_t nops);

Semoparray is a pointer to an array of semaphore operations. NOPS Specifies the number of operations in the array.

Below, let's look at a specific example, it creates a specific IPC structure of the keyword and a semaphore, establishes the index of this semaphore, modifies the value of the semaphore to which the index points, and finally clears the semaphore.

5. Socket connector

Socket programming is one of the main ways to implement inter-process communication between Linux and most other operating systems. Our well-known WWW service, FTP service, Telnet service, etc. are all based on a set of interface programming. The socket interface is also suitable for inter-process communication within the same computer, in addition to the offsite computer processes.

Source

How to "Go" based on the Linux process communication design scheme

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.