Chinaunix首页 | 论坛 | 博客
  • 博客访问: 119085
  • 博文数量: 41
  • 博客积分: 1695
  • 博客等级: 上尉
  • 技术积分: 430
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-21 22:50
文章分类

全部博文(41)

文章存档

2010年(1)

2007年(23)

2006年(17)

我的朋友

分类: C/C++

2007-04-23 20:12:45

#include
#include
#include
#include
#include
#include
#include

#define BUFFSIZE 100

void
sig_alrm(int signo)
{
    return;
}
int main(int argc, char *argv[])
{
    int fd[2], n,;
    pid_t pid;
    char buf[BUFFSIZE];

    if (pipe(fd) < 0)
        perror("pipe error!\n");

    if ((pid = fork()) < 0)
        perror("fork error\n");
    else if (pid > 0) {
        close(fd[0]);
        while ((n = read(STDIN_FILENO, buf, BUFFSIZE)) > 0)
            if (write(fd[1], buf, n) != n)
                perror("write error");

        close(fd[1]);
        kill(pid, SIGALRM);
        if (waitpid(pid, NULL, 0) < 0)
            perror("wait error\n");
        exit(0);        
    }
    else {
        signal(SIGALRM, sig_alrm);
        sleep(20);
        close(fd[1]);
        while ((n = read(fd[0], buf, BUFFSIZE)) > 0) {
            if (write(STDOUT_FILENO, buf, n) != n)
                perror("write error\n");
        exit(0);
        }
    }
}
阅读(1133) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~