This artical gave a way to learn and understand popen() and thus the following pclose() in a Russian nesting doll-like practice.
As described in The GNU C Library Reference Manual, popen and pclose is a combination of pipe (to create the pipe), fork (to create the subprocess), dup2 (to force the subprocess to use the pipe as its standard input or output channel), and exec (to execute the new program).
#include <stdlib.h>
FILE* popen (const char *command, const char *mode);
int pclose (FILE* stream);
popen() executes the shell command command as a subprocess, and creates a pipe to the subprocess and returns a stream that corresponds to that pipe. If a mode argument of “r” is specified, then one can get the output of the well-executed command line program by using fread() on the returned stream(FILE*). Otherwise if “w” is specified, one can write to the stream to send data to the standard input channel of the subprocess. The subprocess inherits its standard input or output channel from the parent process.
pclose() is used to close a stream created by popen. It waits for the child process to terminate and returns 0 if no errors occurs.
Now, let’s see the Russian dolls practice:
First of all, let’s take a look at the overview of the practice.
[root@localhost RussianDolls]# ll -t
total 40
-rwxr-xr-x. 1 root root 16120 Oct 20 23:59 innerDoll
-rwxr-xr-x. 1 root root 16128 Oct 20 23:59 outterDoll
-rw-r--r--. 1 root root 716 Oct 20 23:59 src_outter_doll.c
-rw-r--r--. 1 root root 718 Oct 20 23:55 src_inner_doll.c
And, the following shows the output of the program outterDoll, which is the final target of this practice.
[root@localhost RussianDolls]# ./outterDoll
+++++++++++++++++outter doll output+++++++++++++++++++++++
221 bytes of output have been read by outter doll:
-----------------inner doll output-----------------------
56 bytes of output have been read by inner doll:
innerDoll
outterDoll
src_outter_doll.c
src_inner_doll.c
-----------------inner doll output-----------------------
+++++++++++++++++outter doll output+++++++++++++++++++++++
Then, let’s have a look at the realization of the above.
src_inner_doll.c
#define _GNU_SOURCE
#include<stdio.h>
#include<stdlib.h>
#define BUFF_SIZE 1024
int main(int argc, char* argv[])
{
printf("-----------------inner doll output-----------------------\n");
char buffer[BUFF_SIZE] = { 0 };
FILE* stream;
size_t byteNumRead;
stream = popen("ls -t", "r");
if(stream){
byteNumRead = fread(buffer, sizeof(char), BUFF_SIZE, stream);
printf("%d bytes of output have been read by inner doll:\n%s",
byteNumRead, buffer);
}
if(pclose(stream) != 0){
fprintf (stderr, "pclose() error.\n");
}
printf("-----------------inner doll output-----------------------\n");
return EXIT_SUCCESS;
}
src_outter_doll.c
define _GNU_SOURCE
#include<stdio.h>
#include<stdlib.h>
#define BUFF_SIZE 1024
int main(int argc, char* argv[])
{
printf("+++++++++++++++++outter doll output+++++++++++++++++++++++\n");
char buffer[BUFF_SIZE] = { 0 };
FILE* stream;
size_t byteNumRead;
stream = popen("./innerDoll", "r");
if(stream){
byteNumRead = fread(buffer, sizeof(char), BUFF_SIZE, stream);
printf("%d bytes of output have been read by outter doll:\n%s",
byteNumRead, buffer);
}
if(pclose(stream) != 0){
fprintf (stderr, "pclose() error.\n");
}
printf("+++++++++++++++++outter doll output+++++++++++++++++++++++\n");
return EXIT_SUCCESS;
}
Tips: there is a easy way to prepare the frame of the source code of the outterDoll program, that is, command line " cp src_inner_doll.c src_outter_doll.c "