Linux interprocess communication Learning: How to use anonymous pipes

Source: Internet
Author: User
Tags anonymous definition fread linux

In front, we introduce a way of communication between processes: Using a signal, we create a notification event and generate a response through it, but the information passed is only a signal value. Here is an introduction to another way of interprocess communication-the anonymous pipeline, through which more useful data can be exchanged between processes.

First, what is the pipeline

If you have used Linux commands, you will not feel unfamiliar with the term pipe because we usually use the symbol "|" To use pipelines, but what is the true definition of management? A pipe is a channel in which a process connects data to another process and is usually used as an input to connect the output of one process to another process through a pipe.

For example, enter a command in the shell: ls-l | grep string, we know that the LS command (in fact, a process) lists the files in the current directory, but it does not output directly, but instead outputs the data that would otherwise be exported to the screen through the pipeline to the grep process, as the input to the grep process. The process then filters the input information and prints the string (in the behavior unit) of the information that exists in string to the screen.

Second, using the Popen function

1, Popen function and Pclose function introduction

There is static, there is movement, the open on the relevant, and the same, and the Popen function is the corresponding function is the Pclose function, their prototype is as follows:

#include <stdio.h>  
file* popen (const char *command, const char *open_mode);  
int Pclose (FILE *stream_to_close);

The Poen function allows a program to start another program as a new process, and it can pass data to it or receive data from it. The command is the name of the program to run and the corresponding parameter. Open_mode can only be one of the "R (read only)" and "W (write only)". Note that the return value of the Popen function is a pointer to a file type, and Linux treats everything as a file, which means that we can manipulate it using the file handler function in the stdio I/O library.

If the Open_mode is "R", the main caller can use the output of the invoked program, and the file pointer returned by the function can pass the stdio function (such as fread) to read the output of the program; if Open_mode is "w", The main caller can send data to the invoked program by writing the data to the invoked program through the Stdio function (such as fwrite), and the invoked program can read the data in its own standard input.

The Pclose function is used to close the associated file stream created by Popen. Pclose only returns after the Popen-initiated process finishes, and if the invoked process is still running when Pclose is invoked, the Pclose call waits for the process to end. It returns the exit code of the process where the closed file stream is located.

2. Examples

Most of the time, we simply do not know the length of the output data, in order to avoid the definition of a very large array as a buffer, we can block the way to send data, read a block of data at a time and send a block of data, until all the data is sent out. The following example is how data is read and sent in this way. The source file name is POPEN.C and the code is as follows:

#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <string.h> int  
    Main () {FILE *read_fp = NULL;  
    FILE *write_fp = NULL;  
    Char Buffer[bufsiz + 1];  
          
    int chars_read = 0;  
    Initializes a buffer memset (buffer, ' n ', sizeof (buffer));  
    Open LS and grep process read_fp = Popen ("Ls-l", "R");  
    WRITE_FP = Popen ("grep rwxrwxr-x", "w"); Two processes open successfully if (READ_FP && write_fp) {//read a block of data Chars_read = fread (buffer, sizeof (  
        char), Bufsiz, READ_FP);  
            while (Chars_read > 0) {buffer[chars_read] = ';  
            Writes data to the grep process fwrite (buffer, sizeof (char), Chars_read, WRITE_FP);  
        There is also data readable, looping through reading data until all data is read Chars_read = fread (buffer, sizeof (char), Bufsiz, READ_FP);  
        ///Close file stream Pclose (READ_FP);  
        Pclose (WRITE_FP);  
    Exit (exit_success); } EXIt (exit_failure); }

The results of the operation are as follows:

From the view of running result, the purpose of information filtering is achieved. The program reads the data in process LS and then sends the data to process grep for filtering, which is equivalent to entering the command directly in the shell: ls-l | grep rwxrwxr-x.

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.