Message transmission for inter-process communication in linux

Source: Internet
Author: User
In linux inter-process communication, message transmission is mainly divided into pipelines, FIFO, and message queue (1) pipelines are created by the pipe function and provide a single (one-way) data stream. The pipe function returns two file descriptors: fd [0] and fd [1]. The former is used for open reading, and the latter is used for open writing. The MPs queue has no name, so it can only be associated with a process.

In linux inter-process communication, message transmission is divided into pipelines, FIFO, and message queues.
(1) pipelines
The pipeline is created by the pipe function and provides a single (one-way) data stream. The pipe function returns two file descriptors: fd [0] and fd [1]. The former is used for open reading, and the latter is used for open writing. The pipeline has no name, so it can only be used by a kinship process. Although the pipeline is created by a single process, it is rarely used within a single process. The typical purpose of a pipeline is to provide communication between two different processes (one is a parent process and the other is a child process. First, a process (which will become the parent process) creates a pipeline and calls fork to derive its own copy. Then, the parent process closes the reading end of the MPs queue, and the child process closes the writing end of the same MPs queue. Or the parent process closes the write end of the MPs queue, and the child process closes the read end of the same MPs queue. This provides a one-way data flow between parent and child processes.

(2) FIFO
FIFO refers to First in, First out. in linux, FIFO is similar to a pipeline. It is a one-way (half-duplex) data stream. Unlike pipelines, each FIFO has a path name associated with it, allowing unrelated processes to access the same FIFO. FIFO is also known as a famous pipe. FIFO is created by the mkfifo function. Pathname is a common Unix path name, which is the name of the FIFO. The mkfifo function has implicitly specified O_CREAT | O_EXCL. that is, it creates a new FIFO or returns an EEXIST error (if the specified FIFO name already exists ). If you do not want to create a new FIFO, call open instead of mkfifo. to open an existing FIFO or create a new FIFO, call mkfifo first, and then check whether it returns the EEXIST error. if this error is returned, call open. the mkfifo command can also be used to create a FIFO. You can use it in shell scripts or command lines. After a FIFO is created, it must be read or written. it can be an open function or a standard I/O open function. FIFO cannot be split to read and write because it is half duplex. Write operations on pipelines or FIFO queues always add data to the end, and read operations on them always return data from the beginning. If lseek is called for the pipeline or FIFO, the ESPIPE error is returned.

(3) Posix Message Queue
A message queue can be considered as a message linked list. A thread with sufficient write permission can place messages in the queue, and a thread with sufficient read permission can take messages from the queue. Each message is a record, which is assigned a priority by the sender. Before a process writes a message to a queue, another process does not need to wait for the message to arrive in the queue. This is the opposite of pipelines and FIFO. for the latter, unless the reader already exists, it makes no sense to have the writer first. A process can write some messages to a queue, terminate the process, and then let another process read the messages at a later time. Message queues are persistent with the kernel, which is different from pipelines and FIFO. Posix message queue and System V message queue. There are many similarities between the two groups of functions, but there are also major differences
1. read messages to Posix message queues always return the earliest messages with the highest priority. read messages to System V message queues can return messages with any specified priority.
2. when a message is placed in an empty queue, the Posix message queue can generate a signal or start a thread. System V message queue does not provide similar mechanisms.

Each message in a queue has the following attributes:
1. an unsigned integer priority (Posix) or a long integer (System V ).
2. the length of the data part of the message (which can be 0 ).
3. data itself (if the length is greater than 0)

Function interface
1. mqd_t mq_open (const char * name, int oflag ,...)
The mq_open function creates a new message queue or opens an existing message queue.
2. int mq_close (mqd_t mqdes );
The mq_close function closes a message queue.
3. int mq_unlink (const char * name );
Delete a name used as the first parameter from the system.
4. int mq_getattr (mqd_t mqdes, struct mq_attr * attr );
Int mq_setattr (mqd_t mqdes, const struct mq_attr * attr, struct mq_attr * oattr );
Each message queue has four attributes. mq_getattr returns all these attributes, and mq_setattr sets one of them.
Struct mq_attr {
Long mq_flags;
Long mq_maxmsg;
Long mq_msgsize;
Long mq_curmsgs;
};

5. int mq_send (mqd_t mqdes, const char * ptr, size_t len, unsigned int prio );
Int mq_receive (mqd_t mqdes, char * ptr, size_t len, unsigned int * priop );

The mq_send function writes messages to the message queue, and the mq_receive function reads messages from the message queue.

6. int mq_notify (mqd_t mqdes, const struct sigevent * motification );
Struct:
Union sigval {
Int sival_int;
Void * sival_ptr;
};

Struct sigevent
{
Int sigev_notify;
Int sigev_signo;
Union sigval sigev_value;
Void (* sigev_policy_function) (union sigval );
Pthread_attr_t * sigev_policy_attributes;
};

The mq_notify function creates or deletes asynchronous event notifications for a specified queue. Some rules that are generally applicable to this function
1) if the notification parameter is not empty, the current process will be notified when a message reaches the specified queue that was previously empty. We said, "This process is registered to receive notifications from this queue ".
2) if the notification parameter is a null pointer and the current process is currently registered to receive notifications from the specified queue, the existing registration will be canceled.
3). at any time, only one process can be registered to receive notifications from a given queue.
4 ). when a message reaches a queue that was previously empty and a process has been registered to receive notifications from the queue, the notification is sent only when no thread is blocked in the mq_receive call of the queue. This means that blocking in mq_receive calls takes precedence over registration of any notification.
5). when the notification is sent to its registration process, its registration is revoked. The process must call mq_policy again to re-register (if necessary ).

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.