Inter-Program Collaboration mode-shell out

Source: Internet
Author: User

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

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.