程序间协作方式-shell out
2015-06-05 15:58
525 查看
程序间协作方式-shell out
shell out这个名词虽然没怎么听说,但是说不定在默默的使用,说白了就是通过一个程序通过命令的形式调用另一个程序,即system(2)系统调用。其中存在的交互(或者称为协议)很简单,一个执行完了,接着另一个再执行,不会有通信过程,最多有调用程序向被调用程序传递参数。这种模式的变形,就是C库函数popen()和pclose(),我们可以把popen这种机制看做是对shell out 和 管道的一个上层封装:创建管道,fork一个子进程,关闭不使用的端,运行shell命令,接受输入,或者运行主程序,给shell命令提供输出。场景是相似的。
In a common variant of this pattern, the specialist program may accept input on its standard input, and be called with the C library entry point popen(…, “w”) or as part of a shellscript. Or it may send output to its standard output, and be called with popen(…, “r”) or as part of a shellscript. (If it both reads from standard input and writes to standard output, it does so in a batch mode, completing all reads before doing any writes.) This kind of child process is not usually referred to as a shellout; there is no standard jargon for it, but it might well be called a ‘bolt-on’.
–《Unix编程艺术》
下面是APUE上面的一个实例,通过popen,向分页程序输送内容。
#include "util.h" #define PAGER "${PAGER:-more}" // environment var or default int main(int argc, char *argv[]){ char line[MAXLINE]; FILE *fpin, *fpout; if(argc != 2) err_quit("usage:a.out <pathname>"); if((fpin = fopen(argv[1], "r")) == NULL) err_sys("cannot open %s", argv[1]); if((fpout = popen(PAGER, "w")) == NULL) err_sys("popen error"); // copy the file to pager while(fgets(line, MAXLINE, fpin) != NULL){ if(fputs(line, fpout) == EOF) err_sys("fputs error to pipe"); } if(ferror(fpin)) err_sys("fgets error"); pclose(fpout); exit(0); }
相关文章推荐
- 二、使用find和xargs
- PowerShell实现批量收集SCVMM中虚拟机IP-续
- Linux 文件传输客户端SSH Secure Shell Client
- xftp和xshell的使用
- bash参考手册之六(Bash特性)续七
- shell脚本启动,停止java应用程序
- shell基本命令
- waf shell执行错误的解决方法
- 防止端口扫描shell脚本
- Shell 基础
- linux shell 命令
- shell复习笔记----命令与参数
- 一、文件安全与权限
- SHELLING
- Drupal - upload shell with admin privilege
- bash: export: 'PATH= ......' : not a valid identifier
- shell脚本的学习
- Mac文件权限管理(shell命令)
- Mac上的终端bash命令 Bourne-Again Shell简介
- shell中变量自增的计算方法