管道连接两个进程实例
来源:互联网 发布:r语言数据框加一列 编辑:程序博客网 时间:2024/06/07 22:04
/*该段代码演示怎样创建一个管道链接两个进程。
*用法:pipe command1 command2
*效果:command1|command2
*
*/
#include <stdio.h>
#include <unistd.h>
#define oops(m,x) {perror(m);exit(x);}
main(int ac ,char **av)
{ int thepipe[2];
int newfd;
int pid;
if(ac!=3)
{
fprintf(stderr,"usage:pipe cmd1 cmd2/n");
exit (1);
}
//创建一个管道
if(pipe(thepipe)==-1)
oops("create pipe failed!",1);
//创建一个进程
if((pid=fork())==-1)
oops("cannot fork",2);
//父进程里从管道接受子进程来的运行结果作为操作对象
if(pid>0)
{
close(thepipe[1]);
newfd=dup2(thepipe[0],0);
if(newfd==-1)
oops("could not redirect stdin.",3);
close(thepipe[0]);
execlp(av[2],av[2],NULL);//av[2]从stdin(thepipe[0])读取数据
oops(av[2],4);
}
//子进程把运行结果添加到管道,传给父进程
close(thepipe[0]);
if(dup2(thepipe[1],1)==-1)
oops("cannot redirect stdout",5);
close(thepipe[1]);
execlp(av[1],av[1],NULL);//将av[1]的运行结果->stdout(thepipe[1])
oops(av[1],6);
}
*用法:pipe command1 command2
*效果:command1|command2
*
*/
#include <stdio.h>
#include <unistd.h>
#define oops(m,x) {perror(m);exit(x);}
main(int ac ,char **av)
{ int thepipe[2];
int newfd;
int pid;
if(ac!=3)
{
fprintf(stderr,"usage:pipe cmd1 cmd2/n");
exit (1);
}
//创建一个管道
if(pipe(thepipe)==-1)
oops("create pipe failed!",1);
//创建一个进程
if((pid=fork())==-1)
oops("cannot fork",2);
//父进程里从管道接受子进程来的运行结果作为操作对象
if(pid>0)
{
close(thepipe[1]);
newfd=dup2(thepipe[0],0);
if(newfd==-1)
oops("could not redirect stdin.",3);
close(thepipe[0]);
execlp(av[2],av[2],NULL);//av[2]从stdin(thepipe[0])读取数据
oops(av[2],4);
}
//子进程把运行结果添加到管道,传给父进程
close(thepipe[0]);
if(dup2(thepipe[1],1)==-1)
oops("cannot redirect stdout",5);
close(thepipe[1]);
execlp(av[1],av[1],NULL);//将av[1]的运行结果->stdout(thepipe[1])
oops(av[1],6);
}
- 管道连接两个进程实例
- Linux c 匿名管道实例—创建两个子进程与父进程进行管道通信
- 进程示例之一----连接管道
- 两个DataTable连接实例
- 关于进程通信的实例-管道
- linux进程间通信-----管道总结实例
- 创建一个进程和两个管道,实现与进程通信
- Linux----进程间通信-管道与两个命名管道实现进程双向通信
- 利用管道技术在两个进程之间通讯.
- 利用有名管道,实现两个独立进程间的通讯
- 使用管道实现两个进程之间的通信
- 通过有名管道实现两个进程单向通讯
- 用管道连接的两个命令谁先运行
- 进程管道
- 进程管道
- linux进程通信--管道(pipe)(含实例代码)
- linux进程通信--有名管道(FIFO)(含实例代码)
- 进程间通信实例(IPC)之命名管道
- MTK Phonebook初始化流程
- String中三种加法的区别
- SQL Server 2005中设置Reporting Services发布web报表的匿名访问
- 使用Jquery+CSS实现的表格隔行凸显和当前行高亮效果
- 去掉IE7询问【你查看的网页正在试图关闭窗口?】的方法
- 管道连接两个进程实例
- SQL SERVER性能优化综述
- 腿碰了
- 网游盗号产业内幕
- 端口被占用的解决办法(asp.net)
- Ubuntu下编译安装Erlang
- 对javascript中的Date类型进行扩展
- 将Excel导入MSSQL2000时出现"对于所指定的缓冲区来讲太大"错误的解决方法
- 网页menu菜单自动生成器