Talk C together (88th back: C language instance-use pipelines for inter-process communication 1)

Source: Internet
Author: User

Talk C together (88th back: C language instance-use pipelines for inter-process communication 1)

Hello, everyone. In the previous review, we talked about the example of using pipelines for inter-process communication. The example is as follows:Use the first pipeline for inter-process communication. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!

We introduced three types of pipelines in the previous step. In this case, we introduced the first type of pipelines (pseudo pipelines) and their usage methods. It mainly helps you understand how to use pipelines for inter-process communication.

The first pipeline is called a pseudo pipeline.. Before introducing it, we will introduce two functions:Popen and pclose.

Popen function prototype
FILE * popen(const char * command, const char *open_mode)
The first parameter is a string. You can input related strings as required by the program. The second parameter is the method for opening the pipeline. You can only select one open mode from r and w. R/w indicates reading/writing data from the pipeline. This function returns the file stream pointer. Therefore, we can operate it like an operating file. The common function is fread/fwrite.

As you can see, this function is used to open a pipeline and then operate the pipeline in the process. The first parameter of the function is a string. We can pass various Linux terminal commands to it, while Linux commands can start related processes so that pipeline operations can be performed in the process, A common operation is data transmission.

Pclose function prototype
int pclose(FILE *stream )
This function has only one parameter. It is a pointer of the file stream type, indicating the file stream to be closed. The return value of this function is the exit code at the end of the process where the parameter points.

As you can see, this function is often used to close the pipeline opened by the popen function, so it needs to be used with the popen function and is often used at the end of the process.

MPs queue usage

After understanding these two functions and their usage, we will introduce how to use pseudo-pipeline. The details are as follows:

1. use the popen function to open a pipeline. You can only specify the pipeline as r or w as needed. 2. use the fread/fwrite function to read/write data from the pipeline. 3. use the pclose function to close the pipeline opened in step 1.

Next we will use specific examples to illustrate,The detailed code is as follows::

Int main () {char buffer [BUFSIZ + 1]; int read_count = 0; FILE * p_read, * p_write; memset (buffer, '\ 0', sizeof (buffer )); p_read = popen ("env", "r"); // open the pipeline if (NULL = p_read) {printf ("open pipe for reading failed \ n "); return 1;} p_write = popen ("grep bash", "w"); // open the pipeline if (NULL = p_write) {printf ("open pipe for writing failed \ n"); return 1;} read_count = fread (buffer, sizeof (char), BUFSIZ, p_read); while (read_count> 0) {fwrite (buffer, sizeof (char), strlen (buffer), p_write); read_count = fread (buffer, sizeof (char), BUFSIZ, p_read);} pclose (p_read ); // close the MPs queue pclose (p_write); // close the MPs queue return 0 ;}

From the code above, we can see that two processes are started by passing the env and grep commands and these two Linux commands in the popen function. Both processes perform related operations on the pipeline:

The process started by the env command stores data in the pipeline, and the data content is the running result of the env command. The process started by the grep command reads the data stored by the process where the env command is located from the pipeline, and output the result.

For the r/w open method used in the popen function, some readers may have doubts: the env-started process stores data in the pipeline and should open the pipeline by writing, how can I use the read method to open it? I will explain the running process of the program to you and you will be able to understand the reasons:

For the sake of convenience, we assume that the process where the main function is located is A, the process where env starts is B, and the process started by grep is C. Process A opens the pipeline in Read mode and starts process B. Read means process A can read content from process B, which is the output of process B. Process A reads the content and stores it in the MPs queue. Similarly, process A opens the pipeline in write mode and starts C. Write mode indicates that process A can write content to process C, process A stores the data in the MPs queue. Process C reads the data from the pipeline, processes the data, and outputs the processing result, that is, the result of process A running.

After this analysis, we can clearly see how data is transmitted between processes using pipelines, thus achieving communication between processes.

The following is the running result of the program. For more information, see:

./S // run the compiled program SHELL =/bin/bash // display the program output result

We have said that this pipeline is a pseudo pipeline because the parameter of popen is a Linux Command and it runs the Linux Command by starting the shell. It works the same as the pipeline command "|" in the terminal.

The following is the result of using pipelines on Linux terminals.:

Env | grep bash // enter the command in the terminal and run SHELL =/bin/bash // command to run the result

You can compare the running results of the above programs and find that their running results are exactly the same.

The readers will not write the code in the body, and the detailed code will be put into my resources. You can click here to download and use it.

For more information, see the following. I want to know what examples will be provided later, and I will try again.


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.