fork()、pipe()、dup2() 和 execlp() 的組合技法

來源:互聯網
上載者:User

pipe(),父子進程之間通訊工具。pipe有讀寫兩端,用int表搜尋示。0-read, 1-write.一端進程將資料寫入write內,另一進程由read段讀出。使用參數為一個int數組,共兩個元素,用以表示讀寫兩端的狀態。
close(),用於關閉清空pipe某端,參數為表示pipe連接埠數組二元素之一。
dup2(),用於redirection, 將pipe某端與標準i/o相連,即將standout information 寫入或者pipe內容被standin獨出。如dup2(data[1], STDOUT_FINENO).是進程與外界相連的一個工具。

----------------------

http://iteches.com/archives/5603

對一般程式來說,管線(pipe)機制很少會用到,尤其對於在 Windows 底下的程式開發人員來說,很可能前所未聞,而在 Unix-like 環境中常見的例子,就是往往在命令後面加上”|”,其意味使用”管子(pipe)”將內容匯入另一個程式裡,如:”dmesg | grep ALSA”。在該例中,Shell 有趣的將 demsg 標準輸出(standard output)全數匯入 grep 程式的標準輸入(standard input)中,再交由 grep 處理並找出存在 ALSA 字串的句子所在。

另一個情況,Web Server 在支援 CGI 時,也會使用到 pipe。這是最典型的例子,當 Web Server 欲執行一支外部 CGI Program 時,會利用 pipe 去類比該 CGI 的標準輸入及輸出(Standard I/O)。以上說明當然只是大致上的做法,細節還需加上 fork() 和 dup2() 的配合,透過個範例可清楚瞭解這部份的實現。

這是一個簡單的範例:

void cgi_run(const char* filename){    char buffer[1024] = { 0 };    int len;   int pfd[2];    int status;    pid_t pid;    /* create pipe */    if (pipe(pfd)<0)        return -1;    /* fork to execute external program or scripts */    pid = fork();    if (pid<0) {        return 0;    } else if (pid==0) { /* child process */        dup2(pfd[1], STDOUT_FILENO);        close(pfd[0]);        /* execute CGI */        execlp(filename, filename, NULL);        exit(0);    } else { /* parent process */        close(pfd[1]);        /* print output from CGI */        while((len=read(pfd[0], buffer, 1023))>0) {            buffer[len] = '\0';            printf("%s\n", buffer);        }        /* waiting for CGI */        waitpid((pid_t)pid, &status, 0);    }}
此範例相當易懂,不外乎是使用 fork() 建立一個新的 Process,再去執行一支外部 CGI,而主程式會等待外部 CGI 的輸出,再用 printf() 印出來 CGI 的輸出內容。而比較令人騷不著頭緒的部份,就是 pipe() 和 dup2() 的關係,使用 pipe() 可建立一組雙向的管線,範例中是一組有兩個整數值的數組 pfd,這是一條水管,從 pfd[0] 進入的東西會從 pfd[1] 出來,反之亦然,除此之外,它也是可以跨 Process,達成兩個程式溝通的目的。除了建立 pipe,接著利用 dup2(),可以讓管線去取代外部程式的標準輸出(standard output),然後讓主程式用管線接收。

註:pipe() 回傳的是兩個檔案描述編號(file discriptions),需要用相應的檔案函數去操控它。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.