歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> Linux中的popen-pclose簡介

Linux中的popen-pclose簡介

日期:2017/3/3 16:24:00   编辑:關於Linux

popen

Linux C

進程I/O函數,與pclose函數一起使用。

表頭文件

#include <stdio.h>

函數定義

FILE * popen ( const char * command , const char * type );

int pclose ( FILE * stream );

函數說明

popen() 函數通過創建一個管道,調用 fork 產生一個子進程,執行一個 shell 以運行命令來開啟一個進程。這個進程必須由 pclose() 函數關閉,而不是 fclose() 函數。pclose() 函數關閉標准 I/O 流,等待命令執行結束,然後返回 shell 的終止狀態。如果 shell 不能被執行,則 pclose() 返回的終止狀態與 shell 已執行 exit 一樣。

type 參數只能是讀或者寫中的一種,得到的返回值(標准 I/O 流)也具有和 type 相應的只讀或只寫類型。如果 type 是 "r" 則文件指針連接到 command 的標准輸出;如果 type 是 "w" 則文件指針連接到 command 的標准輸入。

command 參數是一個指向以 NULL 結束的 shell 命令字符串的指針。這行命令將被傳到 bin/sh 並使用-c 標志,shell 將執行這個命令。

popen 的返回值是個標准 I/O 流,必須由 pclose 來終止。前面提到這個流是單向的。所以向這個流寫內容相當於寫入該命令的標准輸入;命令的標准輸出和調用 popen 的進程相同。與之相反的,從流中讀數據相當於讀取命令的標准輸出;命令的標准輸入和調用 popen 的進程相同。

返回值

如果調用 fork() 或 pipe() 失敗,或者不能分配內存將返回NULL,否則返回標准 I/O 流。

返回錯誤

popen 沒有為內存分配失敗設置 errno 值。

如果調用 fork() 或 pipe() 時出現錯誤,errno 被設為相應的錯誤類型。

如果 type 參數不合法,errno將返回EINVAL。

使用舉例

if((fp=popen("/usr/bin/uptime","r"))==NULL);

{

sprintf(buf,"error: %s\n", strerror(errno));

....//異常處理

}

else

{

....

pclose(fp);

}

<span style="background-color: rgb(255, 255, 255);"><span style="font-size:18px;"><strong>#define _LINE_LENGTH 300  
  int get_path_total(const char *path, long long* total) {  
  int err=-1;  
  FILE *file;  
  char line[_LINE_LENGTH];  
  char *p;  
  char tmp[100];  
  char *token;  
  sprintf(tmp, "df %s", path);  
  file = popen(tmp, "r");  
  if (file != NULL) {  
  if (fgets(line, _LINE_LENGTH, file) != NULL) {  
  if (fgets(line, _LINE_LENGTH, file) != NULL) {  
  token = strtok(line, " ");  
  if (token != NULL) {  
  // printf("token=%s\n", token);  
  }  
  token = strtok(NULL, " ");  
  if (token != NULL) {  
  // printf("token=%s\n", token);  
  *total=atoll(token)/1024;//k/1024  
  err=0;  
  }  
  }  
  }  
  pclose(file);  
  }  
  return err;  
  }</strong></span></span>

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等

作者:zieckey (http://zieckey.cublog.cn)

All Rights Reserved!

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則用於使用結束後關閉這個指針。

下面看一個例子:

<span style="background-color: rgb(255, 255, 255);"><span style="font-size:18px;"><strong>#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;  
}     
</strong></span></span>

-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

Copyright © Linux教程網 All Rights Reserved