Chinaunix首页 | 论坛 | 博客
  • 博客访问: 347209
  • 博文数量: 78
  • 博客积分: 3380
  • 博客等级: 中校
  • 技术积分: 857
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-16 19:39
文章分类

全部博文(78)

文章存档

2011年(31)

2010年(47)

分类: LINUX

2010-07-01 18:00:13

#include
#include
#include

int main()
{
 pid_t pid;
 int fds[2];
 if((pipe(fds))==-1)
 {
  perror("pipe error");
  exit(EXIT_FAILURE);
 }
 else
 {
  pid=fork();
  if(pid==-1)
  {
   perror("fork error");
   exit(EXIT_FAILURE);
  }
  else if(pid==0)
  {
   close(fds[0]);//close the parent prcoess writer operation
   dup2(fds[1],1);//read the data from the pipe
   
   execlp("ls","ls","--help",NULL);
   perror("dup2 error");
   exit(EXIT_SUCCESS);
  }
  else if(pid > 0)
  {
   close(fds[1]);//close the parent prcoess writer operation
   dup2(fds[0],0);//read the data from the pipe
   
   execlp("more","more",NULL);
   perror("dup2 error");
   exit(EXIT_FAILURE);

  }
 }
 return 0;
}


 

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