1. popen出现原因:
创建一个管道连接到另一个进程,然后读其输出或向其输入端发送数据是管道比较常见的操作。
2. 函数原型:
#include
FILE *popen(const char *cmdstring, const char *type);
返回值:成功返回文件指针,失败返回NULL。
3. 功能作用
popen调用pipe创建管道,调用fork创建父子进程。子进程执行cmdstring命令后退出,父进程返回fp指针。
3.1 popen(cmdstring,"r")
-
gwwu@hz-dev2.wgw.com:~/test/pipe>more popen_r.c
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#define MAXLINE 256
-
-
int main(int argc, char *argv[])
-
{
-
FILE *fp;
-
char buf[MAXLINE];
-
fp = popen("ls -al","r");
-
-
while(fgets(buf,MAXLINE,fp) != NULL) {
-
printf("%s\n",buf);
-
}
-
-
return 0;
-
}
-
编译运行:
-
gwwu@hz-dev2.wgw.com:~/test/pipe>gcc -g popen_r.c -o popen_r
-
gwwu@hz-dev2.wgw.com:~/test/pipe>./popen_r
-
总用量 208
-
-
drwxrwxr-x 2 gwwu gwwu 4096 4月 15 15:12 .
-
-
drwxr-xr-x 35 gwwu gwwu 4096 4月 9 16:32 ..
-
-
-rwxrwxr-x 1 gwwu gwwu 8947 4月 14 09:32 add2
-
-
-rw-rw-r-- 1 gwwu gwwu 635 4月 14 09:32 add2.c
-
-
-rwxrwxr-x 1 gwwu gwwu 13199 4月 14 10:10 add2_filter
-
-
-rw-rw-r-- 1 gwwu gwwu 2041 4月 14 10:10 add2_filter.c
-
-
-rw-rw-r-- 1 gwwu gwwu 24 4月 14 16:53 dafadfasd
-
-
-rwxrwxr-x 1 gwwu gwwu 13602 4月 14 15:29 fifo
-
-
-rwxrwxr-x 1 gwwu gwwu 13603 4月 14 15:29 fifo1
-
-
-rw-rw-r-- 1 gwwu gwwu 2074 4月 14 15:27 fifo1.c
-
-
-rw-rw-r-- 1 gwwu gwwu 2084 4月 14 15:28 fifo.c
-
-
-rwxrwxr-x 1 gwwu gwwu 10472 4月 14 16:43 myuclc
-
-
-rw-r--r-- 1 gwwu gwwu 519 4月 14 16:45 myuclc.c
-
-
-rwxrwxr-x 1 gwwu gwwu 9748 4月 8 17:42 pipe
-
-
-rw-r--r-- 1 gwwu gwwu 702 4月 8 17:42 pipe.c
-
-
-rwxrwxr-x 1 gwwu gwwu 12577 4月 13 15:20 pipe_more
-
-
-rw-rw-r-- 1 gwwu gwwu 1981 4月 13 15:20 pipe_more.c
-
-
-rw-rw-r-- 1 gwwu gwwu 1812 4月 8 10:56 popen.c
-
-
-rwxrwxr-x 1 gwwu gwwu 11976 4月 13 17:16 popen_more
-
-
-rw-rw-r-- 1 gwwu gwwu 1085 4月 14 09:25 popen_more.c
-
-
-rwxrwxr-x 1 gwwu gwwu 10558 4月 14 16:43 popen_pipe
-
-
-rw-r--r-- 1 gwwu gwwu 740 4月 14 16:24 popen_pipe.c
-
-
-rwxrwxr-x 1 gwwu gwwu 9401 4月 15 15:12 popen_r
-
-
-rw-rw-r-- 1 gwwu gwwu 281 4月 15 15:10 popen_r.c
-
-
-rwxrwxr-x 1 gwwu gwwu 9542 4月 15 14:57 popen_w
-
-
-rw-rw-r-- 1 gwwu gwwu 322 4月 15 14:56 popen_w.c
-
-
gwwu@hz-dev2.wgw.com:~/test/pipe>
3.2 popen(cmdstring,"w")
-
#include <unistd.h>
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
-
int main(int argc, char *argv[])
-
{
-
FILE* fp;
-
-
fp = popen("tftp 10.155.3.245","w");
-
-
if(fp == NULL) {
-
printf("popen error\n");
-
return -1;
-
}
-
-
fprintf(fp,"help\n");
-
fprintf(fp,"quit\n");
-
pclose(fp);
-
}
-
编译运行:
-
gwwu@hz-dev2.wgw.com:~/test/pipe>./popen_w
-
tftp> help
-
tftp-hpa 0.49
-
Commands may be abbreviated. Commands are:
-
-
connect connect to remote tftp
-
mode set file transfer mode
-
put send file
-
get receive file
-
quit exit tftp
-
verbose toggle verbose mode
-
trace toggle packet tracing
-
literal toggle literal mode, ignore ':' in file name
-
status show current status
-
binary set mode to octet
-
ascii set mode to netascii
-
rexmt set per-packet transmission timeout
-
timeout set total retransmission timeout
-
? print help information
-
help print help information
-
tftp> quit
-
gwwu@hz-dev2.wgw.com:~/test/pipe>
阅读(863) | 评论(0) | 转发(0) |