13.3.2 How to implement Popen
when you request the Popen call to run a program, it first starts the shell, the SH command in the system, and then passes the command string to it as a parameter, this has two effects, one good, one bad.
In Linux, all parameter extensions are done by the shell. Therefore, starting the shell to parse the command string before starting the program allows the various shell extensions, such as what the *.c refers to, to complete before the program starts. This feature is very useful, It allows very complex shell commands to be launched through Popen. Other functions that create processes, such as execl, are more complex to invoke because the calling process has to complete the shell extension itself.
One of the less-than-good effects of using the shell is that for each popen call, not only do you start a requested program, but you start a shell, which means that each Popen call will start two more processes. From the point of view of saving system resources, the call cost of Popen function is slightly higher, And the call to the target command is slower than the normal way.
Use
program popen4.cTo demonstrate the behavior of the Popen function. This program
statistics of total rows of source files for all Popen sample programsBy displaying the contents of the file with the cat command and passing the output through the pipeline to the command wc-l, which counts the total number of rows. If you are completing this task on the command line, you can use the following command:
$ Cat Popen*.c | Wc-l
In fact, the input command wc-l POPEN*.C is simpler and more efficient, but in order to demonstrate how the Popen function works in this example, it is used as above.
Write the program popen4.c.
/************************************************************************* > File Name: popen4.c > Description: popen4.c Program demonstrates the behavior of the Popen function, which counts the total number of total rows of source files for all Popen sample programs > Author: liubingbing > Created time: July 09, 2015 Thursday 20:39 42 sec > Other: popen4.c **************************************************************** /#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <string.h>int Main () {FILE *read_fp;char buffer[bufsiz + 1];int chars_read;memset (buffer, ' + ', sizeof); read_fp = Popen ("Cat popen*.c | Wc-l "," R "), if (read_fp! = NULL) {chars_read = fread (buffer, sizeof (char), Bufsiz, READ_FP);p rintf (" Chars_read =%d\n ", c Hars_read); while (Chars_read > 0) {buffer[chars_read-1] = ';p rintf ("reading:-\n%s\n", buffer); chars_read = Fread ( Buffer, sizeof (char), Bufsiz, READ_FP);} Pclose (READ_FP); exit (exit_success);} Exit (exit_failure);}
This program shows that the shell expands the popen*.c to a list of files after launch, the names of the lists begin with Popen, and the shell also processes the pipe symbol |, ending with. C, and passes the output of the cat command to the WC command. Started the shell in a popen call, The cat program and the WC program, and performed an output redirect. The program that calls these commands only sees the final output.
The results of the program popen4.c are as follows: (the file that read_fp points to is supposed to be a cat popen*.c | The value returned by Wc-l is 146.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Linux programming--How to implement Popen (13th chapter)