程序間協作方式-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);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章