One of Linux inter-process communication (IPC)-MPs queue

Source: Internet
Author: User
1 pipe (PIPE)

Pipelines are the oldest form of IPC in Unix systems, and all UNIX systems provide such communication mechanisms, including Linux. The following restrictions apply to using MPs pipelines:

1. due to historical reasons, the pipeline is half-duplex and data can only flow in one direction. To achieve bidirectional communication, you must create two pipelines.

2. Both parties of the pipeline communication must be kinship-related processes (between parent and child processes or brother processes ).

2. Create an MPS queue
#include <unistd.h>int pipe(int pipefd[2]);

Two file descriptors are returned through the pipefd parameter. The pipefd [0] descriptor is used to read data in the pipeline, which can be referred to as the read end of the pipeline. The pipefd [1] file descriptor is used to write data to the pipeline, the short name is the write end of the pipeline.


The returned pipefd descriptor is in the same process and has no meaning. Generally, the pipe process is called immediately after fork, so that the pipeline from the parent process to the child process can be created (the parent process
Disable pipefd [0], sub-process to disable pipefd [1]), or from sub-process to parent process (parent process to disable pipefd [1], sub-process to disable pipefd [0]),
The pipeline is used for inter-process communication.

3. MPS queue read/write


The read and write operations of pipelines are completed by calling read and write operations. Pipefd [0] And pipefd [1] correspond to the read and write ends of the pipeline respectively. The pipefd [0] descriptor is used
Read the data in the pipeline. The pipefd [1] file descriptor is used to write data to the pipeline. If you write data to pipefd [0] or read data to pipefd [1], an error is returned.

When one end of the MPs queue is closed, follow the following rules:

1. When all write ends are closed, when all data in the pipeline is read, read returns 0 to indicate that the end of the file is reached. Theoretically, if a process does not close the write end of the pipeline, the read end will not reach the end of the file.

2. If the read end of the MPs queue is closed and data is written to the MPs queue, A sigpipe signal is generated. By default, sigpipe Signal Processing ends the current process. If the current process ignores the sigpipe signal, the write function returns-1, and errno is set to epipe.


The system constant pipe_buf defines the pipe size. This system constant is defined in limits. H. It can be queried by calling pathconf or fpathconf during runtime.
The value of pipe_buf. The pipe_buf value in Linux is 4096. If you are writing a portable program, use the macro pipe_buf instead of the value 4096.
(Pipe_buf values vary with Unix systems ).


When the data written to the MPs queue is less than or equal to pipe_buf, the system ensures that the write operation is atomic. Write will write data to the MPs queue at one time and return the data. When multiple processes write data to the MPs queue, no
Will insert. If the data volume to be written is greater than pipe_buf, the system no longer ensures that the write operation is atomic. When the pipeline has space, write will write a part of the data. When all the data is
After the MPs queue is written, write returns. When multiple processes write data to the MPs queue at the same time, insert data into the MPs queue.

4. MPS queue application instance
#include <stdio.h>#include <unistd.h>#include <errno.h>#include <stdlib.h>int main(void){int n;int fd[2];pid_t pid;char line[1024];if(pipe(fd) < 0){fprintf(stderr, "pipe error: %s\n", strerror(errno));exit(-1);}if((pid = fork()) < 0){fprintf(stderr, "fork error: %s\n", strerror(errno));exit(-1);}else if(pid > 0) /* Parent */{close(fd[1]);n = read(fd[0], line, 1024);write(STDOUT_FILENO, line, n);}else /* Child */{close(fd[0]);write(fd[1], "Hello World\n", 12);}}

We can see from the example that the pipeline data flows from the child process to the parent process. The parent process closes the write end of the pipeline, retains the read end of the pipeline, while the child process closes the read end of the pipeline, and retains the write end of the pipeline.

5. Summary of Pipelines

Through the above description, we can summarize its features:

1. One-way data flow.

2. There is no pipe name, so data can only be transferred between unrelated processes.

3. The pipeline size is pipe_buf. If the written data is not greater than this value, the system will guarantee atomic operations; otherwise, the system will not guarantee atomic operations.

4. The pipeline transmits a non-formatted byte stream. Therefore, the transmission protocol must be defined between processes at both ends of the pipeline.

 

References:

Advanced Programming in UNIX environment (aupe)

Inter-process communication in Linux (1)

Original

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.