码迷,mamicode.com
首页 > 系统相关 > 详细

程序间协作方式-shell out

时间:2015-06-05 17:40:45      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:popen   shellout   

程序间协作方式-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);
}

程序间协作方式-shell out

标签:popen   shellout   

原文地址:http://blog.csdn.net/vonzhoufz/article/details/46377971

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!