Linux System Development 5 interprocess Communication Pipe () FIFO () mmap ()

Source: Internet
Author: User


" This article declined to reprint , originally from http://990487026.blog.51cto.com"



Linux System Development 5 interprocess Communication Pipe () FIFO () mmap () pipe () Pipeline Communication Introduction pipe () parent-child process communication case pipe () using piping has some restrictions pipe () pipe buffer size pipe () read-side non-blocking pipeline FIFO () Pipeline file FIFO () write end/Read End program pipeline file size on disk is 0mmap () map file to Memory mmap () write end/Read End program mmap () transfer structure data, delete temporary file



Pipe () Piping communication

A pipe works between processes that have a learner relationship, passing through a fork ().

Pipe in kernel space, first pipe in fork, process inheritance


Piping is the most basic IPC mechanism, created by the pipe function:

#include <unistd.h>

int pipe (int filedes[2]);

A pipe acts on a blood-related process, passing by fork.

Calling the pipe function creates a buffer (called a pipe) in the kernel for communication, which has a read-

Write end, and then pass the Filedes parameter out to the user program two file descriptor, Filedes[0] point to the pipe read

End, filedes[1] points to the write end of the pipeline (very well, as if 0 is standard input 1 is the standard output). So the pipeline

The user program looks like an open file via Read (Filedes[0]), or write (filedes[1]);

Reading and writing data to this file is actually reading and writing the kernel buffer. The pipe function call successfully returned 0, and the call failed to return

Back-1.

How can communication between two processes be realized after the pipeline is opened? For example, you can follow the steps below to communicate.

Look at the picture

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/85/90/wKiom1eohn6RznJNAAHIcKSe_OA351.png "title=" Pipe.png "alt=" Wkiom1eohn6rznjnaahickse_oa351.png "/>





1. The parent process calls pipe to open a pipeline and gets two file descriptors pointing at both ends of the pipeline.

2. The parent process calls fork to create a child process, and the child process also has two file descriptors pointing to the same pipe.

3. The parent process closes the pipe read end, and the child process closes the pipe write end. The parent process can write to the pipeline, and the child process can move from

The pipeline is read, the pipeline is implemented by a ring queue, the data flows from the writing end flow from the reading end, thus realizing the interprocess

Letter.

Example Pipe pipe

[email protected]:~/linux_c$ cat main.c  #include  <stdio.h> #include  < stdlib.h> #include  <string.h> #include  <unistd.h> #include  <errno.h> #include  <sys/types.h> #include  <sys/wait.h>int main (Int num,char **args,char  **env) {int fd[2] = {0};char buf[1024] =  "Hello World!  "; Pid_t pid = 0;if (Pipe (FD)  == -1) {perror ("pipe"); exit (1);} Pid = fork ();//Parent Write child read if (pid >0)//In parent process, close parent Read {close (fd[0]); sleep (1);//wait n seconds   send data to Child process write (fd[1 ],buf,strlen (BUF)); wait (NULL);} Else if (pid == 0)//in child process, close sub-write {char buf[1024] = {0};int len = 0; Close (fd[1]);//Closed write-end Len = read (fd[0],buf,sizeof (BUF)); sprintf (buf, "%s\n", buf); Write (stdout_fileno,buf , strlen (BUF));} return 0;} [email protected]:~/linux_c$ gcc -o app main.c  && ./apphello world!  [email protected]:~/linux_c$ 



There are some restrictions on using pipelines:


Two processes can only implement one-way communication through a pipeline, such as the above example, the parent process writes the child process to read, if

Sometimes a child process is required to write the parent process to read, and another pipeline must be opened. Ask the reader to think, if you only open a pipe,

But the parent process does not shut down the read end, the child process does not shut down the write end, both sides have read and write end, why can not achieve two-way

Communication?

The read-write side of the pipeline is passed through an open file descriptor, so two processes to communicate must be from their public

Ancestors there inherited pipeline file descriptors. The example above is when the parent process passes the file descriptor to the child process

, the parent process can fork two times, pass the file descriptor to two sub-processes, and then two sub-processes pass through a

Letter, in short you need to pass the file descriptor by fork so that two processes can access the same pipeline before they can communicate.

There are 4 special cases to be aware of when using pipelines (assuming all blocking I/O operations, without setting the O_nonblock label

LOG):

1. If all the file descriptors that point to the pipe write end are closed (the reference count for the pipe write is equal to 0), still

Then the process reads the data from the read-side of the pipeline, then the remaining data in the pipeline is read, and again read returns 0,

Like reading at the end of a file.

2. If there is a file descriptor pointing to the end of the pipe is not closed (the reference count of the pipe writes is greater than 0), and the

The process of the writing end does not write data to the pipeline, when there is a process reading the data from the pipe, then the remaining number in the pipeline

Read, and then read again blocks until the data is readable in the pipeline and is returned.

3. If all the file descriptors pointing to the pipe read end are closed (the reference count for the pipe read is equal to 0), then

There is a process to write to the pipeline, then the process receives a signal sigpipe, which usually causes the process to terminate unexpectedly. Speak

The signal tells you how to make the sigpipe signal do not terminate the process.

4. If there is a file descriptor pointing to the pipe read end is not closed (the reference count of the pipe read is greater than 0), and the

The process of the read end does not read the data from the pipeline, when the process writes the data to the pipeline, then when the pipeline is full

Write again blocks until you have empty positions in the pipeline before writing the data and returning it.

These four special cases of pipelines have universal significance.

Non-blocking pipeline, Fcntl function set O_nonblock flag

fpathconf (int fd, int name) tests the pipe buffer size, _pc_pipe_buf



Test Pipe buffer size

[Email protected]:~/linux_c$ cat main.c #include <stdio.h> #include <stdlib.h> #include <string.h># Include <unistd.h> #include <errno.h> #include <sys/types.h> #include <sys/wait.h>int main (int Num,char **args,char **env) {int fd[2] = {0};p ipe (FD);p rintf ("%ld \ n", fpathconf (FD[0],_PC_PIPE_BUF)); return 0;} [Email protected]:~/linux_c$ gcc main.c &&/a.out4096 [email protected]:~/linux_c$


Pipe () read-side non-blocking piping






This article from "Soul Bucket Luo" blog, declined reprint!

Linux System Development 5 interprocess Communication Pipe () FIFO () mmap ()

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.