Linux programming--Pipeline output data to Popen (13th chapter)

Source: Internet
Author: User
Tags fread

13.3 sending the output to Popen after seeing an example of capturing an external program output, look at a sample program that sends the output to an external program popen2.c, which sends the data through the pipeline to another program. The OD (octal) command is used here.
Write program popen2.c, it's very similar to popen1.c, and the only difference is this program writes data to the pipeline instead of reading from the pipeline.
/************************************************************************* > File name:popen2.c > DESCRIPTION:POPEN2.C Program writes data to pipelines > author:liubingbing > Created time:2015 July 09 Thursday 19:43 51 sec > other:popen2.c The function of the program is equivalent to entering echo "Once upon a time, there was ..." on the command line | Od-c ************************************************************************/#include <stdio.h> #include <stdlib.h> #include <stdio.h> #include <string.h>int main () {FILE *write_fp;char Buffer[bufsiz + 1];/* Writes a string to buffer */sprintf (buffer, "Once upon a time, there was...\n");/* function Popen allow one program to transfer data to another program * second parameter "W"  Indicates that the calling program can send data to the callee using the fwrite call * The file stream pointer write_fp the data pointed to as the standard input */WRITE_FP = Popen ("Od-c", "W") of the Command "Od-c"; if (write_fp! = NULL) {/* Fwrite writes a data block to a file * The data in buffer is written to the file pointed to by WRITE_FP, written to strlen (buffer) elements, each element has a size of sizeof (char) bytes */fwrite (buffer, sizeof (char), strlen (buffer), WRITE_FP);p close (WRITE_FP); exit (exit_success); Exit (exit_failure);} 
The program uses Popen with the parameter "W" to start the od-c command so that data can be sent to the command. It then sends a string to the OD-C command, which receives and processes it, and finally prints the processing results to its own standard output.
On the command line, you can use the following command to get the same output:
$ echo "Once upon a time, there was ..." | Od-c
The results of the program popen2.c are as follows:

13.3.1 the mechanism used to pass more data is simply to send or receive all data through a single fread or fwrite. Sometimes you might want to send data in chunks, or you don't know the length of the output data at all. To avoid defining a very large buffer, You can use multiple fread or fwrite to divide the data into several parts.
Write program POPEN3.C, all data is read through the pipeline
/************************************************************************* > File name:popen3.c > Description : Popen3.c program reads large amount of data through pipe > author:liubingbing > Created time:2015 July 09 Thursday 20:34 37 sec > Other:pop en3.c ************************************************************************/#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h>int main () {FILE *read_fp;char buffer[bufsiz + 1]; int chars_read;memset (buffer, ' n ', sizeof (buffer));/* The Popen function accesses the information given by the PS AX command * The first parameter "PS Ax" is the program name to run and the corresponding parameter * the second parameter Open_ Mode= "R" indicates that the output of the callee can be called by the program using the * return value of the file* file stream pointer read_fp, using READ_FP can read the output information of "PS ax" */READ_FP = Popen ("PS Ax", "R"); if (Read_ fp = NULL) {/* Fread function reads data from a file stream pointer read_fp to a file stream * reads up to bufsiz elements, each element sizeof (char) bytes * Buffer used to receive data memory address * If successful returns the actual Read element Number of */chars_read = fread (buffer, sizeof (char), Bufsiz, READ_FP); if (Chars_read > 0) {buffer[chars_read-1] = ';p rin ' TF ("Reading%d:-\n%s\n", bufsiz, buffer); chars_reAD = fread (buffer, sizeof (char), Bufsiz, READ_FP);} /* Pclose close the file stream associated with the Popen (READ_FP points to the file stream) */pclose (READ_FP); exit (exit_success);} Exit (exit_failure);}
In this program, the data is read from the called Process PS ax. How much data the process outputs is not known beforehand, so the pipeline must be read multiple times.
This program uses the "R" parameter when calling the Popen function, which is the same as the popen1.c procedure. This time, it reads data from the file stream continuously until there is no data to read. Note that while the PS command takes some time to execute, Linux schedules scheduling between processes, Keep two programs running while they can run. If the read process Popen3 no data to read, it will be suspended until there is data to arrive. If the write process PS produces more data than the available buffer, it will be suspended until the read process reads some data.
In this case, you may not see the second occurrence of the reading:-information. This happens if the value of Bufsiz exceeds the length of the PS command output.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Linux programming--Pipeline output data to Popen (13th chapter)

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.