linux 之 popen函數

來源:互聯網
上載者:User

描述

popen() 函數 用 建立管道 的 方式啟動一個 進程, 並調用 shell. 因為 管道是被定義成單向的, 所以 type 參數 只能定義成 唯讀或者 唯寫, 不能是 兩者同時, 結果流也相應的 是唯讀 或者 唯寫.

command 參數 是 一個 字串指標, 指向的是一個 以 null 結束符 結尾的字串, 這個字串包含 一個 shell 命令. 這個命令 被送到 /bin/sh 以-c 參數 執行, 即由 shell 來執行. type 參數 也是 一個 指向 以 null 結束符結尾的 字串的指標, 這個字串 必須是 'r' 或者 'w’ 來指明 是 讀還是寫.

popen() 函數 的 傳回值 是一個普通的 標準I/O流, 它只能用pclose() 函數 來關閉, 而不是
fclose(). 函數. 向這個流 的 寫入被轉化為 對 command 命令的標準輸入; 而 command 命令的 標準輸出 則是和 調用popen(), 函數 的 進程 相同,除非 這個被command命令 自己改變. 相反的, 讀取 一個 “被popen了的” 流, 就相當於 讀取 command 命令的標準輸出, 而 command 的標準輸入 則是和 調用popen, 函數的進程 相同.

注意, popen 函數的 輸出資料流預設是被全緩衝的.

pclose 函數 等待 相關的進程結束並返回 一個 command 命令的 退出狀態, 就像wait4 函數 一樣

本文介紹了popen函數的使用方法和行為機理,並給出實際的例子來輔助說明了popen函數的使用方法。
檔案中還介紹了幾個檔案操作的函數,如fopen,fread,fwrite等
  

popen使用FIFO管道執行外部程式。

#include <stdio.h>
FILE *popen(const char *command, const char *type);
int pclose(FILE *stream);

popen 通過type是r還是w確定command的輸入/輸出方向,r和w是相對command的管道而言的。r表示command從管道中讀入,w表示 command通過管道輸出到它的stdout,popen返回FIFO管道的檔案流指標。pclose則用於使用結束後關閉這個指標。

下面看一個例子:

/*******************************************************************************************
** Name:popen.c
**      This program is used to show the usage of popen() .
** Author:zieckey,(zieckey@yahoo.com.cn)
** Date:2007/9/30 11:47
** All rights reserved!
*******************************************************************************************/
#include <sys/types.h>  
#include <unistd.h>  
#include <stdlib.h>  
#include <stdio.h>  
#include <string.h>

int main( void )  
{  
    FILE   *stream;  
    FILE    *wstream;
    char   buf[1024];
    
    memset( buf, '/0', sizeof(buf) );//初始化buf,以免後面寫如亂碼到檔案中
    stream = popen( "ls -l", "r" ); //將“ls -l”命令的輸出 通過管道讀取(“r”參數)到FILE* stream
    wstream = fopen( "test_popen.txt", "w+"); //建立一個可寫的檔案

    fread( buf, sizeof(char), sizeof(buf),  stream);  //將剛剛FILE* stream的資料流讀取到buf中
    fwrite( buf, 1, sizeof(buf), wstream );//將buf中的資料寫到FILE    *wstream對應的流中,也是寫到檔案中
   
    pclose( stream ); 

    fclose( wstream );
   
    return 0;
}  

[root@localhost src]# gcc popen.c

[root@localhost src]# ./a.out  

[root@localhost src]# cat test_popen.txt
總計 128
-rwxr-xr-x 1 root root 5558 09-30 11:51 a.out
-rwxr-xr-x 1 root root  542 09-30 00:00 child_fork.c
-rwxr-xr-x 1 root root  480 09-30 00:13 execve.c
-rwxr-xr-x 1 root root 1811 09-29 21:33 fork.c
-rwxr-xr-x 1 root root  162 09-29 18:54 getpid.c
-rwxr-xr-x 1 root root 1105 09-30 11:49 popen.c
-rwxr-xr-x 1 root root  443 09-30 00:55 system.c
-rwxr-xr-x 1 root root    0 09-30 11:51 test_popen.txt
-rwxr-xr-x 1 root root 4094 09-30 11:39 test.txt

 

 

//

rec_fp = popen(t_cmd, "w");

//發送q 退出進程

 fwrite(quit_cmd, sizeof(char), strlen(quit_cmd), rec_fp);
 pclose(rec_fp);

//

相關文章

聯繫我們

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