interprocess communication (i)-pipelines

Source: Internet
Author: User

I'll use a few blogs to summarize some of the ways in which you can communicate between processes in Linux, and I'm going to make this summary part of the beginning in every blog in this series.

Ways to communicate between processes

    • Pipeline
    • Message Queuing
    • Signal
    • Signal Volume
    • Shared storage Area
    • Socket (socket)

In Linux systems where everything is documented, pipelines are also a kind of file (special files), and you can use the MKFIFO command to create a pipeline file

There is a p in front of the pipeline file to identify the pipe file

This is mainly said that through the pipeline to complete the communication between processes, through the pipeline communication there are two ways.

One is the anonymous pipeline, and the other is named pipe

    • Anonymous pipeline


Take a look at the code first

1 #defineMAXLINE 802 intMain ()3 {4     intN;5     intfd[2];6 pid_t pid;7     CharLine[maxline];8     if(Pipe (FD) <0)9Perror ("Pipe");Ten     if(PID = fork ()) <0) OnePerror ("Fork"); A     if(PID >0) -     { -         //Father the         Close (fd[0]); -     } -     Else -     { +         Close (fd[1]); -n = Read (fd[0], line, MAXLINE); +Write (stdout, line, N);//write to the standard output to see the effect A     } at     return 0; -  -}

This program is an example of a simple parent-child process communicating through a pipeline, and the specific work process I show in a drawing way

It is important to note that this step is not able to operate the pipe correctly without closing the appropriate port.

Anonymous pipeline is mainly used, when creating a child process will be the parent process file descriptor copy of this feature, through this feature, the parent-child process to see a common resource-pipeline, and at the same time have the right to the pipeline diarrhea, then one side read, one side write, you can complete the communication between the process.

The so-called anonymous pipe means, no name ... You have no idea where this pipe file is stored, and you don't know the file name of the pipe file, only the file descriptor in the parent-child process that is associated with the pipeline file. Then, depending on the implementation mechanism of the anonymous pipeline, it is easy to see his merits and demerits.

    • n Characteristics of a pipe
      • Pipelines are dependent on the file system, and after creating the pipeline, be sure to close the unused read-write end
      • Only a parent-child process can use pipeline communication, which is known as a blood-related process for interprocess communication. (anonymous pipe exclusive)
      • The pipeline is based on the data stream, and it faces the byte stream!
      • Pipelines can only be called unidirectional data communication, not even half-duplex
      • Synchronization and mutex issues do not need to be considered, the pipeline has been considered
      • When a parent-child process exits, the pipeline's life cycle is over, meaning that the pipeline's life cycle is the process

The above is the use and implementation mechanism of anonymous pipelines, it can be seen that there must be "affinity" between processes can use anonymous pipes to complete interprocess communication. Father and son process can of course, "grandson" process is also possible ~

So in order to solve the problem that only the affinity process can use this way to communicate, there is a way to communicate with named pipes

    • Named pipes

Simply put, the anonymous pipeline we just used is because we don't know the file name and the path, so we can only get the way to connect to the anonymous pipe by inheriting the file descriptor table, if we know the path and the pipe file name? So you can complete non-affinity interprocess communication?

interprocess communication (i)-pipelines

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.