程序间协作方式-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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值