linux popen函數

include <stdio.h>
FILE *popen(const char *command, const char *type);
描述

    popen() 函數創建一個管道 ,然後fork一個子進程, 並調用 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 函數的 輸出流默認是被全緩衝的.

 

include <stdio.h>
int pclose(FILE *stream);

描述

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

下面看一個例子:

#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

小結:popen()函數的工作原理

1、使用pipe()建立匿名管道;父進程關掉管道的其中一端;(父進程使用哪一端依據type參數而定)

2、使用fork()創建子進程,子進程關掉管道的另一端;子進程將標準輸出(或標準輸出)重定向到管道的”寫“(或”讀“)端;

3、在子進程中調用exec族函數執行commond命令,通過管道將結果傳送至父進程

4、父進程將管道的”讀“(或”寫“)端文件描述符轉換爲文件流指針。

 

原文地址:http://www.cnblogs.com/hnrainll/archive/2011/07/23/2114857.html
發佈了9 篇原創文章 · 獲贊 9 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章