Chinaunix首页 | 论坛 | 博客
  • 博客访问: 535646
  • 博文数量: 142
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1452
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-12 16:28
文章分类

全部博文(142)

文章存档

2016年(10)

2015年(60)

2014年(72)

我的朋友

分类: LINUX

2015-04-15 10:45:30

1. popen出现原因:
创建一个管道连接到另一个进程,然后读其输出或向其输入端发送数据是管道比较常见的操作。
2. 函数原型:
#include
FILE *popen(const char *cmdstring, const char *type);  
返回值:成功返回文件指针,失败返回NULL。
3. 功能作用
  popen调用pipe创建管道,调用fork创建父子进程。子进程执行cmdstring命令后退出,父进程返回fp指针。
3.1 popen(cmdstring,"r")
  

点击(此处)折叠或打开

  1. gwwu@hz-dev2.wgw.com:~/test/pipe>more popen_r.c
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #define MAXLINE 256

  6. int main(int argc, char *argv[])
  7. {
  8.     FILE *fp;
  9.     char buf[MAXLINE];
  10.     fp = popen("ls -al","r");

  11.     while(fgets(buf,MAXLINE,fp) != NULL) {
  12.         printf("%s\n",buf);
  13.     }
  14.     
  15.     return 0;
  16. }

点击(此处)折叠或打开

  1. 编译运行:
  2. gwwu@hz-dev2.wgw.com:~/test/pipe>gcc -g popen_r.c -o popen_r
  3. gwwu@hz-dev2.wgw.com:~/test/pipe>./popen_r
  4. 总用量 208

  5. drwxrwxr-x 2 gwwu gwwu 4096 4月 15 15:12 .

  6. drwxr-xr-x 35 gwwu gwwu 4096 4月 9 16:32 ..

  7. -rwxrwxr-x 1 gwwu gwwu 8947 4月 14 09:32 add2

  8. -rw-rw-r-- 1 gwwu gwwu 635 4月 14 09:32 add2.c

  9. -rwxrwxr-x 1 gwwu gwwu 13199 4月 14 10:10 add2_filter

  10. -rw-rw-r-- 1 gwwu gwwu 2041 4月 14 10:10 add2_filter.c

  11. -rw-rw-r-- 1 gwwu gwwu 24 4月 14 16:53 dafadfasd

  12. -rwxrwxr-x 1 gwwu gwwu 13602 4月 14 15:29 fifo

  13. -rwxrwxr-x 1 gwwu gwwu 13603 4月 14 15:29 fifo1

  14. -rw-rw-r-- 1 gwwu gwwu 2074 4月 14 15:27 fifo1.c

  15. -rw-rw-r-- 1 gwwu gwwu 2084 4月 14 15:28 fifo.c

  16. -rwxrwxr-x 1 gwwu gwwu 10472 4月 14 16:43 myuclc

  17. -rw-r--r-- 1 gwwu gwwu 519 4月 14 16:45 myuclc.c

  18. -rwxrwxr-x 1 gwwu gwwu 9748 4月 8 17:42 pipe

  19. -rw-r--r-- 1 gwwu gwwu 702 4月 8 17:42 pipe.c

  20. -rwxrwxr-x 1 gwwu gwwu 12577 4月 13 15:20 pipe_more

  21. -rw-rw-r-- 1 gwwu gwwu 1981 4月 13 15:20 pipe_more.c

  22. -rw-rw-r-- 1 gwwu gwwu 1812 4月 8 10:56 popen.c

  23. -rwxrwxr-x 1 gwwu gwwu 11976 4月 13 17:16 popen_more

  24. -rw-rw-r-- 1 gwwu gwwu 1085 4月 14 09:25 popen_more.c

  25. -rwxrwxr-x 1 gwwu gwwu 10558 4月 14 16:43 popen_pipe

  26. -rw-r--r-- 1 gwwu gwwu 740 4月 14 16:24 popen_pipe.c

  27. -rwxrwxr-x 1 gwwu gwwu 9401 4月 15 15:12 popen_r

  28. -rw-rw-r-- 1 gwwu gwwu 281 4月 15 15:10 popen_r.c

  29. -rwxrwxr-x 1 gwwu gwwu 9542 4月 15 14:57 popen_w

  30. -rw-rw-r-- 1 gwwu gwwu 322 4月 15 14:56 popen_w.c

  31. gwwu@hz-dev2.wgw.com:~/test/pipe>
3.2 popen(cmdstring,"w")



点击(此处)折叠或打开

  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>

  5. int main(int argc, char *argv[])
  6. {
  7.     FILE* fp;

  8.     fp = popen("tftp 10.155.3.245","w");

  9.     if(fp == NULL) {
  10.         printf("popen error\n");
  11.         return -1;
  12.     }

  13.     fprintf(fp,"help\n");
  14.     fprintf(fp,"quit\n");
  15.     pclose(fp);
  16. }

点击(此处)折叠或打开

  1. 编译运行:
  2. gwwu@hz-dev2.wgw.com:~/test/pipe>./popen_w
  3. tftp> help
  4. tftp-hpa 0.49
  5. Commands may be abbreviated. Commands are:

  6. connect connect to remote tftp
  7. mode set file transfer mode
  8. put send file
  9. get receive file
  10. quit exit tftp
  11. verbose toggle verbose mode
  12. trace toggle packet tracing
  13. literal toggle literal mode, ignore ':' in file name
  14. status show current status
  15. binary set mode to octet
  16. ascii set mode to netascii
  17. rexmt set per-packet transmission timeout
  18. timeout set total retransmission timeout
  19. ? print help information
  20. help print help information
  21. tftp> quit
  22. gwwu@hz-dev2.wgw.com:~/test/pipe>


阅读(815) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~