程序间协作方式-shell out
程序间协作方式-shell out
shell out这个名词虽然没怎么听说,但是说不定在默默的使用,说白了就是通过一个程序通过命令的形式调用另一个程序,即system(2)系统调用。其中存在的交互(或者称为协议)很简单,一个执行完了,接着另一个再执行,不会有通信过程,最多有调用程序向被调用程序传递参数。
这种模式的变形,就是C库函数popen()和pclose(),我们可以把popen这种机制看做是对shell out 和 管道的一个上层封装:创建管道,fork一个子进程,关闭不使用的端,运行shell命令,接受输入,或者运行主程序,给shell命令提供输出。场景是相似的。
In a common variant of this pattern, the specialist program may accept input on its standard input, and be called with the C library entry point popen(…, “w”) or as part of a shellscript. Or it may send output to its standard output, and be called with popen(…, “r”) or as part of a shellscript. (If it both reads from standard input and writes to standard output, it does so in a batch mode, completing all reads before doing any writes.) This kind of child process is not usually referred to as a shellout; there is no standard jargon for it, but it might well be called a ‘bolt-on’.
–《Unix编程艺术》
下面是APUE上面的一个实例,通过popen,向分页程序输送内容。
#include "util.h"
#define PAGER "${PAGER:-more}" // environment var or default
int main(int argc, char *argv[]){
char line[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);
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。