Chinaunix首页 | 论坛 | 博客
  • 博客访问: 613470
  • 博文数量: 144
  • 博客积分: 5037
  • 博客等级: 大校
  • 技术积分: 1581
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-30 21:49
文章存档

2010年(16)

2009年(128)

分类: C/C++

2009-04-11 09:53:54

#include <stdio.h>            // ls -l | more
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>

int main(void)
{
    int fd[2];
    pid_t pid;

    if(pipe(fd) < 0)
    {
        perror("pipe error!");
        exit(0);
    }
    if((pid = fork()) < 0)
    {
        perror("fork error!");
        exit(1);
    }
    else if(pid == 0)
    {
        close(fd[0]);    //pipe read closed
        close(1);
        dup2(fd[1], 1);
        execl("/bin/ls", "ls", "-l", NULL);
        exit(1);
    }
    else
    {
        close(fd[1]);    //pipe write closed
        close(0);
        dup2(fd[0], 0);
        execl("/bin/more", "more", NULL);
        exit(1);
    }
    return 0;
}

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