Inter-Program Collaboration mode-shell out
Shell out this noun although not how to hear, but perhaps in silent use, plainly is through a program through the form of a command to invoke another program, that is, System (2) systems call. The existence of an interaction (or protocol) is simple, one execution is done, then another executes, there is no communication process, and at most the calling program passes parameters to the callee.
The variant of this pattern is the C library function Popen () and Pclose (), which we can think of as an upper-level encapsulation of the shell out and the pipeline: Creating a pipeline, fork a subprocess, closing unused ends, running shell commands, accepting inputs, or run the main program to provide output to the shell command. The scene is similar.
In a common variant of this pattern, the specialist program could accept input on its standard input, and is called with the C Library entry point Popen (..., "w") or as part of a shellscript. Or it may send the output to their standard output, and is called with Popen (..., "R") or as part of a shellscript. (If it both reads from-standard input and writes-to-standard output, it does-in-a batch mode, completing all reads BEFO Re doing any writes.) This kind of the child process was not usually referred to as a shellout; There is no standard jargon for it, but it might well be called a ' bolt-on '.
– The Art of UNIX programming
The following is an example above Apue, through Popen, to the paging program to transport content.
#include "util.h"#define PAGER "${pager:-more}" //environment var or default intMainintargcChar*argv[]) {CharLine[maxline]; FILE *fpin, *fpout;if(ARGC! =2) Err_quit ("Usage:a.out <pathname>");if(Fpin = fopen (argv[1],"R")) = = NULL) Err_sys ("Cannot open%s", argv[1]);if(Fpout = Popen (PAGER,"W")) = = NULL) Err_sys ("Popen Error");//Copy the file to pager while(Fgets (line, MAXLINE, fpin)! = NULL) {if(fputs(line, fpout) = = EOF) Err_sys ("Fputs error to Pipe"); }if(Ferror (Fpin)) Err_sys ("Fgets Error"); Pclose (fpout);Exit(0);}
Inter-Program Collaboration mode-shell out