Talk C chestnuts together (87th: C-language Instance--interprocess communication using pipelines Overview)

Source: Internet
Author: User

Hello, crossing. What we said last time is a sample of interprocess communication. This time we say the example is: Use the pipeline for interprocess communication .

Gossip Hugh, words return to the positive.

Let's talk C chestnuts together!

We are in front of the Zhanghuizhong introduced the use of pipelines to process the communication, the time is not long, I believe you remember. Today. We describe the second way of interprocess communication: pipelines. As we all know, when we use signals to communicate between processes, we essentially send a numeric value, and pretend that there is nothing to signal when sending some data between processes. At this point, the pipeline comes in handy.

We are able to send data between processes through a pipeline .

Next, let's give you a concrete introduction to the pipeline.

Piping is similar to the water pipe in our life, it can bring the tap into the home. There is also a pipeline in the Linux command. Usually use the "|" To represent the pipeline. Let's take a sample of the pipeline used in a Linux command:

 env | grep bash     //在终端中输入下面命令而且 执行SHELL=/bin/bash      //命令执行的结果

In the example above, we first use the ENV output environment variable and then pipe the contents of the ENV command output as input to the grep command. The grep command retrieves a bash in the input and displays the results of the search. That is the result of the command execution we see.

The pipelines we use in the process are similar to those used in the example above: entering data within one process and outputting data within another process.

Two processes send data through a pipeline.

we will introduce three types of pipelines and how to use them:

    • The first kind of pipe I call a pseudo-pipe.
    • The second kind of pipeline I call the entry-level pipeline.
    • The third pipe is the true conduit.

The pseudo-pipe uses popen/pclose to open a file stream and then uses the I/O function to operate the convection.

Because the Popen is a certain Linux command. And it executes the Linux command by starting the shell. So the performance is low. Its effect and the end of the pipeline command "|" The same effect. That's why I call it a pseudo-pipe.

The entry-level pipeline creates a pipe through the pipe and returns two file descriptive descriptors. The returned FD is then manipulated using the Read/write system call to implement the process communication function. This method needs to be used in conjunction with the fork function. Since two FD is in the same array, most of the arrays are local variables. Local variables can only be used in parent-child processes created by fork.

Of course, assuming that the FD array is defined as a global variable is also possible. The risk of a global variable is just too great.

The real pipeline needs to use MKFIFO to create a pipeline file that returns an FD. Next, you can manipulate FD using system calls such as Open/read. We are able to manipulate the pipeline file as if it were a normal operation. The pipeline also has another name: Named Pipes (FIFO).

Crossing, let's just make a major outline of the pipeline today. As for how to use the pipe, we will describe it in the chapter back.

At the same time, we will also describe how to use pipelines to communicate between processes.

Crossing, here's a sample of interprocess communication using pipelines. I want to know what the following example, and listen to tell.

Talk C chestnuts together (87th: C-language Instance--interprocess communication using pipelines Overview)

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.