簡單說一下popen()函數
函數定義
|  
                
                 1 
                 
               
                 2 
                 
               
                 3 
                  |  
               
               #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。
函數原型:
|  
                
                 1 
                 
               
                 2 
                 
               
                 3 
                  |  
               
               #include “stdio.h” 
                 FILE *popen( const char * 
                 command 
                 , const char* mode ) 
                    
                 int pclose(FILE *stream_to_close); 
                 |  
             
參數說明:
command: 是一個指向以 NULL 結束的 shell 命令字符串的指針。這行命令將被傳到 bin/sh 並使用 -c 標志,shell 將執行這個命令。
mode: 只能是讀或者寫中的一種,得到的返回值(標准 I/O 流)也具有和 type 相應的只讀或只寫類型。如果 type 是 “r” 則文件指針連接到 command 的標准輸出;如果 type 是 “w” 則文件指針連接到 command 的標准輸入。
作用:
popen函數允許一個程序將另外一個程序作為新進程來啟動,並可以傳遞數據或者通過它接受數據。
其內部實現為調用 fork 產生一個子進程,執行一個 shell, 以運行命令來開啟一個進程,這個進程必須由 pclose() 函數關閉。
缺點:
使用popen的不好影響是,針對每個popen調用,不僅要啟動一個被請求的程序,還要啟動一個shell,即每個popen調用將多啟動兩個進程。
舉例:
|  
                
                 1 
                 
               
                 2 
                 
               
                 3 
                 
               
                 4 
                 
               
                 5 
                 
               
                 6 
                 
               
                 7 
                 
               
                 8 
                 
               
                 9 
                 
               
                 10 
                 
               
                 11 
                 
               
                 12 
                 
               
                 13 
                 
               
                 14 
                 
               
                 15 
                 
               
                 16 
                 
               
                 17 
                 
               
                 18 
                  |  
               
               #include<stdio.h>  
                   
                 #include<unistd.h>  
                   
                 #include<string.h>    
                   
                 int main()  
                   
                 {  
                     
                 FILE *fp=NULL;  
                     
                 FILE *fh=NULL;  
                     
                 char buff[128]={0};    
                    
                 memset(buff,0,sizeof(buff));  
                    
                 fp=popen( 
                 "ls -l" 
                 , 
                 "r" 
                 ); 
                 // 
                 將命令 
                 ls 
                 -l 同過管道讀到fp  
                    
                 fh=fopen( 
                 "shell.c" 
                 , 
                 "w+" 
                 ); 
                 // 
                 創建一個可寫的文件  
                    
                 fread(buff,1,127,fp); 
                 // 
                 將fp的數據流讀到buff中  
                    
                 fwrite(buff,1,127,fh); 
                 // 
                 將buff的數據寫入fh指向的文件中    
                    
                 pclose(fp);  
                    
                 fclose(fh);    
                    
                 return 
                 0;    
                    
                 }  
                ~ 
                 |  
             
運行結果:
|  
                
                 1 
                 
               
                 2 
                 
               
                 3 
                 
               
                 4 
                 
               
                 5 
                 
               
                 6 
                 
               
                 7 
                 
               
                 8 
                  |  
               
               [lol@localhost practice]$  
                 ls 
                popen popen.c shell.c 
                [lol@localhost practice]$  
                 cat 
                 shell.c 
                total 12 
                -rwxrwxr-x. 1 lol lol 5478 May 24 15:39 popen 
                -rw-rw-r--. 1 lol lol 473 May 24 15:39 popen.c 
                -rw-rw-r--. 1 lol lol  [lol@localhost practice]$ vim popen.c 
                [lol@localhost practice]$ 
                 |  
             
