程式間協作方式-shell out

來源:互聯網
上載者:User

標籤: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 defaultint 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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.